From ae40f87a5c319ec8171c6cd565eb830423b05dd4 Mon Sep 17 00:00:00 2001 From: Ziv <16467659+ziv1234@users.noreply.github.com> Date: Thu, 6 Aug 2020 21:40:54 +0300 Subject: [PATCH] Add events to Dynalite platform (#38583) Co-authored-by: Martin Hjelmare --- homeassistant/components/dynalite/bridge.py | 41 +++++++++++++- homeassistant/components/dynalite/const.py | 5 ++ .../components/dynalite/manifest.json | 2 +- requirements_all.txt | 2 +- requirements_test_all.txt | 2 +- tests/components/dynalite/test_bridge.py | 53 +++++++++++++++++++ 6 files changed, 100 insertions(+), 5 deletions(-) diff --git a/homeassistant/components/dynalite/bridge.py b/homeassistant/components/dynalite/bridge.py index 522061a85aa..5bf21801b3d 100644 --- a/homeassistant/components/dynalite/bridge.py +++ b/homeassistant/components/dynalite/bridge.py @@ -2,13 +2,28 @@ from typing import Any, Callable, Dict, List, Optional -from dynalite_devices_lib.dynalite_devices import DynaliteBaseDevice, DynaliteDevices +from dynalite_devices_lib.dynalite_devices import ( + CONF_AREA as dyn_CONF_AREA, + CONF_PRESET as dyn_CONF_PRESET, + NOTIFICATION_PACKET, + NOTIFICATION_PRESET, + DynaliteBaseDevice, + DynaliteDevices, + DynaliteNotification, +) from homeassistant.const import CONF_HOST from homeassistant.core import HomeAssistant, callback from homeassistant.helpers.dispatcher import async_dispatcher_send -from .const import ENTITY_PLATFORMS, LOGGER +from .const import ( + ATTR_AREA, + ATTR_HOST, + ATTR_PACKET, + ATTR_PRESET, + ENTITY_PLATFORMS, + LOGGER, +) from .convert_config import convert_config @@ -26,6 +41,7 @@ class DynaliteBridge: self.dynalite_devices = DynaliteDevices( new_device_func=self.add_devices_when_registered, update_device_func=self.update_device, + notification_func=self.handle_notification, ) self.dynalite_devices.configure(convert_config(config)) @@ -61,6 +77,27 @@ class DynaliteBridge: else: async_dispatcher_send(self.hass, self.update_signal(device)) + @callback + def handle_notification(self, notification: DynaliteNotification) -> None: + """Handle a notification from the platform and issue events.""" + if notification.notification == NOTIFICATION_PACKET: + self.hass.bus.async_fire( + "dynalite_packet", + { + ATTR_HOST: self.host, + ATTR_PACKET: notification.data[NOTIFICATION_PACKET], + }, + ) + if notification.notification == NOTIFICATION_PRESET: + self.hass.bus.async_fire( + "dynalite_preset", + { + ATTR_HOST: self.host, + ATTR_AREA: notification.data[dyn_CONF_AREA], + ATTR_PRESET: notification.data[dyn_CONF_PRESET], + }, + ) + @callback def register_add_devices(self, platform: str, async_add_devices: Callable) -> None: """Add an async_add_entities for a category.""" diff --git a/homeassistant/components/dynalite/const.py b/homeassistant/components/dynalite/const.py index e5a4e90d1bd..373e64a1b76 100644 --- a/homeassistant/components/dynalite/const.py +++ b/homeassistant/components/dynalite/const.py @@ -49,3 +49,8 @@ DEFAULT_TEMPLATES = { CONF_TILT_TIME, ], } + +ATTR_AREA = "area" +ATTR_HOST = "host" +ATTR_PACKET = "packet" +ATTR_PRESET = "preset" diff --git a/homeassistant/components/dynalite/manifest.json b/homeassistant/components/dynalite/manifest.json index e09410e7ef5..a8277eea85c 100644 --- a/homeassistant/components/dynalite/manifest.json +++ b/homeassistant/components/dynalite/manifest.json @@ -4,5 +4,5 @@ "config_flow": true, "documentation": "https://www.home-assistant.io/integrations/dynalite", "codeowners": ["@ziv1234"], - "requirements": ["dynalite_devices==0.1.41"] + "requirements": ["dynalite_devices==0.1.44"] } diff --git a/requirements_all.txt b/requirements_all.txt index 980730ad845..eb468e2cdf8 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -503,7 +503,7 @@ dsmr_parser==0.18 dweepy==0.3.0 # homeassistant.components.dynalite -dynalite_devices==0.1.41 +dynalite_devices==0.1.44 # homeassistant.components.rainforest_eagle eagle200_reader==0.2.4 diff --git a/requirements_test_all.txt b/requirements_test_all.txt index 4736d5d0046..597742a0d6d 100644 --- a/requirements_test_all.txt +++ b/requirements_test_all.txt @@ -258,7 +258,7 @@ doorbirdpy==2.0.8 dsmr_parser==0.18 # homeassistant.components.dynalite -dynalite_devices==0.1.41 +dynalite_devices==0.1.44 # homeassistant.components.ee_brightbox eebrightbox==0.0.4 diff --git a/tests/components/dynalite/test_bridge.py b/tests/components/dynalite/test_bridge.py index ea73f75a390..8f3210bbcf8 100644 --- a/tests/components/dynalite/test_bridge.py +++ b/tests/components/dynalite/test_bridge.py @@ -1,6 +1,21 @@ """Test Dynalite bridge.""" + +from dynalite_devices_lib.dynalite_devices import ( + CONF_AREA as dyn_CONF_AREA, + CONF_PRESET as dyn_CONF_PRESET, + NOTIFICATION_PACKET, + NOTIFICATION_PRESET, + DynaliteNotification, +) + from homeassistant.components import dynalite +from homeassistant.components.dynalite.const import ( + ATTR_AREA, + ATTR_HOST, + ATTR_PACKET, + ATTR_PRESET, +) from homeassistant.helpers.dispatcher import async_dispatcher_connect from tests.async_mock import AsyncMock, Mock, patch @@ -95,3 +110,41 @@ async def test_register_then_add_devices(hass): await hass.async_block_till_done() assert hass.states.get("light.name") assert hass.states.get("switch.name2") + + +async def test_notifications(hass): + """Test that update works.""" + host = "1.2.3.4" + entry = MockConfigEntry(domain=dynalite.DOMAIN, data={dynalite.CONF_HOST: host}) + entry.add_to_hass(hass) + with patch( + "homeassistant.components.dynalite.bridge.DynaliteDevices" + ) as mock_dyn_dev: + mock_dyn_dev().async_setup = AsyncMock(return_value=True) + assert await hass.config_entries.async_setup(entry.entry_id) + await hass.async_block_till_done() + notification_func = mock_dyn_dev.mock_calls[1][2]["notification_func"] + event_listener1 = Mock() + hass.bus.async_listen("dynalite_packet", event_listener1) + packet = [5, 4, 3, 2] + notification_func( + DynaliteNotification(NOTIFICATION_PACKET, {NOTIFICATION_PACKET: packet}) + ) + await hass.async_block_till_done() + event_listener1.assert_called_once() + my_event = event_listener1.mock_calls[0][1][0] + assert my_event.data[ATTR_HOST] == host + assert my_event.data[ATTR_PACKET] == packet + event_listener2 = Mock() + hass.bus.async_listen("dynalite_preset", event_listener2) + notification_func( + DynaliteNotification( + NOTIFICATION_PRESET, {dyn_CONF_AREA: 7, dyn_CONF_PRESET: 2} + ) + ) + await hass.async_block_till_done() + event_listener2.assert_called_once() + my_event = event_listener2.mock_calls[0][1][0] + assert my_event.data[ATTR_HOST] == host + assert my_event.data[ATTR_AREA] == 7 + assert my_event.data[ATTR_PRESET] == 2