Skip to content

Commit

Permalink
test(integration): add tests for listen_changes methods
Browse files Browse the repository at this point in the history
  • Loading branch information
xaviml committed Jul 5, 2021
1 parent a3b565b commit 1f86856
Show file tree
Hide file tree
Showing 8 changed files with 130 additions and 14 deletions.
4 changes: 4 additions & 0 deletions apps/controllerx/cx_core/integration/deconz.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ def get_default_actions_mapping(self) -> Optional[DefaultActionsMapping]:

async def listen_changes(self, controller_id: str) -> None:
listens_to = self.kwargs.get("listen_to", LISTENS_TO_ID)
if listens_to not in (LISTENS_TO_ID, LISTENS_TO_UNIQUE_ID):
raise ValueError(
"`listens_to` for deCONZ integration should either be `id` or `unique_id`"
)
await Hass.listen_event(
self.controller,
self.event_callback,
Expand Down
6 changes: 4 additions & 2 deletions apps/controllerx/cx_core/integration/lutron_caseta.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@ def get_default_actions_mapping(self) -> Optional[DefaultActionsMapping]:
async def listen_changes(self, controller_id: str) -> None:
await Hass.listen_event(
self.controller,
self.callback,
self.event_callback,
"lutron_caseta_button_event",
serial=controller_id,
)

async def callback(self, event_name: str, data: EventData, kwargs: dict) -> None:
async def event_callback(
self, event_name: str, data: EventData, kwargs: dict
) -> None:
button = data["button_number"]
action_type = data["action"]
action = f"button_{button}_{action_type}"
Expand Down
6 changes: 4 additions & 2 deletions apps/controllerx/cx_core/integration/zha.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def get_default_actions_mapping(self) -> Optional[DefaultActionsMapping]:

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

def get_action(self, data: EventData) -> str:
Expand All @@ -28,7 +28,9 @@ def get_action(self, data: EventData) -> str:
action += "_" + "_".join(args)
return action

async def callback(self, event_name: str, data: EventData, kwargs: dict) -> None:
async def event_callback(
self, event_name: str, data: EventData, kwargs: dict
) -> None:
action = self.controller.get_zha_action(data)
if action is None:
# If there is no action extracted from the controller then
Expand Down
21 changes: 13 additions & 8 deletions tests/unit_tests/cx_core/integration/deconz_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
from cx_core.integration.deconz import DeCONZIntegration
from pytest_mock.plugin import MockerFixture

from tests.test_utils import wrap_exetuction


@pytest.mark.parametrize(
"data, type, expected",
Expand Down Expand Up @@ -45,14 +47,15 @@ async def test_callback(
("id", "id"),
("unique_id", "unique_id"),
(None, "id"),
("fake", None),
],
)
@pytest.mark.asyncio
async def test_listen_changes(
fake_controller: Controller,
mocker: MockerFixture,
listen_to: Optional[str],
expected_id: str,
expected_id: Optional[str],
):
kwargs = {}
if listen_to is not None:
Expand All @@ -61,11 +64,13 @@ async def test_listen_changes(
listen_event_mock = mocker.patch.object(Hass, "listen_event")
deconz_integration = DeCONZIntegration(fake_controller, kwargs)

await deconz_integration.listen_changes("controller_id")
with wrap_exetuction(error_expected=expected_id is None, exception=ValueError):
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"}
)
if expected_id is not None:
listen_event_mock.assert_called_once_with(
fake_controller,
deconz_integration.event_callback,
"deconz_event",
**{expected_id: "controller_id"}
)
22 changes: 21 additions & 1 deletion tests/unit_tests/cx_core/integration/lutron_test.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from typing import Dict

import pytest
from appdaemon.plugins.hass.hassapi import Hass
from cx_core.controller import Controller
from cx_core.integration.lutron_caseta import LutronIntegration
from pytest_mock.plugin import MockerFixture
Expand Down Expand Up @@ -42,5 +43,24 @@ async def test_callback(
):
handle_action_patch = mocker.patch.object(fake_controller, "handle_action")
lutron_integration = LutronIntegration(fake_controller, {})
await lutron_integration.callback("test", data, {})
await lutron_integration.event_callback("test", data, {})
handle_action_patch.assert_called_once_with(expected, extra=data)


@pytest.mark.asyncio
async def test_listen_changes(
fake_controller: Controller,
mocker: MockerFixture,
):
controller_id = "controller_id"
listen_event_mock = mocker.patch.object(Hass, "listen_event")
lutron_integration = LutronIntegration(fake_controller, {})

await lutron_integration.listen_changes(controller_id)

listen_event_mock.assert_called_once_with(
fake_controller,
lutron_integration.event_callback,
"lutron_caseta_button_event",
serial=controller_id,
)
20 changes: 20 additions & 0 deletions tests/unit_tests/cx_core/integration/mqtt_test.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from typing import Dict

import pytest
from appdaemon.plugins.mqtt.mqttapi import Mqtt
from cx_core.controller import Controller
from cx_core.integration.mqtt import MQTTIntegration
from pytest_mock.plugin import MockerFixture
Expand Down Expand Up @@ -34,3 +35,22 @@ async def test_callback(
handle_action_patch.assert_called_once_with(expected)
else:
handle_action_patch.assert_not_called()


@pytest.mark.asyncio
async def test_listen_changes(
fake_controller: Controller,
mocker: MockerFixture,
):
controller_id = "controller_id"
listen_event_mock = mocker.patch.object(Mqtt, "listen_event")
mqtt_integration = MQTTIntegration(fake_controller, {})

await mqtt_integration.listen_changes(controller_id)

listen_event_mock.assert_called_once_with(
fake_controller,
mqtt_integration.event_callback,
topic=controller_id,
namespace="mqtt",
)
43 changes: 43 additions & 0 deletions tests/unit_tests/cx_core/integration/state_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
from typing import Optional

import pytest
from appdaemon.plugins.hass.hassapi import Hass
from cx_core.controller import Controller
from cx_core.integration.state import StateIntegration
from pytest_mock.plugin import MockerFixture


@pytest.mark.parametrize("attribute", ["sensor", "entity_id", None])
@pytest.mark.asyncio
async def test_listen_changes(
fake_controller: Controller, mocker: MockerFixture, attribute: Optional[str]
):
kwargs = {}
if attribute is not None:
kwargs["attribute"] = attribute
controller_id = "controller_id"
state_event_mock = mocker.patch.object(Hass, "listen_state")
state_integration = StateIntegration(fake_controller, kwargs)

await state_integration.listen_changes(controller_id)

state_event_mock.assert_called_once_with(
fake_controller,
state_integration.state_callback,
controller_id,
attribute=attribute,
)


@pytest.mark.asyncio
async def test_callback(
fake_controller: Controller,
mocker: MockerFixture,
):

handle_action_patch = mocker.patch.object(fake_controller, "handle_action")
state_integration = StateIntegration(fake_controller, {})

await state_integration.state_callback("test", None, "old_state", "new_state", {})

handle_action_patch.assert_called_once_with("new_state")
22 changes: 21 additions & 1 deletion tests/unit_tests/cx_core/integration/zha_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.zha import ZHAIntegration
from pytest_mock.plugin import MockerFixture
Expand Down Expand Up @@ -48,9 +49,28 @@ async def test_callback(
data = {"command": command, "args": args}
handle_action_patch = mocker.patch.object(fake_controller, "handle_action")
zha_integration = ZHAIntegration(fake_controller, {})
await zha_integration.callback("test", data, {})
await zha_integration.event_callback("test", data, {})

if expected_called_with is not None:
handle_action_patch.assert_called_once_with(expected_called_with)
else:
handle_action_patch.assert_not_called()


@pytest.mark.asyncio
async def test_listen_changes(
fake_controller: Controller,
mocker: MockerFixture,
):
controller_id = "controller_id"
listen_event_mock = mocker.patch.object(Hass, "listen_event")
zha_integration = ZHAIntegration(fake_controller, {})

await zha_integration.listen_changes(controller_id)

listen_event_mock.assert_called_once_with(
fake_controller,
zha_integration.event_callback,
"zha_event",
device_ieee=controller_id,
)

0 comments on commit 1f86856

Please sign in to comment.