deCONZ - create deconz_events through sensor platform (#26592)

* Move event creation into sensor platform where it belongs
* Fixed the weird failing test observed during device automation PR
pull/26642/head
Robert Svensson 2019-09-14 00:16:37 +02:00 committed by GitHub
parent 357f2421c8
commit fb1acfccc9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 70 additions and 90 deletions

View File

@ -3,7 +3,6 @@ import asyncio
import async_timeout
from pydeconz import DeconzSession, errors
from pydeconz.sensor import Switch
from homeassistant.exceptions import ConfigEntryNotReady
from homeassistant.const import CONF_HOST
@ -29,10 +28,9 @@ from .const import (
DEFAULT_ALLOW_DECONZ_GROUPS,
DOMAIN,
NEW_DEVICE,
NEW_SENSOR,
SUPPORTED_PLATFORMS,
)
from .deconz_event import DeconzEvent
from .errors import AuthenticationRequired, CannotConnect
@ -119,14 +117,6 @@ class DeconzGateway:
)
)
self.listeners.append(
async_dispatcher_connect(
hass, self.async_signal_new_device(NEW_SENSOR), self.async_add_remote
)
)
self.async_add_remote(self.api.sensors.values())
self.api.start()
self.config_entry.add_update_listener(self.async_new_address)
@ -185,17 +175,6 @@ class DeconzGateway:
self.hass, self.async_signal_new_device(device_type), device
)
@callback
def async_add_remote(self, sensors):
"""Set up remote from deCONZ."""
for sensor in sensors:
if sensor.type in Switch.ZHATYPE and not (
not self.option_allow_clip_sensor and sensor.type.startswith("CLIP")
):
event = DeconzEvent(sensor, self)
self.hass.async_create_task(event.async_update_device_registry())
self.events.append(event)
@callback
def shutdown(self, event):
"""Wrap the call to deconz.close.

View File

@ -13,6 +13,7 @@ from homeassistant.util import slugify
from .const import ATTR_DARK, ATTR_ON, NEW_SENSOR
from .deconz_device import DeconzDevice
from .deconz_event import DeconzEvent
from .gateway import get_gateway_from_config_entry, DeconzEntityHandler
ATTR_CURRENT = "current"
@ -42,6 +43,14 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
if not sensor.BINARY:
if sensor.type in Switch.ZHATYPE:
if gateway.option_allow_clip_sensor or not sensor.type.startswith(
"CLIP"
):
event = DeconzEvent(sensor, gateway)
hass.async_create_task(event.async_update_device_registry())
gateway.events.append(event)
if sensor.battery:
entities.append(DeconzBattery(sensor, gateway))

View File

@ -0,0 +1,60 @@
"""Test deCONZ remote events."""
from unittest.mock import Mock
from homeassistant.components.deconz.deconz_event import CONF_DECONZ_EVENT, DeconzEvent
from homeassistant.core import callback
async def test_create_event(hass):
"""Successfully created a deCONZ event."""
mock_remote = Mock()
mock_remote.name = "Name"
mock_gateway = Mock()
mock_gateway.hass = hass
event = DeconzEvent(mock_remote, mock_gateway)
assert event.event_id == "name"
async def test_update_event(hass):
"""Successfully update a deCONZ event."""
mock_remote = Mock()
mock_remote.name = "Name"
mock_gateway = Mock()
mock_gateway.hass = hass
event = DeconzEvent(mock_remote, mock_gateway)
mock_remote.changed_keys = {"state": True}
calls = []
@callback
def listener(event):
"""Mock listener."""
calls.append(event)
unsub = hass.bus.async_listen(CONF_DECONZ_EVENT, listener)
event.async_update_callback()
await hass.async_block_till_done()
assert len(calls) == 1
unsub()
async def test_remove_event(hass):
"""Successfully update a deCONZ event."""
mock_remote = Mock()
mock_remote.name = "Name"
mock_gateway = Mock()
mock_gateway.hass = hass
event = DeconzEvent(mock_remote, mock_gateway)
event.async_will_remove_from_hass()
assert event._device is None

View File

@ -126,24 +126,6 @@ async def test_add_device(hass):
assert len(mock_dispatch_send.mock_calls[0]) == 3
@pytest.mark.skip(reason="fails for unkown reason, will refactor in a separate PR")
async def test_add_remote(hass):
"""Successful add remote."""
entry = Mock()
entry.data = ENTRY_CONFIG
remote = Mock()
remote.name = "name"
remote.type = "ZHASwitch"
remote.register_async_callback = Mock()
deconz_gateway = gateway.DeconzGateway(hass, entry)
deconz_gateway.async_add_remote([remote])
await hass.async_block_till_done()
assert len(deconz_gateway.events) == 1
async def test_shutdown():
"""Successful shutdown."""
hass = Mock()
@ -218,53 +200,3 @@ async def test_get_gateway_fails_cannot_connect(hass):
side_effect=pydeconz.errors.RequestError,
), pytest.raises(errors.CannotConnect):
assert await gateway.get_gateway(hass, ENTRY_CONFIG, Mock(), Mock()) is False
@pytest.mark.skip(reason="fails for unkown reason, will refactor in a separate PR")
async def test_create_event(hass):
"""Successfully created a deCONZ event."""
mock_remote = Mock()
mock_remote.name = "Name"
mock_gateway = Mock()
mock_gateway.hass = hass
event = gateway.DeconzEvent(mock_remote, mock_gateway)
await hass.async_block_till_done()
assert event.event_id == "name"
@pytest.mark.skip(reason="fails for unkown reason, will refactor in a separate PR")
async def test_update_event(hass):
"""Successfully update a deCONZ event."""
hass.bus.async_fire = Mock()
mock_remote = Mock()
mock_remote.name = "Name"
mock_gateway = Mock()
mock_gateway.hass = hass
event = gateway.DeconzEvent(mock_remote, mock_gateway)
await hass.async_block_till_done()
mock_remote.changed_keys = {"state": True}
event.async_update_callback()
assert len(hass.bus.async_fire.mock_calls) == 1
@pytest.mark.skip(reason="fails for unkown reason, will refactor in a separate PR")
async def test_remove_event(hass):
"""Successfully update a deCONZ event."""
mock_remote = Mock()
mock_remote.name = "Name"
mock_gateway = Mock()
mock_gateway.hass = hass
event = gateway.DeconzEvent(mock_remote, mock_gateway)
await hass.async_block_till_done()
event.async_will_remove_from_hass()
assert event._device is None