-
-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(integration): add shelly and shellyforhass integrations
related to #441
- Loading branch information
Showing
9 changed files
with
200 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
from typing import Any, Dict, Optional | ||
|
||
from appdaemon.plugins.hass.hassapi import Hass | ||
from cx_const import DefaultActionsMapping | ||
from cx_core.integration import EventData, Integration | ||
|
||
|
||
class ShellyIntegration(Integration): | ||
name = "shelly" | ||
|
||
def get_default_actions_mapping(self) -> Optional[DefaultActionsMapping]: | ||
return self.controller.get_shelly_actions_mapping() | ||
|
||
async def listen_changes(self, controller_id: str) -> None: | ||
await Hass.listen_event( | ||
self.controller, self.event_callback, "shelly.click", device=controller_id | ||
) | ||
|
||
async def event_callback( | ||
self, event_name: str, data: EventData, kwargs: Dict[str, Any] | ||
) -> None: | ||
click_type = data["click_type"] | ||
channel = data["channel"] | ||
action = f"{click_type}_{channel}" | ||
await self.controller.handle_action(action, extra=data) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
from typing import Any, Dict, Optional | ||
|
||
from appdaemon.plugins.hass.hassapi import Hass | ||
from cx_const import DefaultActionsMapping | ||
from cx_core.integration import EventData, Integration | ||
|
||
|
||
class ShellyForHASSIntegration(Integration): | ||
name = "shellyforhass" | ||
|
||
def get_default_actions_mapping(self) -> Optional[DefaultActionsMapping]: | ||
return self.controller.get_shellyforhass_actions_mapping() | ||
|
||
async def listen_changes(self, controller_id: str) -> None: | ||
await Hass.listen_event( | ||
self.controller, | ||
self.event_callback, | ||
"shellyforhass.click", | ||
entity_id=controller_id, | ||
) | ||
|
||
async def event_callback( | ||
self, event_name: str, data: EventData, kwargs: Dict[str, Any] | ||
) -> None: | ||
click_type = data["click_type"] | ||
action = f"{click_type}" | ||
await self.controller.handle_action(action, extra=data) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
from typing import Any, Dict | ||
|
||
import pytest | ||
from appdaemon.plugins.hass.hassapi import Hass | ||
from cx_core.controller import Controller | ||
from cx_core.integration.shelly import ShellyIntegration | ||
from pytest_mock.plugin import MockerFixture | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"data, expected", | ||
[ | ||
( | ||
{ | ||
"device_id": "e09c64a22553484d804353ef97f6fcd6", | ||
"device": "shellybutton1-A4C12A45174", | ||
"channel": 1, | ||
"click_type": "single", | ||
"generation": 1, | ||
}, | ||
"single_1", | ||
), | ||
( | ||
{ | ||
"device_id": "e09d64a22553384d8043532f97f6fcd6", | ||
"device": "shellybutton1-A4C13B45274", | ||
"channel": 3, | ||
"click_type": "btn_down", | ||
"generation": 1, | ||
}, | ||
"btn_down_3", | ||
), | ||
], | ||
) | ||
async def test_callback( | ||
fake_controller: Controller, | ||
mocker: MockerFixture, | ||
data: Dict[str, Any], | ||
expected: str, | ||
) -> None: | ||
handle_action_patch = mocker.patch.object(fake_controller, "handle_action") | ||
shelly_integration = ShellyIntegration(fake_controller, {}) | ||
await shelly_integration.event_callback("test", data, {}) | ||
handle_action_patch.assert_called_once_with(expected, extra=data) | ||
|
||
|
||
async def test_listen_changes( | ||
fake_controller: Controller, | ||
mocker: MockerFixture, | ||
) -> None: | ||
controller_id = "controller_id" | ||
listen_event_mock = mocker.patch.object(Hass, "listen_event") | ||
shelly_integration = ShellyIntegration(fake_controller, {}) | ||
|
||
await shelly_integration.listen_changes(controller_id) | ||
|
||
listen_event_mock.assert_called_once_with( | ||
fake_controller, | ||
shelly_integration.event_callback, | ||
"shelly.click", | ||
device=controller_id, | ||
) |
56 changes: 56 additions & 0 deletions
56
tests/unit_tests/cx_core/integration/shellyforhass_test.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
from typing import Any, Dict | ||
|
||
import pytest | ||
from appdaemon.plugins.hass.hassapi import Hass | ||
from cx_core.controller import Controller | ||
from cx_core.integration.shellyforhass import ShellyForHASSIntegration | ||
from pytest_mock.plugin import MockerFixture | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"data, expected", | ||
[ | ||
( | ||
{ | ||
"entity_id": "binary_sensor.shelly_shbtn_1_xxxxxx_switch", | ||
"click_type": "single", | ||
}, | ||
"single", | ||
), | ||
( | ||
{ | ||
"entity_id": "binary_sensor.shelly_shbtn_1_xxxxxx_switch", | ||
"click_type": "double", | ||
}, | ||
"double", | ||
), | ||
], | ||
) | ||
async def test_callback( | ||
fake_controller: Controller, | ||
mocker: MockerFixture, | ||
data: Dict[str, Any], | ||
expected: str, | ||
) -> None: | ||
handle_action_patch = mocker.patch.object(fake_controller, "handle_action") | ||
shellyforhass_integration = ShellyForHASSIntegration(fake_controller, {}) | ||
await shellyforhass_integration.event_callback("test", data, {}) | ||
handle_action_patch.assert_called_once_with(expected, extra=data) | ||
|
||
|
||
async def test_listen_changes( | ||
fake_controller: Controller, | ||
mocker: MockerFixture, | ||
) -> None: | ||
controller_id = "controller_id" | ||
listen_event_mock = mocker.patch.object(Hass, "listen_event") | ||
shellyforhass_integration = ShellyForHASSIntegration(fake_controller, {}) | ||
|
||
await shellyforhass_integration.listen_changes(controller_id) | ||
|
||
listen_event_mock.assert_called_once_with( | ||
fake_controller, | ||
shellyforhass_integration.event_callback, | ||
"shellyforhass.click", | ||
entity_id=controller_id, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters