Skip to content

Commit

Permalink
fix(homematic): registered controller set was not initialized on cons…
Browse files Browse the repository at this point in the history
…tructor

related to #421
  • Loading branch information
xaviml committed Apr 24, 2022
1 parent 37d8e0b commit ac52dee
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
15 changes: 11 additions & 4 deletions apps/controllerx/cx_core/integration/homematic.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,34 @@
from typing import Any, Dict, Optional, Set
from typing import TYPE_CHECKING, Any, Dict, Optional, Set

from appdaemon.plugins.hass.hassapi import Hass
from cx_const import DefaultActionsMapping
from cx_core.integration import EventData, Integration

if TYPE_CHECKING:
from cx_core.controller import Controller


class HomematicIntegration(Integration):
name = "homematic"
_registererd_controller_ids: Set[str] = set()
_registered_controller_ids: Set[str]

def __init__(self, controller: "Controller", kwargs: Dict[str, Any]):
self._registered_controller_ids = set()
super().__init__(controller, kwargs)

def get_default_actions_mapping(self) -> Optional[DefaultActionsMapping]:
return self.controller.get_homematic_actions_mapping()

async def listen_changes(self, controller_id: str) -> None:
self._registererd_controller_ids.add(controller_id)
self._registered_controller_ids.add(controller_id)
await Hass.listen_event(
self.controller, self.event_callback, "homematic.keypress"
)

async def event_callback(
self, event_name: str, data: EventData, kwargs: Dict[str, Any]
) -> None:
if data["name"] not in self._registererd_controller_ids:
if data["name"] not in self._registered_controller_ids:
return
param = data["param"]
channel = data["channel"]
Expand Down
6 changes: 3 additions & 3 deletions tests/unit_tests/cx_core/integration/homematic_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ async def test_callback(
) -> None:
handle_action_patch = mocker.patch.object(fake_controller, "handle_action")
integration = HomematicIntegration(fake_controller, {})
integration._registererd_controller_ids = registered_controllers
integration._registered_controller_ids = registered_controllers

await integration.event_callback("test", data, {})

Expand All @@ -56,11 +56,11 @@ async def test_listen_changes(
controller_id = "controller_id"
listen_event_mock = mocker.patch.object(Hass, "listen_event")
integration = HomematicIntegration(fake_controller, {})
assert integration._registererd_controller_ids == set()
assert integration._registered_controller_ids == set()

await integration.listen_changes(controller_id)

listen_event_mock.assert_called_once_with(
fake_controller, integration.event_callback, "homematic.keypress"
)
assert integration._registererd_controller_ids == {controller_id}
assert integration._registered_controller_ids == {controller_id}

0 comments on commit ac52dee

Please sign in to comment.