2019-02-18 17:43:22 +01:00
|
|
|
"""deCONZ climate platform tests."""
|
2019-05-27 06:56:00 +02:00
|
|
|
from copy import deepcopy
|
2019-02-18 17:43:22 +01:00
|
|
|
from unittest.mock import Mock, patch
|
|
|
|
|
2019-03-01 23:09:31 -08:00
|
|
|
import asynctest
|
|
|
|
|
2019-02-18 17:43:22 +01:00
|
|
|
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 = {
|
|
|
|
"1": {
|
|
|
|
"id": "Climate 1 id",
|
|
|
|
"name": "Climate 1 name",
|
|
|
|
"type": "ZHAThermostat",
|
2019-05-27 06:56:00 +02:00
|
|
|
"state": {"on": True, "temperature": 2260, "valve": 30},
|
2019-07-31 12:25:30 -07:00
|
|
|
"config": {
|
|
|
|
"battery": 100,
|
|
|
|
"heatsetpoint": 2200,
|
|
|
|
"mode": "auto",
|
|
|
|
"offset": 10,
|
|
|
|
"reachable": True,
|
|
|
|
},
|
|
|
|
"uniqueid": "00:00:00:00:00:00:00:00-00",
|
2019-02-18 17:43:22 +01:00
|
|
|
},
|
|
|
|
"2": {
|
|
|
|
"id": "Sensor 2 id",
|
|
|
|
"name": "Sensor 2 name",
|
|
|
|
"type": "ZHAPresence",
|
|
|
|
"state": {"presence": False},
|
2019-07-31 12:25:30 -07:00
|
|
|
"config": {},
|
|
|
|
},
|
2019-02-18 17:43:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ENTRY_CONFIG = {
|
|
|
|
deconz.config_flow.CONF_API_KEY: "ABCDEF",
|
|
|
|
deconz.config_flow.CONF_BRIDGEID: "0123456789",
|
|
|
|
deconz.config_flow.CONF_HOST: "1.2.3.4",
|
2019-07-31 12:25:30 -07:00
|
|
|
deconz.config_flow.CONF_PORT: 80,
|
2019-02-18 17:43:22 +01:00
|
|
|
}
|
|
|
|
|
2019-08-09 20:31:58 +03:00
|
|
|
ENTRY_OPTIONS = {
|
|
|
|
deconz.const.CONF_ALLOW_CLIP_SENSOR: True,
|
|
|
|
deconz.const.CONF_ALLOW_DECONZ_GROUPS: True,
|
|
|
|
}
|
|
|
|
|
2019-02-18 17:43:22 +01:00
|
|
|
|
|
|
|
async def setup_gateway(hass, data, allow_clip_sensor=True):
|
|
|
|
"""Load the deCONZ sensor platform."""
|
|
|
|
from pydeconz import DeconzSession
|
2019-03-01 23:09:31 -08:00
|
|
|
|
2019-03-24 19:27:32 +01:00
|
|
|
response = Mock(
|
2019-07-31 12:25:30 -07:00
|
|
|
status=200, json=asynctest.CoroutineMock(), text=asynctest.CoroutineMock()
|
2019-03-01 23:09:31 -08:00
|
|
|
)
|
2019-07-31 12:25:30 -07:00
|
|
|
response.content_type = "application/json"
|
|
|
|
|
|
|
|
session = Mock(put=asynctest.CoroutineMock(return_value=response))
|
2019-02-18 17:43:22 +01:00
|
|
|
|
2019-08-09 20:31:58 +03:00
|
|
|
ENTRY_OPTIONS[deconz.const.CONF_ALLOW_CLIP_SENSOR] = allow_clip_sensor
|
2019-02-18 17:43:22 +01:00
|
|
|
|
|
|
|
config_entry = config_entries.ConfigEntry(
|
2019-07-31 12:25:30 -07:00
|
|
|
1,
|
|
|
|
deconz.DOMAIN,
|
|
|
|
"Mock Title",
|
|
|
|
ENTRY_CONFIG,
|
|
|
|
"test",
|
|
|
|
config_entries.CONN_CLASS_LOCAL_PUSH,
|
2019-08-18 06:34:11 +02:00
|
|
|
system_options={},
|
|
|
|
options=ENTRY_OPTIONS,
|
2019-07-31 12:25:30 -07:00
|
|
|
)
|
2019-02-18 17:43:22 +01:00
|
|
|
gateway = deconz.DeconzGateway(hass, config_entry)
|
2019-03-01 23:09:31 -08:00
|
|
|
gateway.api = DeconzSession(hass.loop, session, **config_entry.data)
|
2019-02-18 17:43:22 +01:00
|
|
|
gateway.api.config = Mock()
|
2019-04-05 02:48:24 +02:00
|
|
|
hass.data[deconz.DOMAIN] = {gateway.bridgeid: gateway}
|
2019-02-18 17:43:22 +01:00
|
|
|
|
2019-07-31 12:25:30 -07:00
|
|
|
with patch("pydeconz.DeconzSession.async_get_state", return_value=mock_coro(data)):
|
2019-02-18 17:43:22 +01:00
|
|
|
await gateway.api.async_load_parameters()
|
|
|
|
|
2019-07-31 12:25:30 -07:00
|
|
|
await hass.config_entries.async_forward_entry_setup(config_entry, "climate")
|
2019-02-18 17:43:22 +01:00
|
|
|
# To flush out the service call to update the group
|
|
|
|
await hass.async_block_till_done()
|
2019-04-05 02:48:24 +02:00
|
|
|
return gateway
|
2019-02-18 17:43:22 +01:00
|
|
|
|
|
|
|
|
|
|
|
async def test_platform_manually_configured(hass):
|
|
|
|
"""Test that we do not discover anything or try to set up a gateway."""
|
2019-07-31 12:25:30 -07:00
|
|
|
assert (
|
|
|
|
await async_setup_component(
|
|
|
|
hass, climate.DOMAIN, {"climate": {"platform": deconz.DOMAIN}}
|
|
|
|
)
|
|
|
|
is True
|
|
|
|
)
|
2019-02-18 17:43:22 +01:00
|
|
|
assert deconz.DOMAIN not in hass.data
|
|
|
|
|
|
|
|
|
|
|
|
async def test_no_sensors(hass):
|
|
|
|
"""Test that no sensors in deconz results in no climate entities."""
|
2019-04-05 02:48:24 +02:00
|
|
|
gateway = await setup_gateway(hass, {})
|
|
|
|
assert not hass.data[deconz.DOMAIN][gateway.bridgeid].deconz_ids
|
2019-02-18 17:43:22 +01:00
|
|
|
assert not hass.states.async_all()
|
|
|
|
|
|
|
|
|
|
|
|
async def test_climate_devices(hass):
|
|
|
|
"""Test successful creation of sensor entities."""
|
2019-05-27 06:56:00 +02:00
|
|
|
gateway = await setup_gateway(hass, {"sensors": deepcopy(SENSOR)})
|
2019-04-05 02:48:24 +02:00
|
|
|
assert "climate.climate_1_name" in gateway.deconz_ids
|
|
|
|
assert "sensor.sensor_2_name" not in gateway.deconz_ids
|
2019-02-18 17:43:22 +01:00
|
|
|
assert len(hass.states.async_all()) == 1
|
|
|
|
|
2019-07-31 12:25:30 -07:00
|
|
|
gateway.api.sensors["1"].async_update({"state": {"on": False}})
|
2019-02-18 17:43:22 +01:00
|
|
|
|
|
|
|
await hass.services.async_call(
|
2019-07-31 12:25:30 -07:00
|
|
|
"climate",
|
|
|
|
"set_hvac_mode",
|
2019-08-30 14:28:39 +02:00
|
|
|
{"entity_id": "climate.climate_1_name", "hvac_mode": "auto"},
|
2019-07-31 12:25:30 -07:00
|
|
|
blocking=True,
|
2019-02-18 17:43:22 +01:00
|
|
|
)
|
2019-04-05 02:48:24 +02:00
|
|
|
gateway.api.session.put.assert_called_with(
|
2019-07-31 12:25:30 -07:00
|
|
|
"http://1.2.3.4:80/api/ABCDEF/sensors/1/config", data='{"mode": "auto"}'
|
2019-02-18 17:43:22 +01:00
|
|
|
)
|
|
|
|
|
2019-08-30 14:28:39 +02:00
|
|
|
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"}'
|
|
|
|
)
|
|
|
|
|
2019-02-18 17:43:22 +01:00
|
|
|
await hass.services.async_call(
|
2019-07-31 12:25:30 -07:00
|
|
|
"climate",
|
|
|
|
"set_hvac_mode",
|
|
|
|
{"entity_id": "climate.climate_1_name", "hvac_mode": "off"},
|
|
|
|
blocking=True,
|
2019-02-18 17:43:22 +01:00
|
|
|
)
|
2019-04-05 02:48:24 +02:00
|
|
|
gateway.api.session.put.assert_called_with(
|
2019-07-31 12:25:30 -07:00
|
|
|
"http://1.2.3.4:80/api/ABCDEF/sensors/1/config", data='{"mode": "off"}'
|
2019-02-18 17:43:22 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
await hass.services.async_call(
|
2019-07-31 12:25:30 -07:00
|
|
|
"climate",
|
|
|
|
"set_temperature",
|
|
|
|
{"entity_id": "climate.climate_1_name", "temperature": 20},
|
|
|
|
blocking=True,
|
2019-02-18 17:43:22 +01:00
|
|
|
)
|
2019-04-05 02:48:24 +02:00
|
|
|
gateway.api.session.put.assert_called_with(
|
2019-07-31 12:25:30 -07:00
|
|
|
"http://1.2.3.4:80/api/ABCDEF/sensors/1/config", data='{"heatsetpoint": 2000.0}'
|
2019-02-18 17:43:22 +01:00
|
|
|
)
|
|
|
|
|
2019-08-30 14:28:39 +02:00
|
|
|
assert len(gateway.api.session.put.mock_calls) == 4
|
2019-02-18 17:43:22 +01:00
|
|
|
|
|
|
|
|
|
|
|
async def test_verify_state_update(hass):
|
|
|
|
"""Test that state update properly."""
|
2019-05-27 06:56:00 +02:00
|
|
|
gateway = await setup_gateway(hass, {"sensors": deepcopy(SENSOR)})
|
2019-04-05 02:48:24 +02:00
|
|
|
assert "climate.climate_1_name" in gateway.deconz_ids
|
2019-02-18 17:43:22 +01:00
|
|
|
|
2019-07-31 12:25:30 -07:00
|
|
|
thermostat = hass.states.get("climate.climate_1_name")
|
2019-08-30 14:28:39 +02:00
|
|
|
assert thermostat.state == "auto"
|
2019-02-18 17:43:22 +01:00
|
|
|
|
|
|
|
state_update = {
|
|
|
|
"t": "event",
|
|
|
|
"e": "changed",
|
|
|
|
"r": "sensors",
|
|
|
|
"id": "1",
|
2019-07-31 12:25:30 -07:00
|
|
|
"state": {"on": False},
|
2019-02-18 17:43:22 +01:00
|
|
|
}
|
2019-04-05 02:48:24 +02:00
|
|
|
gateway.api.async_event_handler(state_update)
|
2019-02-18 17:43:22 +01:00
|
|
|
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
assert len(hass.states.async_all()) == 1
|
|
|
|
|
2019-07-31 12:25:30 -07:00
|
|
|
thermostat = hass.states.get("climate.climate_1_name")
|
2019-08-30 14:28:39 +02:00
|
|
|
assert thermostat.state == "auto"
|
2019-07-31 12:25:30 -07:00
|
|
|
assert gateway.api.sensors["1"].changed_keys == {"state", "r", "t", "on", "e", "id"}
|
2019-02-18 17:43:22 +01:00
|
|
|
|
|
|
|
|
|
|
|
async def test_add_new_climate_device(hass):
|
|
|
|
"""Test successful creation of climate entities."""
|
2019-04-05 02:48:24 +02:00
|
|
|
gateway = await setup_gateway(hass, {})
|
2019-02-18 17:43:22 +01:00
|
|
|
sensor = Mock()
|
2019-07-31 12:25:30 -07:00
|
|
|
sensor.name = "name"
|
|
|
|
sensor.type = "ZHAThermostat"
|
2019-08-12 07:42:12 -07:00
|
|
|
sensor.uniqueid = "1"
|
2019-02-18 17:43:22 +01:00
|
|
|
sensor.register_async_callback = Mock()
|
2019-09-06 01:38:00 +02:00
|
|
|
async_dispatcher_send(hass, gateway.async_signal_new_device("sensor"), [sensor])
|
2019-02-18 17:43:22 +01:00
|
|
|
await hass.async_block_till_done()
|
2019-04-05 02:48:24 +02:00
|
|
|
assert "climate.name" in gateway.deconz_ids
|
2019-02-18 17:43:22 +01:00
|
|
|
|
|
|
|
|
|
|
|
async def test_do_not_allow_clipsensor(hass):
|
|
|
|
"""Test that clip sensors can be ignored."""
|
2019-04-05 02:48:24 +02:00
|
|
|
gateway = await setup_gateway(hass, {}, allow_clip_sensor=False)
|
2019-02-18 17:43:22 +01:00
|
|
|
sensor = Mock()
|
2019-07-31 12:25:30 -07:00
|
|
|
sensor.name = "name"
|
|
|
|
sensor.type = "CLIPThermostat"
|
2019-02-18 17:43:22 +01:00
|
|
|
sensor.register_async_callback = Mock()
|
2019-09-06 01:38:00 +02:00
|
|
|
async_dispatcher_send(hass, gateway.async_signal_new_device("sensor"), [sensor])
|
2019-02-18 17:43:22 +01:00
|
|
|
await hass.async_block_till_done()
|
2019-04-05 02:48:24 +02:00
|
|
|
assert len(gateway.deconz_ids) == 0
|
2019-02-18 17:43:22 +01:00
|
|
|
|
|
|
|
|
|
|
|
async def test_unload_sensor(hass):
|
|
|
|
"""Test that it works to unload sensor entities."""
|
2019-04-05 02:48:24 +02:00
|
|
|
gateway = await setup_gateway(hass, {"sensors": SENSOR})
|
2019-02-18 17:43:22 +01:00
|
|
|
|
2019-04-05 02:48:24 +02:00
|
|
|
await gateway.async_reset()
|
2019-02-18 17:43:22 +01:00
|
|
|
|
|
|
|
assert len(hass.states.async_all()) == 0
|