1
0
mirror of https://github.com/home-assistant/core.git synced 2025-12-24 21:06:19 +00:00

deCONZ rewrite sensor tests (#26679)

* Improve binary sensor tests

* Fix sensor tests

* Improve readability of binary sensor

* Fix climate tests
Fix sensor platform not loading climate devices as sensors

* Add test to verify adding new sensor after start up
This commit is contained in:
Robert Svensson
2019-09-18 19:07:32 +02:00
committed by Paulus Schoutsen
parent ce42b46ccd
commit 886d8bd6e2
4 changed files with 527 additions and 310 deletions

View File

@@ -1,23 +1,18 @@
"""deCONZ climate platform tests."""
from copy import deepcopy
from unittest.mock import Mock, patch
import asynctest
from asynctest import patch
from homeassistant import config_entries
from homeassistant.components import deconz
from homeassistant.helpers.dispatcher import async_dispatcher_send
from homeassistant.setup import async_setup_component
import homeassistant.components.climate as climate
from tests.common import mock_coro
SENSOR = {
SENSORS = {
"1": {
"id": "Climate 1 id",
"name": "Climate 1 name",
"id": "Thermostat id",
"name": "Thermostat",
"type": "ZHAThermostat",
"state": {"on": True, "temperature": 2260, "valve": 30},
"config": {
@@ -30,62 +25,66 @@ SENSOR = {
"uniqueid": "00:00:00:00:00:00:00:00-00",
},
"2": {
"id": "Sensor 2 id",
"name": "Sensor 2 name",
"id": "Presence sensor id",
"name": "Presence sensor",
"type": "ZHAPresence",
"state": {"presence": False},
"config": {},
"config": {"reachable": True},
"uniqueid": "00:00:00:00:00:00:00:01-00",
},
"3": {
"id": "CLIP thermostat id",
"name": "CLIP thermostat",
"type": "CLIPThermostat",
"state": {"on": True, "temperature": 2260, "valve": 30},
"config": {"reachable": True},
"uniqueid": "00:00:00:00:00:00:00:02-00",
},
}
BRIDGEID = "0123456789"
ENTRY_CONFIG = {
deconz.config_flow.CONF_API_KEY: "ABCDEF",
deconz.config_flow.CONF_BRIDGEID: "0123456789",
deconz.config_flow.CONF_BRIDGEID: BRIDGEID,
deconz.config_flow.CONF_HOST: "1.2.3.4",
deconz.config_flow.CONF_PORT: 80,
}
ENTRY_OPTIONS = {
deconz.const.CONF_ALLOW_CLIP_SENSOR: True,
deconz.const.CONF_ALLOW_DECONZ_GROUPS: True,
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_gateway(hass, data, allow_clip_sensor=True):
"""Load the deCONZ sensor platform."""
from pydeconz import DeconzSession
response = Mock(
status=200, json=asynctest.CoroutineMock(), text=asynctest.CoroutineMock()
)
response.content_type = "application/json"
session = Mock(put=asynctest.CoroutineMock(return_value=response))
ENTRY_OPTIONS[deconz.const.CONF_ALLOW_CLIP_SENSOR] = allow_clip_sensor
async def setup_deconz_integration(hass, config, options, get_state_response):
"""Create the deCONZ gateway."""
config_entry = config_entries.ConfigEntry(
1,
deconz.DOMAIN,
"Mock Title",
ENTRY_CONFIG,
"test",
config_entries.CONN_CLASS_LOCAL_PUSH,
version=1,
domain=deconz.DOMAIN,
title="Mock Title",
data=config,
source="test",
connection_class=config_entries.CONN_CLASS_LOCAL_PUSH,
system_options={},
options=ENTRY_OPTIONS,
options=options,
entry_id="1",
)
gateway = deconz.DeconzGateway(hass, config_entry)
gateway.api = DeconzSession(hass.loop, session, **config_entry.data)
gateway.api.config = Mock()
hass.data[deconz.DOMAIN] = {gateway.bridgeid: gateway}
with patch("pydeconz.DeconzSession.async_get_state", return_value=mock_coro(data)):
await gateway.api.async_load_parameters()
await hass.config_entries.async_forward_entry_setup(config_entry, "climate")
# To flush out the service call to update the group
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()
return gateway
hass.config_entries._entries.append(config_entry)
return hass.data[deconz.DOMAIN][config[deconz.CONF_BRIDGEID]]
async def test_platform_manually_configured(hass):
@@ -101,69 +100,155 @@ async def test_platform_manually_configured(hass):
async def test_no_sensors(hass):
"""Test that no sensors in deconz results in no climate entities."""
gateway = await setup_gateway(hass, {})
assert not hass.data[deconz.DOMAIN][gateway.bridgeid].deconz_ids
assert not hass.states.async_all()
data = deepcopy(DECONZ_WEB_REQUEST)
gateway = await setup_deconz_integration(
hass, ENTRY_CONFIG, options={}, get_state_response=data
)
assert len(gateway.deconz_ids) == 0
assert len(hass.states.async_all()) == 0
async def test_climate_devices(hass):
"""Test successful creation of sensor entities."""
gateway = await setup_gateway(hass, {"sensors": deepcopy(SENSOR)})
assert "climate.climate_1_name" in gateway.deconz_ids
assert "sensor.sensor_2_name" not in gateway.deconz_ids
assert len(hass.states.async_all()) == 1
data = deepcopy(DECONZ_WEB_REQUEST)
data["sensors"] = deepcopy(SENSORS)
gateway = await setup_deconz_integration(
hass, ENTRY_CONFIG, options={}, get_state_response=data
)
assert "climate.thermostat" in gateway.deconz_ids
assert "sensor.thermostat" not in gateway.deconz_ids
assert "sensor.thermostat_battery_level" in gateway.deconz_ids
assert "climate.presence_sensor" not in gateway.deconz_ids
assert "climate.clip_thermostat" not in gateway.deconz_ids
assert len(hass.states.async_all()) == 3
gateway.api.sensors["1"].async_update({"state": {"on": False}})
thermostat = hass.states.get("climate.thermostat")
assert thermostat.state == "auto"
await hass.services.async_call(
"climate",
"set_hvac_mode",
{"entity_id": "climate.climate_1_name", "hvac_mode": "auto"},
blocking=True,
)
gateway.api.session.put.assert_called_with(
"http://1.2.3.4:80/api/ABCDEF/sensors/1/config", data='{"mode": "auto"}'
)
thermostat = hass.states.get("sensor.thermostat")
assert thermostat is None
await hass.services.async_call(
"climate",
"set_hvac_mode",
{"entity_id": "climate.climate_1_name", "hvac_mode": "heat"},
blocking=True,
)
gateway.api.session.put.assert_called_with(
"http://1.2.3.4:80/api/ABCDEF/sensors/1/config", data='{"mode": "heat"}'
)
thermostat_battery_level = hass.states.get("sensor.thermostat_battery_level")
assert thermostat_battery_level.state == "100"
await hass.services.async_call(
"climate",
"set_hvac_mode",
{"entity_id": "climate.climate_1_name", "hvac_mode": "off"},
blocking=True,
)
gateway.api.session.put.assert_called_with(
"http://1.2.3.4:80/api/ABCDEF/sensors/1/config", data='{"mode": "off"}'
)
presence_sensor = hass.states.get("climate.presence_sensor")
assert presence_sensor is None
await hass.services.async_call(
"climate",
"set_temperature",
{"entity_id": "climate.climate_1_name", "temperature": 20},
blocking=True,
)
gateway.api.session.put.assert_called_with(
"http://1.2.3.4:80/api/ABCDEF/sensors/1/config", data='{"heatsetpoint": 2000.0}'
)
clip_thermostat = hass.states.get("climate.clip_thermostat")
assert clip_thermostat is None
assert len(gateway.api.session.put.mock_calls) == 4
thermostat_device = gateway.api.sensors["1"]
thermostat_device.async_update({"config": {"mode": "off"}})
await hass.async_block_till_done()
thermostat = hass.states.get("climate.thermostat")
assert thermostat.state == "off"
thermostat_device.async_update({"config": {"mode": "other"}, "state": {"on": True}})
await hass.async_block_till_done()
thermostat = hass.states.get("climate.thermostat")
assert thermostat.state == "heat"
thermostat_device.async_update({"state": {"on": False}})
await hass.async_block_till_done()
thermostat = hass.states.get("climate.thermostat")
assert thermostat.state == "off"
# Verify service calls
with patch.object(
thermostat_device, "_async_set_callback", return_value=True
) as set_callback:
await hass.services.async_call(
climate.DOMAIN,
climate.SERVICE_SET_HVAC_MODE,
{"entity_id": "climate.thermostat", "hvac_mode": "auto"},
blocking=True,
)
await hass.async_block_till_done()
set_callback.assert_called_with("/sensors/1/config", {"mode": "auto"})
with patch.object(
thermostat_device, "_async_set_callback", return_value=True
) as set_callback:
await hass.services.async_call(
climate.DOMAIN,
climate.SERVICE_SET_HVAC_MODE,
{"entity_id": "climate.thermostat", "hvac_mode": "heat"},
blocking=True,
)
await hass.async_block_till_done()
set_callback.assert_called_with("/sensors/1/config", {"mode": "heat"})
with patch.object(
thermostat_device, "_async_set_callback", return_value=True
) as set_callback:
await hass.services.async_call(
climate.DOMAIN,
climate.SERVICE_SET_HVAC_MODE,
{"entity_id": "climate.thermostat", "hvac_mode": "off"},
blocking=True,
)
set_callback.assert_called_with("/sensors/1/config", {"mode": "off"})
with patch.object(
thermostat_device, "_async_set_callback", return_value=True
) as set_callback:
await hass.services.async_call(
climate.DOMAIN,
climate.SERVICE_SET_TEMPERATURE,
{"entity_id": "climate.thermostat", "temperature": 20},
blocking=True,
)
set_callback.assert_called_with("/sensors/1/config", {"heatsetpoint": 2000.0})
async def test_clip_climate_device(hass):
"""Test successful creation of sensor entities."""
data = deepcopy(DECONZ_WEB_REQUEST)
data["sensors"] = deepcopy(SENSORS)
gateway = await setup_deconz_integration(
hass,
ENTRY_CONFIG,
options={deconz.gateway.CONF_ALLOW_CLIP_SENSOR: True},
get_state_response=data,
)
assert "climate.thermostat" in gateway.deconz_ids
assert "sensor.thermostat" not in gateway.deconz_ids
assert "sensor.thermostat_battery_level" in gateway.deconz_ids
assert "climate.presence_sensor" not in gateway.deconz_ids
assert "climate.clip_thermostat" in gateway.deconz_ids
assert len(hass.states.async_all()) == 4
thermostat = hass.states.get("climate.thermostat")
assert thermostat.state == "auto"
thermostat = hass.states.get("sensor.thermostat")
assert thermostat is None
thermostat_battery_level = hass.states.get("sensor.thermostat_battery_level")
assert thermostat_battery_level.state == "100"
presence_sensor = hass.states.get("climate.presence_sensor")
assert presence_sensor is None
clip_thermostat = hass.states.get("climate.clip_thermostat")
assert clip_thermostat.state == "heat"
async def test_verify_state_update(hass):
"""Test that state update properly."""
gateway = await setup_gateway(hass, {"sensors": deepcopy(SENSOR)})
assert "climate.climate_1_name" in gateway.deconz_ids
data = deepcopy(DECONZ_WEB_REQUEST)
data["sensors"] = deepcopy(SENSORS)
gateway = await setup_deconz_integration(
hass, ENTRY_CONFIG, options={}, get_state_response=data
)
assert "climate.thermostat" in gateway.deconz_ids
thermostat = hass.states.get("climate.climate_1_name")
thermostat = hass.states.get("climate.thermostat")
assert thermostat.state == "auto"
state_update = {
@@ -174,44 +259,32 @@ async def test_verify_state_update(hass):
"state": {"on": False},
}
gateway.api.async_event_handler(state_update)
await hass.async_block_till_done()
assert len(hass.states.async_all()) == 1
thermostat = hass.states.get("climate.climate_1_name")
thermostat = hass.states.get("climate.thermostat")
assert thermostat.state == "auto"
assert gateway.api.sensors["1"].changed_keys == {"state", "r", "t", "on", "e", "id"}
async def test_add_new_climate_device(hass):
"""Test successful creation of climate entities."""
gateway = await setup_gateway(hass, {})
sensor = Mock()
sensor.name = "name"
sensor.type = "ZHAThermostat"
sensor.uniqueid = "1"
sensor.register_async_callback = Mock()
async_dispatcher_send(hass, gateway.async_signal_new_device("sensor"), [sensor])
await hass.async_block_till_done()
assert "climate.name" in gateway.deconz_ids
async def test_do_not_allow_clipsensor(hass):
"""Test that clip sensors can be ignored."""
gateway = await setup_gateway(hass, {}, allow_clip_sensor=False)
sensor = Mock()
sensor.name = "name"
sensor.type = "CLIPThermostat"
sensor.register_async_callback = Mock()
async_dispatcher_send(hass, gateway.async_signal_new_device("sensor"), [sensor])
await hass.async_block_till_done()
"""Test that adding a new climate device works."""
data = deepcopy(DECONZ_WEB_REQUEST)
gateway = await setup_deconz_integration(
hass, ENTRY_CONFIG, options={}, get_state_response=data
)
assert len(gateway.deconz_ids) == 0
state_added = {
"t": "event",
"e": "added",
"r": "sensors",
"id": "1",
"sensor": deepcopy(SENSORS["1"]),
}
gateway.api.async_event_handler(state_added)
await hass.async_block_till_done()
async def test_unload_sensor(hass):
"""Test that it works to unload sensor entities."""
gateway = await setup_gateway(hass, {"sensors": SENSOR})
assert "climate.thermostat" in gateway.deconz_ids
await gateway.async_reset()
assert len(hass.states.async_all()) == 0
thermostat = hass.states.get("climate.thermostat")
assert thermostat.state == "auto"