Add events to Dynalite platform ()

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
pull/38616/head
Ziv 2020-08-06 21:40:54 +03:00 committed by GitHub
parent fd52ff531d
commit ae40f87a5c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 100 additions and 5 deletions

View File

@ -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."""

View File

@ -49,3 +49,8 @@ DEFAULT_TEMPLATES = {
CONF_TILT_TIME,
],
}
ATTR_AREA = "area"
ATTR_HOST = "host"
ATTR_PACKET = "packet"
ATTR_PRESET = "preset"

View File

@ -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"]
}

View File

@ -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

View File

@ -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

View File

@ -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