Skip to content

Commit

Permalink
feat(integration): allow listen to unique id for deconz
Browse files Browse the repository at this point in the history
related to #333
  • Loading branch information
xaviml committed Jul 5, 2021
1 parent cfc44d0 commit a3b565b
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 1 deletion.
9 changes: 8 additions & 1 deletion apps/controllerx/cx_core/integration/deconz.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
from cx_const import DefaultActionsMapping # type:ignore
from cx_core.integration import EventData, Integration

LISTENS_TO_ID = "id"
LISTENS_TO_UNIQUE_ID = "unique_id"


class DeCONZIntegration(Integration):
name = "deconz"
Expand All @@ -12,8 +15,12 @@ def get_default_actions_mapping(self) -> Optional[DefaultActionsMapping]:
return self.controller.get_deconz_actions_mapping()

async def listen_changes(self, controller_id: str) -> None:
listens_to = self.kwargs.get("listen_to", LISTENS_TO_ID)
await Hass.listen_event(
self.controller, self.event_callback, "deconz_event", id=controller_id
self.controller,
self.event_callback,
"deconz_event",
**{listens_to: controller_id}
)

async def event_callback(
Expand Down
33 changes: 33 additions & 0 deletions tests/unit_tests/cx_core/integration/deconz_test.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from typing import Dict, Optional

import pytest
from appdaemon.plugins.hass.hassapi import Hass
from cx_core.controller import Controller
from cx_core.integration.deconz import DeCONZIntegration
from pytest_mock.plugin import MockerFixture
Expand Down Expand Up @@ -36,3 +37,35 @@ async def test_callback(
deconz_integration = DeCONZIntegration(fake_controller, kwargs)
await deconz_integration.event_callback("test", data, {})
handle_action_patch.assert_called_once_with(expected, extra=data)


@pytest.mark.parametrize(
"listen_to, expected_id",
[
("id", "id"),
("unique_id", "unique_id"),
(None, "id"),
],
)
@pytest.mark.asyncio
async def test_listen_changes(
fake_controller: Controller,
mocker: MockerFixture,
listen_to: Optional[str],
expected_id: str,
):
kwargs = {}
if listen_to is not None:
kwargs["listen_to"] = listen_to

listen_event_mock = mocker.patch.object(Hass, "listen_event")
deconz_integration = DeCONZIntegration(fake_controller, kwargs)

await deconz_integration.listen_changes("controller_id")

listen_event_mock.assert_called_once_with(
fake_controller,
deconz_integration.event_callback,
"deconz_event",
**{expected_id: "controller_id"}
)
55 changes: 55 additions & 0 deletions tests/unit_tests/cx_core/integration/z2m_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@
from typing import Any, Dict, Optional

import pytest
from appdaemon.plugins.hass.hassapi import Hass
from appdaemon.plugins.mqtt.mqttapi import Mqtt
from cx_core.controller import Controller
from cx_core.integration.z2m import Z2MIntegration
from pytest_mock import MockerFixture

from tests.test_utils import wrap_exetuction


@pytest.mark.parametrize(
"data, action_key, action_group, handle_action_called, expected_called_with",
Expand Down Expand Up @@ -56,3 +60,54 @@ async def test_event_callback(
)
else:
handle_action_patch.assert_not_called()


@pytest.mark.parametrize(
"listen_to, topic_prefix, expected_id",
[
("ha", None, "ha"),
(None, None, "ha"),
("mqtt", None, "mqtt"),
("mqtt", "my_prefix", "mqtt"),
("fake", None, None),
],
)
@pytest.mark.asyncio
async def test_listen_changes(
fake_controller: Controller,
mocker: MockerFixture,
listen_to: Optional[str],
topic_prefix: Optional[str],
expected_id: str,
):
kwargs = {}
if listen_to is not None:
kwargs["listen_to"] = listen_to
if topic_prefix is not None:
kwargs["topic_prefix"] = topic_prefix

hass_listen_state_mock = mocker.patch.object(Hass, "listen_state")
mqtt_listen_event_mock = mocker.patch.object(Mqtt, "listen_event")
z2m_integration = Z2MIntegration(fake_controller, kwargs)

with wrap_exetuction(error_expected=expected_id is None, exception=ValueError):
await z2m_integration.listen_changes("controller_id")

if expected_id is None:
return

if expected_id == "ha":
hass_listen_state_mock.assert_called_once_with(
fake_controller,
z2m_integration.state_callback,
"controller_id",
)
elif expected_id == "mqtt":
mqtt_listen_event_mock.assert_called_once_with(
fake_controller,
z2m_integration.event_callback,
topic=f"{topic_prefix or 'zigbee2mqtt'}/controller_id",
namespace="mqtt",
)
else:
assert False, "expected_id cannot be other than 'ha' or 'mqtt'"

0 comments on commit a3b565b

Please sign in to comment.