- Platform information:
- Hardware: Pi3B+
- OS: Raspbian GNU/Linux 9 (stretch)
- Java Runtime Environment: openjdk version “1.8.0_242”
- openHAB version: 2.5.3 Release Build
I am trying to use pyunifi in a rule in order for me to see whether anyone has joined the guest network (and therefore whether I have guests in the house).
Setup
I have added the path to the python2.7 dist-packages into the EXTRA_JAVA_OPTS as follows:
sudo nano /etc/default/openhab2
then added -Dpython.path=/usr/local/lib/python2.7/dist-packages
to the end of EXTRA_JAVA_OPTS so it ends up looking like:
EXTRA_JAVA_OPTS="-Dgnu.io.rxtx.SerialPorts=/dev/ttyUSB0:/dev/ttyS0:/dev/ttyS2:/dev/ttyACM0:/dev/ttyAMA0 -Dpython.path=/usr/local/lib/python2.7/dist-packages"
Rule
My rule is as follows (triggered by a test switch for now):
from core.rules import rule
from core.triggers import when
from pyunifi.controller import Controller
@rule("Get Wifi clients", description="See if there are any guests on the wifi")
#@when("Time cron 0 */5 * ? * *")
@when("Item sStartupSwitch changed to ON")
def get_wifi_guest_status(event):
c = Controller('192.168.1.116', 'admin', 'password', ssl_verify=False)
wifi_clients = str(c.get_clients())
if("'is_guest': True" in wifi_clients):
get_wifi_guest_status.log.info("Guests on wifi")
else:
get_wifi_guest_status.log.info("No guests on wifi")
I’ve switched ssl verification off because I haven’t setup an ssl certificate on my device with the controller software.
Logs
23:42:37.806 [ERROR] [jsr223.jython.Get Wifi clients ] - Traceback (most recent call last):
File "/etc/openhab2/automation/lib/python/core/log.py", line 51, in wrapper
return fn(*args, **kwargs)
File "<script>", line 9, in get_wifi_guest_status
File "/usr/local/lib/python2.7/dist-packages/pyunifi/controller.py", line 113, in __init__
self._login()
File "/usr/local/lib/python2.7/dist-packages/pyunifi/controller.py", line 161, in _login
r = self.session.post(login_url, json=params)
File "/usr/local/lib/python2.7/dist-packages/requests/sessions.py", line 578, in post
return self.request('POST', url, data=data, json=json, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/requests/sessions.py", line 530, in request
resp = self.send(prep, **send_kwargs)
File "/usr/local/lib/python2.7/dist-packages/requests/sessions.py", line 643, in send
r = adapter.send(request, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/requests/adapters.py", line 514, in send
raise SSLError(e, request=request)
SSLError: HTTPSConnectionPool(host='192.168.1.116', port=8443): Max retries exceeded with url: /api/login (Caused by SSLError(SSLError(1, u'General SSLEngine problem (javax.net.ssl.SSLHandshakeException: General SSLEngine problem)'),))
23:42:37.818 [ERROR] [re.automation.internal.RuleEngineImpl] - Failed to execute rule '1e44517b-af42-42b2-9d9b-9b32b31c9653': Fail to execute action: 1
23:42:37.823 [DEBUG] [re.automation.internal.RuleEngineImpl] -
java.lang.RuntimeException: Fail to execute action: 1
at org.openhab.core.automation.internal.RuleEngineImpl.executeActions(RuleEngineImpl.java:1197) ~[bundleFile:?]
at org.openhab.core.automation.internal.RuleEngineImpl.runRule(RuleEngineImpl.java:993) [bundleFile:?]
at org.openhab.core.automation.internal.TriggerHandlerCallbackImpl$TriggerData.run(TriggerHandlerCallbackImpl.java:91) [bundleFile:?]
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [?:1.8.0_242]
at java.util.concurrent.FutureTask.run(FutureTask.java:266) [?:1.8.0_242]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [?:1.8.0_242]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [?:1.8.0_242]
at java.lang.Thread.run(Thread.java:748) [?:1.8.0_242]
Caused by: org.python.core.PyException
at org.python.core.Py.AttributeError(Py.java:205) ~[?:?]
at org.python.core.PyObject.noAttributeError(PyObject.java:1013) ~[?:?]
at org.python.core.PyObject.__getattr__(PyObject.java:1008) ~[?:?]
at core.rules$py.execute$6(/etc/openhab2/automation/lib/python/core/rules.py:110) ~[?:?]
at core.rules$py.call_function(/etc/openhab2/automation/lib/python/core/rules.py) ~[?:?]
at org.python.core.PyTableCode.call(PyTableCode.java:167) ~[?:?]
at org.python.core.PyBaseCode.call(PyBaseCode.java:307) ~[?:?]
at org.python.core.PyBaseCode.call(PyBaseCode.java:198) ~[?:?]
at org.python.core.PyFunction.__call__(PyFunction.java:482) ~[?:?]
at org.python.core.PyMethod.instancemethod___call__(PyMethod.java:237) ~[?:?]
at org.python.core.PyMethod.__call__(PyMethod.java:228) ~[?:?]
at org.python.core.PyMethod.__call__(PyMethod.java:218) ~[?:?]
at org.python.core.PyMethod.__call__(PyMethod.java:213) ~[?:?]
at org.python.core.PyObject._jcallexc(PyObject.java:3626) ~[?:?]
at org.python.core.PyObject._jcall(PyObject.java:3658) ~[?:?]
at org.python.proxies.core.rules$_FunctionRule$16.execute(Unknown Source) ~[?:?]
at org.openhab.core.automation.module.script.rulesupport.shared.simple.SimpleRuleActionHandlerDelegate.execute(SimpleRuleActionHandlerDelegate.java:34) ~[?:?]
at org.openhab.core.automation.module.script.rulesupport.internal.delegates.SimpleActionHandlerDelegate.execute(SimpleActionHandlerDelegate.java:59) ~[?:?]
at org.openhab.core.automation.internal.RuleEngineImpl.executeActions(RuleEngineImpl.java:1189) ~[bundleFile:?]
... 7 more
Is this something to do with the fact I’m trying to use this python module in jython, and therefore something funky is going on, or is there a simple solution? Kinda stumped at the moment!