Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

deCONZ improve gateway tests #26709

Merged
merged 6 commits into from Sep 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 2 additions & 6 deletions homeassistant/components/deconz/gateway.py
Expand Up @@ -184,11 +184,7 @@ def shutdown(self, event):
self.api.close()

async def async_reset(self):
"""Reset this gateway to default state.

Will cancel any scheduled setup retry and will unload
the config entry.
"""
"""Reset this gateway to default state."""
self.api.async_connection_status_callback = None
self.api.close()

Expand All @@ -203,7 +199,7 @@ async def async_reset(self):

for event in self.events:
event.async_will_remove_from_hass()
self.events.remove(event)
self.events.clear()

self.deconz_ids = {}
return True
Expand Down
1 change: 0 additions & 1 deletion homeassistant/components/deconz/scene.py
Expand Up @@ -9,7 +9,6 @@

async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
"""Old way of setting up deCONZ platforms."""
pass


async def async_setup_entry(hass, config_entry, async_add_entities):
Expand Down
52 changes: 5 additions & 47 deletions tests/components/deconz/test_binary_sensor.py
@@ -1,14 +1,12 @@
"""deCONZ binary sensor platform tests."""
from copy import deepcopy

from asynctest import patch

from homeassistant import config_entries
from homeassistant.components import deconz
from homeassistant.setup import async_setup_component

import homeassistant.components.binary_sensor as binary_sensor

from .test_gateway import ENTRY_CONFIG, DECONZ_WEB_REQUEST, setup_deconz_integration

SENSORS = {
"1": {
Expand Down Expand Up @@ -50,50 +48,6 @@
},
}

BRIDGEID = "0123456789"

ENTRY_CONFIG = {
deconz.config_flow.CONF_API_KEY: "ABCDEF",
deconz.config_flow.CONF_BRIDGEID: BRIDGEID,
deconz.config_flow.CONF_HOST: "1.2.3.4",
deconz.config_flow.CONF_PORT: 80,
}

DECONZ_CONFIG = {
"bridgeid": BRIDGEID,
"mac": "00:11:22:33:44:55",
"name": "deCONZ mock gateway",
"sw_version": "2.05.69",
"websocketport": 1234,
}

DECONZ_WEB_REQUEST = {"config": DECONZ_CONFIG}


async def setup_deconz_integration(hass, config, options, get_state_response):
"""Create the deCONZ gateway."""
config_entry = config_entries.ConfigEntry(
version=1,
domain=deconz.DOMAIN,
title="Mock Title",
data=config,
source="test",
connection_class=config_entries.CONN_CLASS_LOCAL_PUSH,
system_options={},
options=options,
entry_id="1",
)

with patch(
"pydeconz.DeconzSession.async_get_state", return_value=get_state_response
), patch("pydeconz.DeconzSession.start", return_value=True):
await deconz.async_setup_entry(hass, config_entry)
await hass.async_block_till_done()

hass.config_entries._entries.append(config_entry)

return hass.data[deconz.DOMAIN][config[deconz.CONF_BRIDGEID]]


async def test_platform_manually_configured(hass):
"""Test that we do not discover anything or try to set up a gateway."""
Expand Down Expand Up @@ -147,6 +101,10 @@ async def test_binary_sensors(hass):
presence_sensor = hass.states.get("binary_sensor.presence_sensor")
assert presence_sensor.state == "on"

await gateway.async_reset()

assert len(hass.states.async_all()) == 0


async def test_allow_clip_sensor(hass):
"""Test that CLIP sensors can be allowed."""
Expand Down
51 changes: 6 additions & 45 deletions tests/components/deconz/test_climate.py
Expand Up @@ -3,12 +3,13 @@

from asynctest import patch

from homeassistant import config_entries
from homeassistant.components import deconz
from homeassistant.setup import async_setup_component

import homeassistant.components.climate as climate

from .test_gateway import ENTRY_CONFIG, DECONZ_WEB_REQUEST, setup_deconz_integration

SENSORS = {
"1": {
"id": "Thermostat id",
Expand Down Expand Up @@ -42,50 +43,6 @@
},
}

BRIDGEID = "0123456789"

ENTRY_CONFIG = {
deconz.config_flow.CONF_API_KEY: "ABCDEF",
deconz.config_flow.CONF_BRIDGEID: BRIDGEID,
deconz.config_flow.CONF_HOST: "1.2.3.4",
deconz.config_flow.CONF_PORT: 80,
}

DECONZ_CONFIG = {
"bridgeid": BRIDGEID,
"mac": "00:11:22:33:44:55",
"name": "deCONZ mock gateway",
"sw_version": "2.05.69",
"websocketport": 1234,
}

DECONZ_WEB_REQUEST = {"config": DECONZ_CONFIG}


async def setup_deconz_integration(hass, config, options, get_state_response):
"""Create the deCONZ gateway."""
config_entry = config_entries.ConfigEntry(
version=1,
domain=deconz.DOMAIN,
title="Mock Title",
data=config,
source="test",
connection_class=config_entries.CONN_CLASS_LOCAL_PUSH,
system_options={},
options=options,
entry_id="1",
)

with patch(
"pydeconz.DeconzSession.async_get_state", return_value=get_state_response
), patch("pydeconz.DeconzSession.start", return_value=True):
await deconz.async_setup_entry(hass, config_entry)
await hass.async_block_till_done()

hass.config_entries._entries.append(config_entry)

return hass.data[deconz.DOMAIN][config[deconz.CONF_BRIDGEID]]


async def test_platform_manually_configured(hass):
"""Test that we do not discover anything or try to set up a gateway."""
Expand Down Expand Up @@ -205,6 +162,10 @@ async def test_climate_devices(hass):
)
set_callback.assert_called_with("/sensors/1/config", {"heatsetpoint": 2000.0})

await gateway.async_reset()

assert len(hass.states.async_all()) == 0


async def test_clip_climate_device(hass):
"""Test successful creation of sensor entities."""
Expand Down
51 changes: 6 additions & 45 deletions tests/components/deconz/test_cover.py
Expand Up @@ -3,12 +3,13 @@

from asynctest import patch

from homeassistant import config_entries
from homeassistant.components import deconz
from homeassistant.setup import async_setup_component

import homeassistant.components.cover as cover

from .test_gateway import ENTRY_CONFIG, DECONZ_WEB_REQUEST, setup_deconz_integration

COVERS = {
"1": {
"id": "Level controllable cover id",
Expand All @@ -35,50 +36,6 @@
},
}

BRIDGEID = "0123456789"

ENTRY_CONFIG = {
deconz.config_flow.CONF_API_KEY: "ABCDEF",
deconz.config_flow.CONF_BRIDGEID: BRIDGEID,
deconz.config_flow.CONF_HOST: "1.2.3.4",
deconz.config_flow.CONF_PORT: 80,
}

DECONZ_CONFIG = {
"bridgeid": BRIDGEID,
"mac": "00:11:22:33:44:55",
"name": "deCONZ mock gateway",
"sw_version": "2.05.69",
"websocketport": 1234,
}

DECONZ_WEB_REQUEST = {"config": DECONZ_CONFIG}


async def setup_deconz_integration(hass, config, options, get_state_response):
"""Create the deCONZ gateway."""
config_entry = config_entries.ConfigEntry(
version=1,
domain=deconz.DOMAIN,
title="Mock Title",
data=config,
source="test",
connection_class=config_entries.CONN_CLASS_LOCAL_PUSH,
system_options={},
options=options,
entry_id="1",
)

with patch(
"pydeconz.DeconzSession.async_get_state", return_value=get_state_response
), patch("pydeconz.DeconzSession.start", return_value=True):
await deconz.async_setup_entry(hass, config_entry)
await hass.async_block_till_done()

hass.config_entries._entries.append(config_entry)

return hass.data[deconz.DOMAIN][config[deconz.CONF_BRIDGEID]]


async def test_platform_manually_configured(hass):
"""Test that we do not discover anything or try to set up a gateway."""
Expand Down Expand Up @@ -159,3 +116,7 @@ async def test_cover(hass):
)
await hass.async_block_till_done()
set_callback.assert_called_with("/lights/1/state", {"bri_inc": 0})

await gateway.async_reset()

assert len(hass.states.async_all()) == 2
120 changes: 67 additions & 53 deletions tests/components/deconz/test_deconz_event.py
@@ -1,60 +1,74 @@
"""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()
from copy import deepcopy

from asynctest import Mock

from homeassistant.components.deconz.deconz_event import CONF_DECONZ_EVENT

from .test_gateway import ENTRY_CONFIG, DECONZ_WEB_REQUEST, setup_deconz_integration

SENSORS = {
"1": {
"id": "Switch 1 id",
"name": "Switch 1",
"type": "ZHASwitch",
"state": {"buttonevent": 1000},
"config": {},
"uniqueid": "00:00:00:00:00:00:00:01-00",
},
"2": {
"id": "Switch 2 id",
"name": "Switch 2",
"type": "ZHASwitch",
"state": {"buttonevent": 1000},
"config": {"battery": 100},
"uniqueid": "00:00:00:00:00:00:00:02-00",
},
}


async def test_deconz_events(hass):
"""Test successful creation of deconz events."""
data = deepcopy(DECONZ_WEB_REQUEST)
data["sensors"] = deepcopy(SENSORS)
gateway = await setup_deconz_integration(
hass, ENTRY_CONFIG, options={}, get_state_response=data
)
assert "sensor.switch_1" not in gateway.deconz_ids
assert "sensor.switch_1_battery_level" not in gateway.deconz_ids
assert "sensor.switch_2" not in gateway.deconz_ids
assert "sensor.switch_2_battery_level" in gateway.deconz_ids
assert len(hass.states.async_all()) == 1
assert len(gateway.events) == 2

switch_1 = hass.states.get("sensor.switch_1")
assert switch_1 is None

switch_1_battery_level = hass.states.get("sensor.switch_1_battery_level")
assert switch_1_battery_level is None

switch_2 = hass.states.get("sensor.switch_2")
assert switch_2 is None

switch_2_battery_level = hass.states.get("sensor.switch_2_battery_level")
assert switch_2_battery_level.state == "100"

mock_listener = Mock()
unsub = hass.bus.async_listen(CONF_DECONZ_EVENT, mock_listener)

gateway.api.sensors["1"].async_update({"state": {"buttonevent": 2000}})
await hass.async_block_till_done()

assert len(calls) == 1
assert len(mock_listener.mock_calls) == 1
assert mock_listener.mock_calls[0][1][0].data == {
"id": "switch_1",
"unique_id": "00:00:00:00:00:00:00:01",
"event": 2000,
}

unsub()

await gateway.async_reset()

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
assert len(hass.states.async_all()) == 0
assert len(gateway.events) == 0