Various light test improvements (#34131)

This commit is contained in:
Franck Nijhof
2020-04-13 15:33:04 +02:00
committed by GitHub
parent 4bcceb4078
commit 6d24a65313
7 changed files with 418 additions and 259 deletions
+79 -32
View File
@@ -1,11 +1,25 @@
"""The tests for the demo light component."""
import pytest
from homeassistant.components import light
from homeassistant.components.demo import DOMAIN
from homeassistant.components.light import (
ATTR_BRIGHTNESS,
ATTR_BRIGHTNESS_PCT,
ATTR_COLOR_TEMP,
ATTR_EFFECT,
ATTR_KELVIN,
ATTR_MAX_MIREDS,
ATTR_MIN_MIREDS,
ATTR_RGB_COLOR,
ATTR_WHITE_VALUE,
ATTR_XY_COLOR,
DOMAIN as LIGHT_DOMAIN,
SERVICE_TURN_OFF,
SERVICE_TURN_ON,
)
from homeassistant.const import ATTR_ENTITY_ID, STATE_OFF, STATE_ON
from homeassistant.setup import async_setup_component
from tests.components.light import common
ENTITY_LIGHT = "light.bed_light"
@@ -13,63 +27,96 @@ ENTITY_LIGHT = "light.bed_light"
def setup_comp(hass):
"""Set up demo component."""
hass.loop.run_until_complete(
async_setup_component(hass, light.DOMAIN, {"light": {"platform": "demo"}})
async_setup_component(hass, LIGHT_DOMAIN, {LIGHT_DOMAIN: {"platform": DOMAIN}})
)
async def test_state_attributes(hass):
"""Test light state attributes."""
await common.async_turn_on(hass, ENTITY_LIGHT, xy_color=(0.4, 0.4), brightness=25)
state = hass.states.get(ENTITY_LIGHT)
assert light.is_on(hass, ENTITY_LIGHT)
assert (0.4, 0.4) == state.attributes.get(light.ATTR_XY_COLOR)
assert state.attributes.get(light.ATTR_BRIGHTNESS) == 25
assert state.attributes.get(light.ATTR_RGB_COLOR) == (255, 234, 164)
assert state.attributes.get(light.ATTR_EFFECT) == "rainbow"
await common.async_turn_on(
hass, ENTITY_LIGHT, rgb_color=(251, 253, 255), white_value=254
await hass.services.async_call(
LIGHT_DOMAIN,
SERVICE_TURN_ON,
{ATTR_ENTITY_ID: ENTITY_LIGHT, ATTR_XY_COLOR: (0.4, 0.4), ATTR_BRIGHTNESS: 25},
blocking=True,
)
state = hass.states.get(ENTITY_LIGHT)
assert state.attributes.get(light.ATTR_WHITE_VALUE) == 254
assert state.attributes.get(light.ATTR_RGB_COLOR) == (250, 252, 255)
assert state.attributes.get(light.ATTR_XY_COLOR) == (0.319, 0.326)
await common.async_turn_on(hass, ENTITY_LIGHT, color_temp=400, effect="none")
assert state.state == STATE_ON
assert state.attributes.get(ATTR_XY_COLOR) == (0.4, 0.4)
assert state.attributes.get(ATTR_BRIGHTNESS) == 25
assert state.attributes.get(ATTR_RGB_COLOR) == (255, 234, 164)
assert state.attributes.get(ATTR_EFFECT) == "rainbow"
await hass.services.async_call(
LIGHT_DOMAIN,
SERVICE_TURN_ON,
{
ATTR_ENTITY_ID: ENTITY_LIGHT,
ATTR_RGB_COLOR: (251, 253, 255),
ATTR_WHITE_VALUE: 254,
},
blocking=True,
)
state = hass.states.get(ENTITY_LIGHT)
assert state.attributes.get(light.ATTR_COLOR_TEMP) == 400
assert state.attributes.get(light.ATTR_MIN_MIREDS) == 153
assert state.attributes.get(light.ATTR_MAX_MIREDS) == 500
assert state.attributes.get(light.ATTR_EFFECT) == "none"
await common.async_turn_on(hass, ENTITY_LIGHT, kelvin=3000, brightness_pct=50)
assert state.attributes.get(ATTR_WHITE_VALUE) == 254
assert state.attributes.get(ATTR_RGB_COLOR) == (250, 252, 255)
assert state.attributes.get(ATTR_XY_COLOR) == (0.319, 0.326)
await hass.services.async_call(
LIGHT_DOMAIN,
SERVICE_TURN_ON,
{ATTR_ENTITY_ID: ENTITY_LIGHT, ATTR_EFFECT: "none", ATTR_COLOR_TEMP: 400},
blocking=True,
)
state = hass.states.get(ENTITY_LIGHT)
assert state.attributes.get(light.ATTR_COLOR_TEMP) == 333
assert state.attributes.get(light.ATTR_BRIGHTNESS) == 127
assert state.attributes.get(ATTR_COLOR_TEMP) == 400
assert state.attributes.get(ATTR_MIN_MIREDS) == 153
assert state.attributes.get(ATTR_MAX_MIREDS) == 500
assert state.attributes.get(ATTR_EFFECT) == "none"
await hass.services.async_call(
LIGHT_DOMAIN,
SERVICE_TURN_ON,
{ATTR_ENTITY_ID: ENTITY_LIGHT, ATTR_BRIGHTNESS_PCT: 50, ATTR_KELVIN: 3000},
blocking=True,
)
state = hass.states.get(ENTITY_LIGHT)
assert state.attributes.get(ATTR_COLOR_TEMP) == 333
assert state.attributes.get(ATTR_BRIGHTNESS) == 127
async def test_turn_off(hass):
"""Test light turn off method."""
await hass.services.async_call(
"light", "turn_on", {"entity_id": ENTITY_LIGHT}, blocking=True
LIGHT_DOMAIN, SERVICE_TURN_ON, {ATTR_ENTITY_ID: ENTITY_LIGHT}, blocking=True
)
assert light.is_on(hass, ENTITY_LIGHT)
state = hass.states.get(ENTITY_LIGHT)
assert state.state == STATE_ON
await hass.services.async_call(
"light", "turn_off", {"entity_id": ENTITY_LIGHT}, blocking=True
LIGHT_DOMAIN, SERVICE_TURN_OFF, {ATTR_ENTITY_ID: ENTITY_LIGHT}, blocking=True
)
assert not light.is_on(hass, ENTITY_LIGHT)
state = hass.states.get(ENTITY_LIGHT)
assert state.state == STATE_OFF
async def test_turn_off_without_entity_id(hass):
"""Test light turn off all lights."""
await hass.services.async_call(
"light", "turn_on", {"entity_id": "all"}, blocking=True
LIGHT_DOMAIN, SERVICE_TURN_ON, {ATTR_ENTITY_ID: "all"}, blocking=True
)
assert light.is_on(hass, ENTITY_LIGHT)
state = hass.states.get(ENTITY_LIGHT)
assert state.state == STATE_ON
await hass.services.async_call(
"light", "turn_off", {"entity_id": "all"}, blocking=True
LIGHT_DOMAIN, SERVICE_TURN_OFF, {ATTR_ENTITY_ID: "all"}, blocking=True
)
assert not light.is_on(hass, ENTITY_LIGHT)
state = hass.states.get(ENTITY_LIGHT)
assert state.state == STATE_OFF