Navigation Menu

Skip to content

Commit

Permalink
deCONZ - create deconz_events through sensor platform (#26592)
Browse files Browse the repository at this point in the history
* Move event creation into sensor platform where it belongs
* Fixed the weird failing test observed during device automation PR
  • Loading branch information
Kane610 committed Sep 13, 2019
1 parent 357f242 commit fb1acfc
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 90 deletions.
23 changes: 1 addition & 22 deletions homeassistant/components/deconz/gateway.py
Expand Up @@ -3,7 +3,6 @@
import async_timeout

from pydeconz import DeconzSession, errors
from pydeconz.sensor import Switch

from homeassistant.exceptions import ConfigEntryNotReady
from homeassistant.const import CONF_HOST
Expand All @@ -29,10 +28,9 @@
DEFAULT_ALLOW_DECONZ_GROUPS,
DOMAIN,
NEW_DEVICE,
NEW_SENSOR,
SUPPORTED_PLATFORMS,
)
from .deconz_event import DeconzEvent

from .errors import AuthenticationRequired, CannotConnect


Expand Down Expand Up @@ -119,14 +117,6 @@ async def async_setup(self):
)
)

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)
Expand Down Expand Up @@ -185,17 +175,6 @@ def async_add_device_callback(self, device_type, device):
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.
Expand Down
9 changes: 9 additions & 0 deletions homeassistant/components/deconz/sensor.py
Expand Up @@ -13,6 +13,7 @@

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"
Expand Down Expand Up @@ -42,6 +43,14 @@ def async_add_sensor(sensors):
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))

Expand Down
60 changes: 60 additions & 0 deletions tests/components/deconz/test_deconz_event.py
@@ -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
68 changes: 0 additions & 68 deletions tests/components/deconz/test_gateway.py
Expand Up @@ -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()
Expand Down Expand Up @@ -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

0 comments on commit fb1acfc

Please sign in to comment.