Skip to content

Commit

Permalink
refactor(controller_base): change listen_changes to async
Browse files Browse the repository at this point in the history
  • Loading branch information
xaviml committed Jun 24, 2021
1 parent 213012c commit b86e45d
Show file tree
Hide file tree
Showing 12 changed files with 26 additions and 23 deletions.
4 changes: 2 additions & 2 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ PRERELEASE_NOTE
## :pencil2: Features
-->

<!--
## :hammer: Fixes
-->

- Clean action handle when there is an error. This will help for error logging.

<!--
## :clock2: Performance
Expand Down
2 changes: 1 addition & 1 deletion apps/controllerx/cx_core/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ async def init(self) -> None:

# Listen for device changes
for controller_id in controllers_ids:
self.integration.listen_changes(controller_id)
await self.integration.listen_changes(controller_id)

def filter_actions(
self,
Expand Down
2 changes: 1 addition & 1 deletion apps/controllerx/cx_core/integration/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def get_default_actions_mapping(self) -> Optional[DefaultActionsMapping]:
raise NotImplementedError

@abc.abstractmethod
def listen_changes(self, controller_id: str) -> None:
async def listen_changes(self, controller_id: str) -> None:
raise NotImplementedError


Expand Down
4 changes: 2 additions & 2 deletions apps/controllerx/cx_core/integration/deconz.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ class DeCONZIntegration(Integration):
def get_default_actions_mapping(self) -> Optional[DefaultActionsMapping]:
return self.controller.get_deconz_actions_mapping()

def listen_changes(self, controller_id: str) -> None:
Hass.listen_event(
async def listen_changes(self, controller_id: str) -> None:
await Hass.listen_event(
self.controller, self.event_callback, "deconz_event", id=controller_id
)

Expand Down
4 changes: 2 additions & 2 deletions apps/controllerx/cx_core/integration/lutron_caseta.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ class LutronIntegration(Integration):
def get_default_actions_mapping(self) -> Optional[DefaultActionsMapping]:
return self.controller.get_lutron_caseta_actions_mapping()

def listen_changes(self, controller_id: str) -> None:
Hass.listen_event(
async def listen_changes(self, controller_id: str) -> None:
await Hass.listen_event(
self.controller,
self.callback,
"lutron_caseta_button_event",
Expand Down
4 changes: 2 additions & 2 deletions apps/controllerx/cx_core/integration/mqtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ class MQTTIntegration(Integration):
def get_default_actions_mapping(self) -> Optional[DefaultActionsMapping]:
return self.controller.get_z2m_actions_mapping()

def listen_changes(self, controller_id: str) -> None:
Mqtt.listen_event(
async def listen_changes(self, controller_id: str) -> None:
await Mqtt.listen_event(
self.controller, self.event_callback, topic=controller_id, namespace="mqtt"
)

Expand Down
4 changes: 2 additions & 2 deletions apps/controllerx/cx_core/integration/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ class StateIntegration(Integration):
def get_default_actions_mapping(self) -> Optional[DefaultActionsMapping]:
return self.controller.get_z2m_actions_mapping()

def listen_changes(self, controller_id: str) -> None:
async def listen_changes(self, controller_id: str) -> None:
attribute = self.kwargs.get("attribute", None)
Hass.listen_state(
await Hass.listen_state(
self.controller, self.state_callback, controller_id, attribute=attribute
)

Expand Down
6 changes: 3 additions & 3 deletions apps/controllerx/cx_core/integration/z2m.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ class Z2MIntegration(Integration):
def get_default_actions_mapping(self) -> Optional[DefaultActionsMapping]:
return self.controller.get_z2m_actions_mapping()

def listen_changes(self, controller_id: str) -> None:
async def listen_changes(self, controller_id: str) -> None:
listens_to = self.kwargs.get("listen_to", LISTENS_TO_HA)
if listens_to == LISTENS_TO_HA:
Hass.listen_state(self.controller, self.state_callback, controller_id)
await Hass.listen_state(self.controller, self.state_callback, controller_id)
elif listens_to == LISTENS_TO_MQTT:
topic_prefix = self.kwargs.get("topic_prefix", "zigbee2mqtt")
Mqtt.listen_event(
await Mqtt.listen_event(
self.controller,
self.event_callback,
topic=f"{topic_prefix}/{controller_id}",
Expand Down
4 changes: 2 additions & 2 deletions apps/controllerx/cx_core/integration/zha.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ class ZHAIntegration(Integration):
def get_default_actions_mapping(self) -> Optional[DefaultActionsMapping]:
return self.controller.get_zha_actions_mapping()

def listen_changes(self, controller_id: str) -> None:
Hass.listen_event(
async def listen_changes(self, controller_id: str) -> None:
await Hass.listen_event(
self.controller, self.callback, "zha_event", device_ieee=controller_id
)

Expand Down
6 changes: 3 additions & 3 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ def hass_mock(monkeypatch: MonkeyPatch):
"""

monkeypatch.setattr(hass.Hass, "__init__", fake_fn())
monkeypatch.setattr(hass.Hass, "listen_event", fake_fn())
monkeypatch.setattr(mqtt.Mqtt, "listen_event", fake_fn())
monkeypatch.setattr(hass.Hass, "listen_state", fake_fn())
monkeypatch.setattr(hass.Hass, "listen_event", fake_fn(async_=True))
monkeypatch.setattr(mqtt.Mqtt, "listen_event", fake_fn(async_=True))
monkeypatch.setattr(hass.Hass, "listen_state", fake_fn(async_=True))
monkeypatch.setattr(hass.Hass, "log", fake_fn())
monkeypatch.setattr(hass.Hass, "call_service", fake_fn(async_=True))
monkeypatch.setattr(hass.Hass, "get_ad_version", fake_fn(to_return="4.0.0"))
Expand Down
5 changes: 4 additions & 1 deletion tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ def __init__(self, name: str, controller: "Controller", mocker: MockerFixture):
self.get_default_actions_mapping = MagicMock(
name="get_default_actions_mapping", return_value={}
)
self.listen_changes = mocker.stub(name="listen_changes")
self.listen_changes_stub = mocker.stub(name="listen_changes")

async def listen_changes(self, controller_id: str) -> None:
self.listen_changes_stub(controller_id)


def fake_fn(to_return=None, async_: bool = False) -> Callable:
Expand Down
4 changes: 2 additions & 2 deletions tests/unit_tests/cx_core/controller_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,8 @@ async def test_initialize(
if not error_expected:
get_default_actions_mapping.assert_called_once()
for controller_id in controller_input:
integration_mock.listen_changes.assert_any_call(controller_id)
assert integration_mock.listen_changes.call_count == len(controller_input)
integration_mock.listen_changes_stub.assert_any_call(controller_id)
assert integration_mock.listen_changes_stub.call_count == len(controller_input)
assert list(sut_before_init.actions_mapping.keys()) == actions_output


Expand Down

0 comments on commit b86e45d

Please sign in to comment.