-
-
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.
Merge pull request #469 from xaviml/feat/shelly-integration
feat(integration): add shelly and shellyforhass integrations
- Loading branch information
Showing
17 changed files
with
274 additions
and
6 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
from cx_const import DefaultActionsMapping, Light | ||
from cx_core import LightController | ||
|
||
|
||
class ShellyI3LightController(LightController): | ||
def get_shellyforhass_actions_mapping(self) -> DefaultActionsMapping: | ||
return { | ||
"single": Light.CLICK_BRIGHTNESS_UP, | ||
"long": Light.CLICK_BRIGHTNESS_DOWN, | ||
"double": Light.ON_FULL_BRIGHTNESS, | ||
} | ||
|
||
|
||
class ShellyPlusI4LightController(LightController): | ||
def get_shelly_actions_mapping(self) -> DefaultActionsMapping: | ||
return { | ||
"single_push_1": Light.ON, | ||
"long_push_1": Light.HOLD_COLOR_UP, | ||
"btn_up_1": Light.RELEASE, | ||
"double_push_1": Light.ON_FULL_COLOR_TEMP, | ||
"single_push_2": Light.OFF, | ||
"long_push_2": Light.HOLD_COLOR_DOWN, | ||
"btn_up_2": Light.RELEASE, | ||
"double_push_2": Light.ON_MIN_COLOR_TEMP, | ||
"single_push_3": Light.CLICK_BRIGHTNESS_UP, | ||
"long_push_3": Light.HOLD_BRIGHTNESS_UP, | ||
"btn_up_3": Light.RELEASE, | ||
"double_push_3": Light.ON_FULL_BRIGHTNESS, | ||
"single_push_4": Light.CLICK_BRIGHTNESS_DOWN, | ||
"long_push_4": Light.HOLD_BRIGHTNESS_DOWN, | ||
"btn_up_4": Light.RELEASE, | ||
"double_push_4": Light.ON_MIN_BRIGHTNESS, | ||
} | ||
|
||
|
||
class Shelly25LightController(LightController): | ||
def get_shelly_actions_mapping(self) -> DefaultActionsMapping: | ||
return { | ||
"single_push_1": Light.ON, | ||
"long_push_1": Light.HOLD_BRIGHTNESS_UP, | ||
"btn_up_1": Light.RELEASE, | ||
"double_push_1": Light.ON_FULL_BRIGHTNESS, | ||
"single_push_2": Light.OFF, | ||
"long_push_2": Light.HOLD_BRIGHTNESS_DOWN, | ||
"btn_up_2": Light.RELEASE, | ||
"double_push_2": Light.ON_MIN_BRIGHTNESS, | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
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