2016-03-09 10:25:50 +01:00
|
|
|
"""The tests for the Light component."""
|
2016-10-30 22:18:53 +01:00
|
|
|
# pylint: disable=protected-access
|
2019-12-08 18:16:23 +01:00
|
|
|
from io import StringIO
|
|
|
|
import os
|
2014-11-25 21:28:43 -08:00
|
|
|
|
2018-12-14 10:18:36 +01:00
|
|
|
import pytest
|
2020-09-25 19:14:25 +02:00
|
|
|
import voluptuous as vol
|
2018-12-14 10:18:36 +01:00
|
|
|
|
2019-04-14 22:31:01 -07:00
|
|
|
from homeassistant import core
|
2019-12-08 18:16:23 +01:00
|
|
|
from homeassistant.components import light
|
2014-12-06 23:57:02 -08:00
|
|
|
from homeassistant.const import (
|
2019-07-31 12:25:30 -07:00
|
|
|
ATTR_ENTITY_ID,
|
|
|
|
CONF_PLATFORM,
|
|
|
|
SERVICE_TOGGLE,
|
2019-12-08 18:16:23 +01:00
|
|
|
SERVICE_TURN_OFF,
|
|
|
|
SERVICE_TURN_ON,
|
|
|
|
STATE_OFF,
|
|
|
|
STATE_ON,
|
2019-07-31 12:25:30 -07:00
|
|
|
)
|
2019-12-08 18:16:23 +01:00
|
|
|
from homeassistant.exceptions import Unauthorized
|
2020-09-25 19:14:25 +02:00
|
|
|
from homeassistant.setup import async_setup_component
|
2014-11-25 21:28:43 -08:00
|
|
|
|
2020-05-03 11:27:19 -07:00
|
|
|
import tests.async_mock as mock
|
2020-09-25 19:14:25 +02:00
|
|
|
from tests.common import async_mock_service
|
2018-09-27 23:13:11 +02:00
|
|
|
from tests.components.light import common
|
2014-11-25 21:28:43 -08:00
|
|
|
|
|
|
|
|
2020-09-25 19:14:25 +02:00
|
|
|
@pytest.fixture
|
|
|
|
def mock_storage(hass, hass_storage):
|
|
|
|
"""Clean up user light files at the end."""
|
|
|
|
yield
|
|
|
|
user_light_file = hass.config.path(light.LIGHT_PROFILES_FILE)
|
2014-11-25 21:28:43 -08:00
|
|
|
|
2020-09-25 19:14:25 +02:00
|
|
|
if os.path.isfile(user_light_file):
|
|
|
|
os.remove(user_light_file)
|
2014-11-25 21:28:43 -08:00
|
|
|
|
|
|
|
|
2020-09-25 19:14:25 +02:00
|
|
|
async def test_methods(hass):
|
|
|
|
"""Test if methods call the services as expected."""
|
|
|
|
# Test is_on
|
|
|
|
hass.states.async_set("light.test", STATE_ON)
|
|
|
|
assert light.is_on(hass, "light.test")
|
2014-11-25 23:16:07 -08:00
|
|
|
|
2020-09-25 19:14:25 +02:00
|
|
|
hass.states.async_set("light.test", STATE_OFF)
|
|
|
|
assert not light.is_on(hass, "light.test")
|
2014-11-25 23:16:07 -08:00
|
|
|
|
2020-09-25 19:14:25 +02:00
|
|
|
# Test turn_on
|
|
|
|
turn_on_calls = async_mock_service(hass, light.DOMAIN, SERVICE_TURN_ON)
|
2014-11-25 21:28:43 -08:00
|
|
|
|
2020-09-25 19:14:25 +02:00
|
|
|
await common.async_turn_on(
|
|
|
|
hass,
|
|
|
|
entity_id="entity_id_val",
|
|
|
|
transition="transition_val",
|
|
|
|
brightness="brightness_val",
|
|
|
|
rgb_color="rgb_color_val",
|
|
|
|
xy_color="xy_color_val",
|
|
|
|
profile="profile_val",
|
|
|
|
color_name="color_name_val",
|
|
|
|
white_value="white_val",
|
|
|
|
)
|
2014-11-25 21:28:43 -08:00
|
|
|
|
2020-09-25 19:14:25 +02:00
|
|
|
await hass.async_block_till_done()
|
2014-11-25 21:28:43 -08:00
|
|
|
|
2020-09-25 19:14:25 +02:00
|
|
|
assert len(turn_on_calls) == 1
|
|
|
|
call = turn_on_calls[-1]
|
2014-11-25 21:28:43 -08:00
|
|
|
|
2020-09-25 19:14:25 +02:00
|
|
|
assert call.domain == light.DOMAIN
|
|
|
|
assert call.service == SERVICE_TURN_ON
|
|
|
|
assert call.data.get(ATTR_ENTITY_ID) == "entity_id_val"
|
|
|
|
assert call.data.get(light.ATTR_TRANSITION) == "transition_val"
|
|
|
|
assert call.data.get(light.ATTR_BRIGHTNESS) == "brightness_val"
|
|
|
|
assert call.data.get(light.ATTR_RGB_COLOR) == "rgb_color_val"
|
|
|
|
assert call.data.get(light.ATTR_XY_COLOR) == "xy_color_val"
|
|
|
|
assert call.data.get(light.ATTR_PROFILE) == "profile_val"
|
|
|
|
assert call.data.get(light.ATTR_COLOR_NAME) == "color_name_val"
|
|
|
|
assert call.data.get(light.ATTR_WHITE_VALUE) == "white_val"
|
2014-11-25 21:28:43 -08:00
|
|
|
|
2020-09-25 19:14:25 +02:00
|
|
|
# Test turn_off
|
|
|
|
turn_off_calls = async_mock_service(hass, light.DOMAIN, SERVICE_TURN_OFF)
|
2014-11-25 21:28:43 -08:00
|
|
|
|
2020-09-25 19:14:25 +02:00
|
|
|
await common.async_turn_off(
|
|
|
|
hass, entity_id="entity_id_val", transition="transition_val"
|
|
|
|
)
|
2014-11-25 21:28:43 -08:00
|
|
|
|
2020-09-25 19:14:25 +02:00
|
|
|
await hass.async_block_till_done()
|
2014-11-25 21:28:43 -08:00
|
|
|
|
2020-09-25 19:14:25 +02:00
|
|
|
assert len(turn_off_calls) == 1
|
|
|
|
call = turn_off_calls[-1]
|
2014-11-25 21:28:43 -08:00
|
|
|
|
2020-09-25 19:14:25 +02:00
|
|
|
assert light.DOMAIN == call.domain
|
|
|
|
assert SERVICE_TURN_OFF == call.service
|
|
|
|
assert call.data[ATTR_ENTITY_ID] == "entity_id_val"
|
|
|
|
assert call.data[light.ATTR_TRANSITION] == "transition_val"
|
2014-11-25 21:28:43 -08:00
|
|
|
|
2020-09-25 19:14:25 +02:00
|
|
|
# Test toggle
|
|
|
|
toggle_calls = async_mock_service(hass, light.DOMAIN, SERVICE_TOGGLE)
|
2014-11-25 21:28:43 -08:00
|
|
|
|
2020-09-25 19:14:25 +02:00
|
|
|
await common.async_toggle(
|
|
|
|
hass, entity_id="entity_id_val", transition="transition_val"
|
|
|
|
)
|
2016-01-17 16:42:18 -05:00
|
|
|
|
2020-09-25 19:14:25 +02:00
|
|
|
await hass.async_block_till_done()
|
2016-01-17 16:42:18 -05:00
|
|
|
|
2020-09-25 19:14:25 +02:00
|
|
|
assert len(toggle_calls) == 1
|
|
|
|
call = toggle_calls[-1]
|
2016-01-17 16:42:18 -05:00
|
|
|
|
2020-09-25 19:14:25 +02:00
|
|
|
assert call.domain == light.DOMAIN
|
|
|
|
assert call.service == SERVICE_TOGGLE
|
|
|
|
assert call.data[ATTR_ENTITY_ID] == "entity_id_val"
|
|
|
|
assert call.data[light.ATTR_TRANSITION] == "transition_val"
|
2016-01-17 16:42:18 -05:00
|
|
|
|
|
|
|
|
2020-09-25 19:14:25 +02:00
|
|
|
async def test_services(hass):
|
|
|
|
"""Test the provided services."""
|
|
|
|
platform = getattr(hass.components, "test.light")
|
2014-11-30 23:14:08 -08:00
|
|
|
|
2020-09-25 19:14:25 +02:00
|
|
|
platform.init()
|
|
|
|
assert await async_setup_component(
|
|
|
|
hass, light.DOMAIN, {light.DOMAIN: {CONF_PLATFORM: "test"}}
|
|
|
|
)
|
|
|
|
await hass.async_block_till_done()
|
2014-11-25 21:28:43 -08:00
|
|
|
|
2020-09-25 19:14:25 +02:00
|
|
|
ent1, ent2, ent3 = platform.ENTITIES
|
2014-11-25 21:28:43 -08:00
|
|
|
|
2020-09-25 19:14:25 +02:00
|
|
|
# Test init
|
|
|
|
assert light.is_on(hass, ent1.entity_id)
|
|
|
|
assert not light.is_on(hass, ent2.entity_id)
|
|
|
|
assert not light.is_on(hass, ent3.entity_id)
|
2014-11-25 21:28:43 -08:00
|
|
|
|
2020-09-25 19:14:25 +02:00
|
|
|
# Test basic turn_on, turn_off, toggle services
|
|
|
|
await common.async_turn_off(hass, entity_id=ent1.entity_id)
|
|
|
|
await common.async_turn_on(hass, entity_id=ent2.entity_id)
|
2014-11-25 21:28:43 -08:00
|
|
|
|
2020-09-25 19:14:25 +02:00
|
|
|
await hass.async_block_till_done()
|
2014-11-25 21:28:43 -08:00
|
|
|
|
2020-09-25 19:14:25 +02:00
|
|
|
assert not light.is_on(hass, ent1.entity_id)
|
|
|
|
assert light.is_on(hass, ent2.entity_id)
|
2014-11-25 21:28:43 -08:00
|
|
|
|
2020-09-25 19:14:25 +02:00
|
|
|
# turn on all lights
|
|
|
|
await common.async_turn_on(hass)
|
2014-11-25 21:28:43 -08:00
|
|
|
|
2020-09-25 19:14:25 +02:00
|
|
|
await hass.async_block_till_done()
|
2014-11-25 21:28:43 -08:00
|
|
|
|
2020-09-25 19:14:25 +02:00
|
|
|
assert light.is_on(hass, ent1.entity_id)
|
|
|
|
assert light.is_on(hass, ent2.entity_id)
|
|
|
|
assert light.is_on(hass, ent3.entity_id)
|
2014-11-25 21:28:43 -08:00
|
|
|
|
2020-09-25 19:14:25 +02:00
|
|
|
# turn off all lights
|
|
|
|
await common.async_turn_off(hass)
|
2014-11-25 21:28:43 -08:00
|
|
|
|
2020-09-25 19:14:25 +02:00
|
|
|
await hass.async_block_till_done()
|
2014-11-25 21:28:43 -08:00
|
|
|
|
2020-09-25 19:14:25 +02:00
|
|
|
assert not light.is_on(hass, ent1.entity_id)
|
|
|
|
assert not light.is_on(hass, ent2.entity_id)
|
|
|
|
assert not light.is_on(hass, ent3.entity_id)
|
2016-01-17 16:42:18 -05:00
|
|
|
|
2020-09-25 19:14:25 +02:00
|
|
|
# turn off all lights by setting brightness to 0
|
|
|
|
await common.async_turn_on(hass)
|
|
|
|
await hass.async_block_till_done()
|
2019-03-31 06:04:32 +02:00
|
|
|
|
2020-09-25 19:14:25 +02:00
|
|
|
await common.async_turn_on(hass, brightness=0)
|
|
|
|
await hass.async_block_till_done()
|
2019-03-31 06:04:32 +02:00
|
|
|
|
2020-09-25 19:14:25 +02:00
|
|
|
assert not light.is_on(hass, ent1.entity_id)
|
|
|
|
assert not light.is_on(hass, ent2.entity_id)
|
|
|
|
assert not light.is_on(hass, ent3.entity_id)
|
2019-03-31 06:04:32 +02:00
|
|
|
|
2020-09-25 19:14:25 +02:00
|
|
|
# toggle all lights
|
|
|
|
await common.async_toggle(hass)
|
2019-03-31 06:04:32 +02:00
|
|
|
|
2020-09-25 19:14:25 +02:00
|
|
|
await hass.async_block_till_done()
|
2019-03-31 06:04:32 +02:00
|
|
|
|
2020-09-25 19:14:25 +02:00
|
|
|
assert light.is_on(hass, ent1.entity_id)
|
|
|
|
assert light.is_on(hass, ent2.entity_id)
|
|
|
|
assert light.is_on(hass, ent3.entity_id)
|
2016-01-17 16:42:18 -05:00
|
|
|
|
2020-09-25 19:14:25 +02:00
|
|
|
# toggle all lights
|
|
|
|
await common.async_toggle(hass)
|
2016-01-17 16:42:18 -05:00
|
|
|
|
2020-09-25 19:14:25 +02:00
|
|
|
await hass.async_block_till_done()
|
2016-01-17 16:42:18 -05:00
|
|
|
|
2020-09-25 19:14:25 +02:00
|
|
|
assert not light.is_on(hass, ent1.entity_id)
|
|
|
|
assert not light.is_on(hass, ent2.entity_id)
|
|
|
|
assert not light.is_on(hass, ent3.entity_id)
|
|
|
|
|
|
|
|
# Ensure all attributes process correctly
|
|
|
|
await common.async_turn_on(
|
|
|
|
hass, ent1.entity_id, transition=10, brightness=20, color_name="blue"
|
|
|
|
)
|
|
|
|
await common.async_turn_on(
|
|
|
|
hass, ent2.entity_id, rgb_color=(255, 255, 255), white_value=255
|
|
|
|
)
|
|
|
|
await common.async_turn_on(hass, ent3.entity_id, xy_color=(0.4, 0.6))
|
|
|
|
await hass.async_block_till_done()
|
2016-01-17 16:42:18 -05:00
|
|
|
|
2020-09-25 19:14:25 +02:00
|
|
|
_, data = ent1.last_call("turn_on")
|
|
|
|
assert {
|
|
|
|
light.ATTR_TRANSITION: 10,
|
|
|
|
light.ATTR_BRIGHTNESS: 20,
|
|
|
|
light.ATTR_HS_COLOR: (240, 100),
|
|
|
|
} == data
|
|
|
|
|
|
|
|
_, data = ent2.last_call("turn_on")
|
|
|
|
assert {light.ATTR_HS_COLOR: (0, 0), light.ATTR_WHITE_VALUE: 255} == data
|
|
|
|
|
|
|
|
_, data = ent3.last_call("turn_on")
|
|
|
|
assert {light.ATTR_HS_COLOR: (71.059, 100)} == data
|
|
|
|
|
|
|
|
# Ensure attributes are filtered when light is turned off
|
|
|
|
await common.async_turn_on(
|
|
|
|
hass, ent1.entity_id, transition=10, brightness=0, color_name="blue"
|
|
|
|
)
|
|
|
|
await common.async_turn_on(
|
|
|
|
hass,
|
|
|
|
ent2.entity_id,
|
|
|
|
brightness=0,
|
|
|
|
rgb_color=(255, 255, 255),
|
|
|
|
white_value=0,
|
|
|
|
)
|
|
|
|
await common.async_turn_on(hass, ent3.entity_id, brightness=0, xy_color=(0.4, 0.6))
|
2016-01-17 16:42:18 -05:00
|
|
|
|
2020-09-25 19:14:25 +02:00
|
|
|
await hass.async_block_till_done()
|
2014-11-25 21:28:43 -08:00
|
|
|
|
2020-09-25 19:14:25 +02:00
|
|
|
assert not light.is_on(hass, ent1.entity_id)
|
|
|
|
assert not light.is_on(hass, ent2.entity_id)
|
|
|
|
assert not light.is_on(hass, ent3.entity_id)
|
2014-11-25 21:28:43 -08:00
|
|
|
|
2020-09-25 19:14:25 +02:00
|
|
|
_, data = ent1.last_call("turn_off")
|
|
|
|
assert {light.ATTR_TRANSITION: 10} == data
|
2014-11-25 21:28:43 -08:00
|
|
|
|
2020-09-25 19:14:25 +02:00
|
|
|
_, data = ent2.last_call("turn_off")
|
|
|
|
assert {} == data
|
2014-11-25 21:28:43 -08:00
|
|
|
|
2020-09-25 19:14:25 +02:00
|
|
|
_, data = ent3.last_call("turn_off")
|
|
|
|
assert {} == data
|
2014-11-25 21:28:43 -08:00
|
|
|
|
2020-09-25 19:14:25 +02:00
|
|
|
# One of the light profiles
|
|
|
|
prof_name, prof_h, prof_s, prof_bri, prof_t = "relax", 35.932, 69.412, 144, 0
|
2014-11-25 21:28:43 -08:00
|
|
|
|
2020-09-25 19:14:25 +02:00
|
|
|
# Test light profiles
|
|
|
|
await common.async_turn_on(hass, ent1.entity_id, profile=prof_name)
|
|
|
|
# Specify a profile and a brightness attribute to overwrite it
|
|
|
|
await common.async_turn_on(
|
|
|
|
hass, ent2.entity_id, profile=prof_name, brightness=100, transition=1
|
|
|
|
)
|
2019-03-31 06:04:32 +02:00
|
|
|
|
2020-09-25 19:14:25 +02:00
|
|
|
await hass.async_block_till_done()
|
2019-03-31 06:04:32 +02:00
|
|
|
|
2020-09-25 19:14:25 +02:00
|
|
|
_, data = ent1.last_call("turn_on")
|
|
|
|
assert {
|
|
|
|
light.ATTR_BRIGHTNESS: prof_bri,
|
|
|
|
light.ATTR_HS_COLOR: (prof_h, prof_s),
|
|
|
|
light.ATTR_TRANSITION: prof_t,
|
|
|
|
} == data
|
|
|
|
|
|
|
|
_, data = ent2.last_call("turn_on")
|
|
|
|
assert {
|
|
|
|
light.ATTR_BRIGHTNESS: 100,
|
|
|
|
light.ATTR_HS_COLOR: (prof_h, prof_s),
|
|
|
|
light.ATTR_TRANSITION: 1,
|
|
|
|
} == data
|
|
|
|
|
|
|
|
# Test toggle with parameters
|
|
|
|
await common.async_toggle(
|
|
|
|
hass, ent3.entity_id, profile=prof_name, brightness_pct=100
|
|
|
|
)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
_, data = ent3.last_call("turn_on")
|
|
|
|
assert {
|
|
|
|
light.ATTR_BRIGHTNESS: 255,
|
|
|
|
light.ATTR_HS_COLOR: (prof_h, prof_s),
|
|
|
|
light.ATTR_TRANSITION: prof_t,
|
|
|
|
} == data
|
|
|
|
|
|
|
|
# Test bad data
|
|
|
|
await common.async_turn_on(hass)
|
|
|
|
await common.async_turn_on(hass, ent1.entity_id, profile="nonexisting")
|
|
|
|
with pytest.raises(vol.MultipleInvalid):
|
|
|
|
await common.async_turn_on(hass, ent2.entity_id, xy_color=["bla-di-bla", 5])
|
|
|
|
with pytest.raises(vol.MultipleInvalid):
|
|
|
|
await common.async_turn_on(hass, ent3.entity_id, rgb_color=[255, None, 2])
|
2019-03-31 06:04:32 +02:00
|
|
|
|
2020-09-25 19:14:25 +02:00
|
|
|
await hass.async_block_till_done()
|
2019-03-31 06:04:32 +02:00
|
|
|
|
2020-09-25 19:14:25 +02:00
|
|
|
_, data = ent1.last_call("turn_on")
|
|
|
|
assert {} == data
|
2019-03-31 06:04:32 +02:00
|
|
|
|
2020-09-25 19:14:25 +02:00
|
|
|
_, data = ent2.last_call("turn_on")
|
|
|
|
assert {} == data
|
2019-03-31 06:04:32 +02:00
|
|
|
|
2020-09-25 19:14:25 +02:00
|
|
|
_, data = ent3.last_call("turn_on")
|
|
|
|
assert {} == data
|
2014-11-25 21:28:43 -08:00
|
|
|
|
2020-09-25 19:14:25 +02:00
|
|
|
# faulty attributes will not trigger a service call
|
|
|
|
with pytest.raises(vol.MultipleInvalid):
|
|
|
|
await common.async_turn_on(
|
|
|
|
hass, ent1.entity_id, profile=prof_name, brightness="bright"
|
2020-07-21 17:19:07 -07:00
|
|
|
)
|
2020-09-25 19:14:25 +02:00
|
|
|
with pytest.raises(vol.MultipleInvalid):
|
|
|
|
await common.async_turn_on(hass, ent1.entity_id, rgb_color="yellowish")
|
|
|
|
with pytest.raises(vol.MultipleInvalid):
|
|
|
|
await common.async_turn_on(hass, ent2.entity_id, white_value="high")
|
2014-11-25 21:28:43 -08:00
|
|
|
|
2020-09-25 19:14:25 +02:00
|
|
|
await hass.async_block_till_done()
|
2014-11-25 21:28:43 -08:00
|
|
|
|
2020-09-25 19:14:25 +02:00
|
|
|
_, data = ent1.last_call("turn_on")
|
|
|
|
assert {} == data
|
2014-11-25 21:28:43 -08:00
|
|
|
|
2020-09-25 19:14:25 +02:00
|
|
|
_, data = ent2.last_call("turn_on")
|
|
|
|
assert {} == data
|
2016-09-21 06:26:40 +02:00
|
|
|
|
2014-11-25 21:28:43 -08:00
|
|
|
|
2020-09-25 19:14:25 +02:00
|
|
|
async def test_broken_light_profiles(hass, mock_storage):
|
|
|
|
"""Test light profiles."""
|
|
|
|
platform = getattr(hass.components, "test.light")
|
|
|
|
platform.init()
|
2014-11-25 23:16:07 -08:00
|
|
|
|
2020-09-25 19:14:25 +02:00
|
|
|
user_light_file = hass.config.path(light.LIGHT_PROFILES_FILE)
|
2014-11-25 23:16:07 -08:00
|
|
|
|
2020-09-25 19:14:25 +02:00
|
|
|
# Setup a wrong light file
|
|
|
|
with open(user_light_file, "w") as user_file:
|
|
|
|
user_file.write("id,x,y,brightness,transition\n")
|
|
|
|
user_file.write("I,WILL,NOT,WORK,EVER\n")
|
2014-11-25 23:16:07 -08:00
|
|
|
|
2020-09-25 19:14:25 +02:00
|
|
|
assert not await async_setup_component(
|
|
|
|
hass, light.DOMAIN, {light.DOMAIN: {CONF_PLATFORM: "test"}}
|
|
|
|
)
|
2014-11-25 23:16:07 -08:00
|
|
|
|
2015-01-19 21:39:24 -08:00
|
|
|
|
2020-09-25 19:14:25 +02:00
|
|
|
async def test_light_profiles(hass, mock_storage):
|
|
|
|
"""Test light profiles."""
|
|
|
|
platform = getattr(hass.components, "test.light")
|
|
|
|
platform.init()
|
2014-11-25 23:16:07 -08:00
|
|
|
|
2020-09-25 19:14:25 +02:00
|
|
|
user_light_file = hass.config.path(light.LIGHT_PROFILES_FILE)
|
2014-11-25 23:16:07 -08:00
|
|
|
|
2020-09-25 19:14:25 +02:00
|
|
|
with open(user_light_file, "w") as user_file:
|
|
|
|
user_file.write("id,x,y,brightness\n")
|
|
|
|
user_file.write("test,.4,.6,100\n")
|
|
|
|
user_file.write("test_off,0,0,0\n")
|
2014-11-25 23:16:07 -08:00
|
|
|
|
2020-09-25 19:14:25 +02:00
|
|
|
assert await async_setup_component(
|
|
|
|
hass, light.DOMAIN, {light.DOMAIN: {CONF_PLATFORM: "test"}}
|
|
|
|
)
|
|
|
|
await hass.async_block_till_done()
|
2014-11-25 23:16:07 -08:00
|
|
|
|
2020-09-25 19:14:25 +02:00
|
|
|
ent1, _, _ = platform.ENTITIES
|
2014-11-25 23:16:07 -08:00
|
|
|
|
2020-09-25 19:14:25 +02:00
|
|
|
await common.async_turn_on(hass, ent1.entity_id, profile="test")
|
2014-11-25 23:16:07 -08:00
|
|
|
|
2020-09-25 19:14:25 +02:00
|
|
|
await hass.async_block_till_done()
|
2014-11-25 23:16:07 -08:00
|
|
|
|
2020-09-25 19:14:25 +02:00
|
|
|
_, data = ent1.last_call("turn_on")
|
2018-02-27 18:02:21 -08:00
|
|
|
|
2020-09-25 19:14:25 +02:00
|
|
|
assert light.is_on(hass, ent1.entity_id)
|
|
|
|
assert data == {
|
|
|
|
light.ATTR_HS_COLOR: (71.059, 100),
|
|
|
|
light.ATTR_BRIGHTNESS: 100,
|
|
|
|
light.ATTR_TRANSITION: 0,
|
|
|
|
}
|
2019-03-31 06:04:32 +02:00
|
|
|
|
2020-09-25 19:14:25 +02:00
|
|
|
await common.async_turn_on(hass, ent1.entity_id, profile="test_off")
|
2019-03-31 06:04:32 +02:00
|
|
|
|
2020-09-25 19:14:25 +02:00
|
|
|
await hass.async_block_till_done()
|
2019-03-31 06:04:32 +02:00
|
|
|
|
2020-09-25 19:14:25 +02:00
|
|
|
_, data = ent1.last_call("turn_off")
|
2020-07-21 17:19:07 -07:00
|
|
|
|
2020-09-25 19:14:25 +02:00
|
|
|
assert not light.is_on(hass, ent1.entity_id)
|
|
|
|
assert data == {light.ATTR_TRANSITION: 0}
|
2020-07-21 17:19:07 -07:00
|
|
|
|
|
|
|
|
2020-09-25 19:14:25 +02:00
|
|
|
async def test_light_profiles_with_transition(hass, mock_storage):
|
|
|
|
"""Test light profiles with transition."""
|
|
|
|
platform = getattr(hass.components, "test.light")
|
|
|
|
platform.init()
|
2020-07-21 17:19:07 -07:00
|
|
|
|
2020-09-25 19:14:25 +02:00
|
|
|
user_light_file = hass.config.path(light.LIGHT_PROFILES_FILE)
|
2020-07-21 17:19:07 -07:00
|
|
|
|
2020-09-25 19:14:25 +02:00
|
|
|
with open(user_light_file, "w") as user_file:
|
|
|
|
user_file.write("id,x,y,brightness,transition\n")
|
|
|
|
user_file.write("test,.4,.6,100,2\n")
|
|
|
|
user_file.write("test_off,0,0,0,0\n")
|
2020-07-21 17:19:07 -07:00
|
|
|
|
2020-09-25 19:14:25 +02:00
|
|
|
assert await async_setup_component(
|
|
|
|
hass, light.DOMAIN, {light.DOMAIN: {CONF_PLATFORM: "test"}}
|
|
|
|
)
|
|
|
|
await hass.async_block_till_done()
|
2020-07-21 17:19:07 -07:00
|
|
|
|
2020-09-25 19:14:25 +02:00
|
|
|
ent1, _, _ = platform.ENTITIES
|
2020-07-21 17:19:07 -07:00
|
|
|
|
2020-09-25 19:14:25 +02:00
|
|
|
await common.async_turn_on(hass, ent1.entity_id, profile="test")
|
2020-07-21 17:19:07 -07:00
|
|
|
|
2020-09-25 19:14:25 +02:00
|
|
|
await hass.async_block_till_done()
|
2020-07-21 17:19:07 -07:00
|
|
|
|
2020-09-25 19:14:25 +02:00
|
|
|
_, data = ent1.last_call("turn_on")
|
2020-07-21 17:19:07 -07:00
|
|
|
|
2020-09-25 19:14:25 +02:00
|
|
|
assert light.is_on(hass, ent1.entity_id)
|
|
|
|
assert data == {
|
|
|
|
light.ATTR_HS_COLOR: (71.059, 100),
|
|
|
|
light.ATTR_BRIGHTNESS: 100,
|
|
|
|
light.ATTR_TRANSITION: 2,
|
|
|
|
}
|
2020-07-21 17:19:07 -07:00
|
|
|
|
2020-09-25 19:14:25 +02:00
|
|
|
await common.async_turn_on(hass, ent1.entity_id, profile="test_off")
|
2020-07-21 17:19:07 -07:00
|
|
|
|
2020-09-25 19:14:25 +02:00
|
|
|
await hass.async_block_till_done()
|
2019-03-31 06:04:32 +02:00
|
|
|
|
2020-09-25 19:14:25 +02:00
|
|
|
_, data = ent1.last_call("turn_off")
|
2018-07-24 20:29:59 +02:00
|
|
|
|
2020-09-25 19:14:25 +02:00
|
|
|
assert not light.is_on(hass, ent1.entity_id)
|
|
|
|
assert data == {light.ATTR_TRANSITION: 0}
|
2018-07-24 20:29:59 +02:00
|
|
|
|
|
|
|
|
2020-09-25 19:14:25 +02:00
|
|
|
async def test_default_profiles_group(hass, mock_storage):
|
|
|
|
"""Test default turn-on light profile for all lights."""
|
|
|
|
platform = getattr(hass.components, "test.light")
|
|
|
|
platform.init()
|
2018-07-24 20:29:59 +02:00
|
|
|
|
2020-09-25 19:14:25 +02:00
|
|
|
user_light_file = hass.config.path(light.LIGHT_PROFILES_FILE)
|
|
|
|
real_isfile = os.path.isfile
|
|
|
|
real_open = open
|
|
|
|
|
|
|
|
def _mock_isfile(path):
|
|
|
|
if path == user_light_file:
|
|
|
|
return True
|
|
|
|
return real_isfile(path)
|
|
|
|
|
|
|
|
def _mock_open(path, *args, **kwargs):
|
|
|
|
if path == user_light_file:
|
|
|
|
return StringIO(profile_data)
|
|
|
|
return real_open(path, *args, **kwargs)
|
|
|
|
|
|
|
|
profile_data = "id,x,y,brightness,transition\ngroup.all_lights.default,.4,.6,99,2\n"
|
|
|
|
with mock.patch("os.path.isfile", side_effect=_mock_isfile), mock.patch(
|
|
|
|
"builtins.open", side_effect=_mock_open
|
|
|
|
):
|
|
|
|
assert await async_setup_component(
|
|
|
|
hass, light.DOMAIN, {light.DOMAIN: {CONF_PLATFORM: "test"}}
|
2019-07-31 12:25:30 -07:00
|
|
|
)
|
2020-09-25 19:14:25 +02:00
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
ent, _, _ = platform.ENTITIES
|
|
|
|
await common.async_turn_on(hass, ent.entity_id)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
_, data = ent.last_call("turn_on")
|
|
|
|
assert data == {
|
|
|
|
light.ATTR_HS_COLOR: (71.059, 100),
|
|
|
|
light.ATTR_BRIGHTNESS: 99,
|
|
|
|
light.ATTR_TRANSITION: 2,
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async def test_default_profiles_light(hass, mock_storage):
|
|
|
|
"""Test default turn-on light profile for a specific light."""
|
|
|
|
platform = getattr(hass.components, "test.light")
|
|
|
|
platform.init()
|
|
|
|
|
|
|
|
user_light_file = hass.config.path(light.LIGHT_PROFILES_FILE)
|
|
|
|
real_isfile = os.path.isfile
|
|
|
|
real_open = open
|
|
|
|
|
|
|
|
def _mock_isfile(path):
|
|
|
|
if path == user_light_file:
|
|
|
|
return True
|
|
|
|
return real_isfile(path)
|
|
|
|
|
|
|
|
def _mock_open(path, *args, **kwargs):
|
|
|
|
if path == user_light_file:
|
|
|
|
return StringIO(profile_data)
|
|
|
|
return real_open(path, *args, **kwargs)
|
|
|
|
|
|
|
|
profile_data = (
|
|
|
|
"id,x,y,brightness,transition\n"
|
|
|
|
+ "group.all_lights.default,.3,.5,200,0\n"
|
|
|
|
+ "light.ceiling_2.default,.6,.6,100,3\n"
|
|
|
|
)
|
|
|
|
with mock.patch("os.path.isfile", side_effect=_mock_isfile), mock.patch(
|
|
|
|
"builtins.open", side_effect=_mock_open
|
|
|
|
):
|
|
|
|
assert await async_setup_component(
|
|
|
|
hass, light.DOMAIN, {light.DOMAIN: {CONF_PLATFORM: "test"}}
|
2019-09-15 09:50:17 +02:00
|
|
|
)
|
2020-09-25 19:14:25 +02:00
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
dev = next(filter(lambda x: x.entity_id == "light.ceiling_2", platform.ENTITIES))
|
|
|
|
await common.async_turn_on(hass, dev.entity_id)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
_, data = dev.last_call("turn_on")
|
|
|
|
assert data == {
|
|
|
|
light.ATTR_HS_COLOR: (50.353, 100),
|
|
|
|
light.ATTR_BRIGHTNESS: 100,
|
|
|
|
light.ATTR_TRANSITION: 3,
|
|
|
|
}
|
2018-07-24 20:29:59 +02:00
|
|
|
|
2018-02-27 18:02:21 -08:00
|
|
|
|
2018-11-21 12:26:08 +01:00
|
|
|
async def test_light_context(hass, hass_admin_user):
|
2018-07-29 01:53:37 +01:00
|
|
|
"""Test that light context works."""
|
2019-12-04 22:47:40 -08:00
|
|
|
platform = getattr(hass.components, "test.light")
|
|
|
|
platform.init()
|
2019-07-31 12:25:30 -07:00
|
|
|
assert await async_setup_component(hass, "light", {"light": {"platform": "test"}})
|
2020-05-31 22:18:30 -07:00
|
|
|
await hass.async_block_till_done()
|
2018-07-29 01:53:37 +01:00
|
|
|
|
2019-07-31 12:25:30 -07:00
|
|
|
state = hass.states.get("light.ceiling")
|
2018-07-29 01:53:37 +01:00
|
|
|
assert state is not None
|
|
|
|
|
2019-07-31 12:25:30 -07:00
|
|
|
await hass.services.async_call(
|
|
|
|
"light",
|
|
|
|
"toggle",
|
|
|
|
{"entity_id": state.entity_id},
|
|
|
|
True,
|
|
|
|
core.Context(user_id=hass_admin_user.id),
|
|
|
|
)
|
2018-07-29 01:53:37 +01:00
|
|
|
|
2019-07-31 12:25:30 -07:00
|
|
|
state2 = hass.states.get("light.ceiling")
|
2018-07-29 01:53:37 +01:00
|
|
|
assert state2 is not None
|
|
|
|
assert state.state != state2.state
|
2018-11-21 12:26:08 +01:00
|
|
|
assert state2.context.user_id == hass_admin_user.id
|
2018-12-14 10:18:36 +01:00
|
|
|
|
|
|
|
|
|
|
|
async def test_light_turn_on_auth(hass, hass_admin_user):
|
|
|
|
"""Test that light context works."""
|
2019-12-04 22:47:40 -08:00
|
|
|
platform = getattr(hass.components, "test.light")
|
|
|
|
platform.init()
|
2019-07-31 12:25:30 -07:00
|
|
|
assert await async_setup_component(hass, "light", {"light": {"platform": "test"}})
|
2020-05-31 22:18:30 -07:00
|
|
|
await hass.async_block_till_done()
|
2018-12-14 10:18:36 +01:00
|
|
|
|
2019-07-31 12:25:30 -07:00
|
|
|
state = hass.states.get("light.ceiling")
|
2018-12-14 10:18:36 +01:00
|
|
|
assert state is not None
|
|
|
|
|
|
|
|
hass_admin_user.mock_policy({})
|
|
|
|
|
|
|
|
with pytest.raises(Unauthorized):
|
2019-07-31 12:25:30 -07:00
|
|
|
await hass.services.async_call(
|
|
|
|
"light",
|
|
|
|
"turn_on",
|
|
|
|
{"entity_id": state.entity_id},
|
|
|
|
True,
|
|
|
|
core.Context(user_id=hass_admin_user.id),
|
|
|
|
)
|
2020-02-04 16:13:29 -08:00
|
|
|
|
|
|
|
|
|
|
|
async def test_light_brightness_step(hass):
|
|
|
|
"""Test that light context works."""
|
|
|
|
platform = getattr(hass.components, "test.light")
|
|
|
|
platform.init()
|
|
|
|
entity = platform.ENTITIES[0]
|
|
|
|
entity.supported_features = light.SUPPORT_BRIGHTNESS
|
|
|
|
entity.brightness = 100
|
|
|
|
assert await async_setup_component(hass, "light", {"light": {"platform": "test"}})
|
2020-05-31 22:18:30 -07:00
|
|
|
await hass.async_block_till_done()
|
2020-02-04 16:13:29 -08:00
|
|
|
|
|
|
|
state = hass.states.get(entity.entity_id)
|
|
|
|
assert state is not None
|
|
|
|
assert state.attributes["brightness"] == 100
|
|
|
|
|
|
|
|
await hass.services.async_call(
|
|
|
|
"light",
|
|
|
|
"turn_on",
|
|
|
|
{"entity_id": entity.entity_id, "brightness_step": -10},
|
|
|
|
True,
|
|
|
|
)
|
|
|
|
|
|
|
|
_, data = entity.last_call("turn_on")
|
|
|
|
assert data["brightness"] == 90, data
|
|
|
|
|
|
|
|
await hass.services.async_call(
|
|
|
|
"light",
|
|
|
|
"turn_on",
|
|
|
|
{"entity_id": entity.entity_id, "brightness_step_pct": 10},
|
|
|
|
True,
|
|
|
|
)
|
|
|
|
|
|
|
|
_, data = entity.last_call("turn_on")
|
2020-04-14 13:26:18 -05:00
|
|
|
assert data["brightness"] == 126, data
|
|
|
|
|
|
|
|
|
|
|
|
async def test_light_brightness_pct_conversion(hass):
|
|
|
|
"""Test that light brightness percent conversion."""
|
|
|
|
platform = getattr(hass.components, "test.light")
|
|
|
|
platform.init()
|
|
|
|
entity = platform.ENTITIES[0]
|
|
|
|
entity.supported_features = light.SUPPORT_BRIGHTNESS
|
|
|
|
entity.brightness = 100
|
|
|
|
assert await async_setup_component(hass, "light", {"light": {"platform": "test"}})
|
2020-05-31 22:18:30 -07:00
|
|
|
await hass.async_block_till_done()
|
2020-04-14 13:26:18 -05:00
|
|
|
|
|
|
|
state = hass.states.get(entity.entity_id)
|
|
|
|
assert state is not None
|
|
|
|
assert state.attributes["brightness"] == 100
|
|
|
|
|
|
|
|
await hass.services.async_call(
|
2020-08-27 13:56:20 +02:00
|
|
|
"light",
|
|
|
|
"turn_on",
|
|
|
|
{"entity_id": entity.entity_id, "brightness_pct": 1},
|
|
|
|
True,
|
2020-04-14 13:26:18 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
_, data = entity.last_call("turn_on")
|
|
|
|
assert data["brightness"] == 3, data
|
|
|
|
|
|
|
|
await hass.services.async_call(
|
2020-08-27 13:56:20 +02:00
|
|
|
"light",
|
|
|
|
"turn_on",
|
|
|
|
{"entity_id": entity.entity_id, "brightness_pct": 2},
|
|
|
|
True,
|
2020-04-14 13:26:18 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
_, data = entity.last_call("turn_on")
|
|
|
|
assert data["brightness"] == 5, data
|
|
|
|
|
|
|
|
await hass.services.async_call(
|
2020-08-27 13:56:20 +02:00
|
|
|
"light",
|
|
|
|
"turn_on",
|
|
|
|
{"entity_id": entity.entity_id, "brightness_pct": 50},
|
|
|
|
True,
|
2020-04-14 13:26:18 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
_, data = entity.last_call("turn_on")
|
|
|
|
assert data["brightness"] == 128, data
|
|
|
|
|
|
|
|
await hass.services.async_call(
|
2020-08-27 13:56:20 +02:00
|
|
|
"light",
|
|
|
|
"turn_on",
|
|
|
|
{"entity_id": entity.entity_id, "brightness_pct": 99},
|
|
|
|
True,
|
2020-04-14 13:26:18 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
_, data = entity.last_call("turn_on")
|
|
|
|
assert data["brightness"] == 252, data
|
|
|
|
|
|
|
|
await hass.services.async_call(
|
|
|
|
"light",
|
|
|
|
"turn_on",
|
|
|
|
{"entity_id": entity.entity_id, "brightness_pct": 100},
|
|
|
|
True,
|
|
|
|
)
|
|
|
|
|
|
|
|
_, data = entity.last_call("turn_on")
|
|
|
|
assert data["brightness"] == 255, data
|
2020-04-26 18:49:41 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_deprecated_base_class(caplog):
|
|
|
|
"""Test deprecated base class."""
|
|
|
|
|
|
|
|
class CustomLight(light.Light):
|
|
|
|
pass
|
|
|
|
|
|
|
|
CustomLight()
|
|
|
|
assert "Light is deprecated, modify CustomLight" in caplog.text
|