core/tests/components/deconz/test_light.py

276 lines
8.5 KiB
Python
Raw Normal View History

"""deCONZ light 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.light as light
GROUPS = {
"1": {
"id": "Light group id",
"name": "Light group",
"type": "LightGroup",
"state": {"all_on": False, "any_on": True},
"action": {},
"scenes": [],
2019-07-31 19:25:30 +00:00
"lights": ["1", "2"],
},
"2": {
"id": "Empty group id",
"name": "Empty group",
"type": "LightGroup",
"state": {},
"action": {},
"scenes": [],
"lights": [],
},
}
LIGHTS = {
"1": {
"id": "RGB light id",
"name": "RGB light",
"state": {
"on": True,
"bri": 255,
"colormode": "xy",
"effect": "colorloop",
"xy": (500, 500),
"reachable": True,
},
"type": "Extended color light",
"uniqueid": "00:00:00:00:00:00:00:00-00",
},
"2": {
"id": "Tunable white light id",
"name": "Tunable white light",
"state": {"on": True, "colormode": "ct", "ct": 2500, "reachable": True},
"type": "Tunable white light",
"uniqueid": "00:00:00:00:00:00:00:01-00",
},
"3": {
"id": "On off switch id",
"name": "On off switch",
"type": "On/Off plug-in unit",
"state": {"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: BRIDGEID,
deconz.config_flow.CONF_HOST: "1.2.3.4",
2019-07-31 19:25:30 +00:00
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",
2019-07-31 19:25:30 +00:00
)
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."""
2019-07-31 19:25:30 +00:00
assert (
await async_setup_component(
hass, light.DOMAIN, {"light": {"platform": deconz.DOMAIN}}
)
is True
)
assert deconz.DOMAIN not in hass.data
async def test_no_lights_or_groups(hass):
"""Test that no lights or groups entities are created."""
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_lights_and_groups(hass):
"""Test that lights or groups entities are created."""
data = deepcopy(DECONZ_WEB_REQUEST)
data["groups"] = deepcopy(GROUPS)
data["lights"] = deepcopy(LIGHTS)
gateway = await setup_deconz_integration(
hass, ENTRY_CONFIG, options={}, get_state_response=data
2019-07-31 19:25:30 +00:00
)
assert "light.rgb_light" in gateway.deconz_ids
assert "light.tunable_white_light" in gateway.deconz_ids
assert "light.light_group" in gateway.deconz_ids
assert "light.empty_group" not in gateway.deconz_ids
assert "light.on_off_switch" not in gateway.deconz_ids
# 4 entities + 2 groups (one for switches and one for lights)
assert len(hass.states.async_all()) == 6
rgb_light = hass.states.get("light.rgb_light")
assert rgb_light.state == "on"
assert rgb_light.attributes["brightness"] == 255
assert rgb_light.attributes["hs_color"] == (224.235, 100.0)
assert rgb_light.attributes["is_deconz_group"] is False
tunable_white_light = hass.states.get("light.tunable_white_light")
assert tunable_white_light.state == "on"
assert tunable_white_light.attributes["color_temp"] == 2500
light_group = hass.states.get("light.light_group")
assert light_group.state == "on"
assert light_group.attributes["all_on"] is False
empty_group = hass.states.get("light.empty_group")
assert empty_group is None
rgb_light_device = gateway.api.lights["1"]
rgb_light_device.async_update({"state": {"on": False}})
await hass.async_block_till_done()
rgb_light = hass.states.get("light.rgb_light")
assert rgb_light.state == "off"
with patch.object(
rgb_light_device, "_async_set_callback", return_value=True
) as set_callback:
await hass.services.async_call(
light.DOMAIN,
light.SERVICE_TURN_ON,
{
"entity_id": "light.rgb_light",
"color_temp": 2500,
"brightness": 200,
"transition": 5,
"flash": "short",
"effect": "colorloop",
},
blocking=True,
)
await hass.async_block_till_done()
set_callback.assert_called_with(
"/lights/1/state",
{
"ct": 2500,
"bri": 200,
"transitiontime": 50,
"alert": "select",
"effect": "colorloop",
},
)
with patch.object(
rgb_light_device, "_async_set_callback", return_value=True
) as set_callback:
await hass.services.async_call(
light.DOMAIN,
light.SERVICE_TURN_ON,
{
"entity_id": "light.rgb_light",
"hs_color": (20, 30),
"flash": "long",
"effect": "None",
},
blocking=True,
)
await hass.async_block_till_done()
set_callback.assert_called_with(
"/lights/1/state",
{"xy": (0.411, 0.351), "alert": "lselect", "effect": "none"},
)
with patch.object(
rgb_light_device, "_async_set_callback", return_value=True
) as set_callback:
await hass.services.async_call(
light.DOMAIN,
light.SERVICE_TURN_OFF,
{"entity_id": "light.rgb_light", "transition": 5, "flash": "short"},
blocking=True,
)
await hass.async_block_till_done()
set_callback.assert_called_with(
"/lights/1/state", {"bri": 0, "transitiontime": 50, "alert": "select"}
)
with patch.object(
rgb_light_device, "_async_set_callback", return_value=True
) as set_callback:
await hass.services.async_call(
light.DOMAIN,
light.SERVICE_TURN_OFF,
{"entity_id": "light.rgb_light", "flash": "long"},
blocking=True,
)
await hass.async_block_till_done()
set_callback.assert_called_with("/lights/1/state", {"alert": "lselect"})
async def test_disable_light_groups(hass):
"""Test successful creation of sensor entities."""
data = deepcopy(DECONZ_WEB_REQUEST)
data["groups"] = deepcopy(GROUPS)
data["lights"] = deepcopy(LIGHTS)
gateway = await setup_deconz_integration(
hass,
ENTRY_CONFIG,
options={deconz.gateway.CONF_ALLOW_DECONZ_GROUPS: False},
get_state_response=data,
)
assert "light.rgb_light" in gateway.deconz_ids
assert "light.tunable_white_light" in gateway.deconz_ids
assert "light.light_group" not in gateway.deconz_ids
assert "light.empty_group" not in gateway.deconz_ids
assert "light.on_off_switch" not in gateway.deconz_ids
# 4 entities + 2 groups (one for switches and one for lights)
assert len(hass.states.async_all()) == 5
rgb_light = hass.states.get("light.rgb_light")
assert rgb_light is not None
tunable_white_light = hass.states.get("light.tunable_white_light")
assert tunable_white_light is not None
light_group = hass.states.get("light.light_group")
assert light_group is None
empty_group = hass.states.get("light.empty_group")
assert empty_group is None