Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
n8henrie committed May 29, 2024
2 parents cd51040 + d9cf13e commit f5bcad9
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 59 deletions.
1 change: 0 additions & 1 deletion .github/workflows/python-package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ jobs:
strategy:
matrix:
python-version:
- "3.7"
- "3.8"
- "3.9"
- "3.10"
Expand Down
5 changes: 0 additions & 5 deletions .python-version

This file was deleted.

15 changes: 0 additions & 15 deletions .travis.yml

This file was deleted.

2 changes: 2 additions & 0 deletions cecplugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
cec>=0.2.8
"""

from __future__ import annotations

import typing as t

import cec
Expand Down
32 changes: 16 additions & 16 deletions mqttplugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,15 @@
```
Dependencies:
paho-mqtt==1.6.1
paho-mqtt==2.1.0
"""

from __future__ import annotations

import typing as t

from fauxmo.plugins import FauxmoPlugin
from paho.mqtt.client import Client, MQTTMessage
from paho.mqtt.client import CallbackAPIVersion, Client, MQTTMessage


class MQTTPlugin(FauxmoPlugin):
Expand Down Expand Up @@ -129,12 +131,17 @@ def __init__(
self.on_cmd, self.on_value = on_cmd[0], on_cmd[1]
self.off_cmd, self.off_value = off_cmd[0], off_cmd[1]
self.state_cmd = state_cmd
self.status = "unknown"
self._subscribed = False
self.initial_state = initial_state
self.use_fake_state = use_fake_state

self.client = Client(client_id=mqtt_client_id)
self.use_fake_state = use_fake_state
if initial_state is not None:
self.status = initial_state
else:
self.status = "unknown"

self.client = Client(
CallbackAPIVersion.VERSION1, client_id=mqtt_client_id
)
if mqtt_user or mqtt_pw:
self.client.username_pw_set(mqtt_user, mqtt_pw)
self.client.on_connect = self.on_connect
Expand All @@ -143,7 +150,7 @@ def __init__(

self.client.connect(mqtt_server, mqtt_port, 60)

super().__init__(name=name, port=port)
super().__init__(name=name, port=port, initial_state=initial_state)

self.client.loop_start()

Expand All @@ -157,7 +164,7 @@ def on_subscribe(
client: Client,
userdata: str,
mid: int,
granted_qos: t.Tuple[int],
granted_qos: t.Tuple[int, ...],
) -> None:
"""Set attribute to show that initial subscription is complete."""
self._subscribed = True
Expand Down Expand Up @@ -217,14 +224,7 @@ def get_state(self) -> str:
State if known, else "unknown".
"""
if self.status != "unknown":
return self.status

if self.initial_state is not None:
state, self.initial_state = self.initial_state, None
return state

if self.use_fake_state is True:
return super().get_state()

return "unknown"
return self.status
2 changes: 1 addition & 1 deletion requirements-test.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ flake8-import-order==0.18.2
flake8==3.9.2
httpbin==0.7.0
mypy==1.2.0
paho-mqtt==1.6.1
paho-mqtt==2.1.0
pycodestyle==2.7.0
pydocstyle==6.1.1
pytest==7.3.0
Expand Down
4 changes: 2 additions & 2 deletions restapiplugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
requests==2.28.2
"""

from typing import Union # noqa
from __future__ import annotations

import requests
from fauxmo.plugins import FauxmoPlugin
Expand Down Expand Up @@ -133,7 +133,7 @@ def __init__(
self.method = method
self.state_method = state_method
self.headers = headers
self.auth: Union[HTTPBasicAuth, HTTPDigestAuth] = None
self.auth: HTTPBasicAuth | HTTPDigestAuth = None

self.on_cmd = on_cmd
self.off_cmd = off_cmd
Expand Down
35 changes: 16 additions & 19 deletions tests/test_mqttplugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
import time
from unittest.mock import MagicMock, patch

from mqttplugin import MQTTPlugin
from mqttplugin import CallbackAPIVersion, MQTTPlugin

config_path_str = "tests/test_mqttplugin_config.json"


def test_mqttplugin_mosquitto_dot_org() -> None:
"""Test MQTTPlugin against test.mosquitto.org."""
retries = 100

with open(config_path_str) as f:
config: dict = json.load(f)

Expand All @@ -22,27 +24,27 @@ def test_mqttplugin_mosquitto_dot_org() -> None:
for device_conf in mosquitto_devices:
device = MQTTPlugin(**device_conf)

for _ in range(100):
for rt in range(100):
if device.subscribed is True:
break
time.sleep(0.1)
time.sleep(rt / 10)
else:
assert False, "Time out waiting for subscribe."

assert device.on() is True
for _ in range(20):
for rt in range(retries):
state = device.get_state()
if state != "unknown":
break
time.sleep(0.1)
time.sleep(rt / 10)
assert state == "on"

assert device.off() is True
for _ in range(20):
for rt in range(retries):
state = device.get_state()
if state != "on":
break
time.sleep(0.1)
time.sleep(rt / 10)
assert state == "off"


Expand All @@ -66,20 +68,15 @@ def test_mqtt_auth(mock_client: MagicMock) -> None:

@patch("mqttplugin.Client", autospec=True)
def test_mqtt_nostate(mock_client: MagicMock) -> None:
"""If state_cmd is not specified, loop_start is not called."""
mock_instance = mock_client.return_value
"""Ensure loop_start is called whether or not no_state is specified."""
with open(config_path_str) as f:
config: dict = json.load(f)

device_conf = next(
device
for device in config["PLUGINS"]["MQTTPlugin"]["DEVICES"]
if device["mqtt_server"] == "mqtt.yes_auth.no_state"
)
device = MQTTPlugin(**device_conf)
mock_instance.loop_start.assert_not_called()
mock_instance.subscribe.assert_not_called()
assert device.subscribed is False
for device_conf in config["PLUGINS"]["MQTTPlugin"]["DEVICES"]:
mock_instance = mock_client.return_value
_ = MQTTPlugin(**device_conf)
mock_instance.loop_start.assert_called_once()
mock_client.reset_mock()


@patch("mqttplugin.Client", autospec=True)
Expand Down Expand Up @@ -111,5 +108,5 @@ def test_mqtt_clientid(mock_client: MagicMock) -> None:
)
MQTTPlugin(**device_conf)
mock_client.assert_called_once_with(
client_id=device_conf["mqtt_client_id"]
CallbackAPIVersion.VERSION1, client_id=device_conf["mqtt_client_id"]
)
2 changes: 2 additions & 0 deletions zwaveplugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@
requests==2.28.2
"""

from __future__ import annotations

import requests
from fauxmo import logger
from fauxmo.plugins import FauxmoPlugin
Expand Down

0 comments on commit f5bcad9

Please sign in to comment.