mirror of
https://github.com/home-assistant/core.git
synced 2025-04-26 10:17:51 +00:00
Rewrite light component tests to async pytest tests (#40589)
This commit is contained in:
parent
0c12af347e
commit
e30acfbfee
@ -2,9 +2,9 @@
|
|||||||
# pylint: disable=protected-access
|
# pylint: disable=protected-access
|
||||||
from io import StringIO
|
from io import StringIO
|
||||||
import os
|
import os
|
||||||
import unittest
|
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
import voluptuous as vol
|
||||||
|
|
||||||
from homeassistant import core
|
from homeassistant import core
|
||||||
from homeassistant.components import light
|
from homeassistant.components import light
|
||||||
@ -18,45 +18,37 @@ from homeassistant.const import (
|
|||||||
STATE_ON,
|
STATE_ON,
|
||||||
)
|
)
|
||||||
from homeassistant.exceptions import Unauthorized
|
from homeassistant.exceptions import Unauthorized
|
||||||
from homeassistant.setup import async_setup_component, setup_component
|
from homeassistant.setup import async_setup_component
|
||||||
|
|
||||||
import tests.async_mock as mock
|
import tests.async_mock as mock
|
||||||
from tests.common import get_test_home_assistant, mock_service, mock_storage
|
from tests.common import async_mock_service
|
||||||
from tests.components.light import common
|
from tests.components.light import common
|
||||||
|
|
||||||
|
|
||||||
class TestLight(unittest.TestCase):
|
@pytest.fixture
|
||||||
"""Test the light module."""
|
def mock_storage(hass, hass_storage):
|
||||||
|
"""Clean up user light files at the end."""
|
||||||
# pylint: disable=invalid-name
|
yield
|
||||||
def setUp(self):
|
user_light_file = hass.config.path(light.LIGHT_PROFILES_FILE)
|
||||||
"""Set up things to be run when tests are started."""
|
|
||||||
self.hass = get_test_home_assistant()
|
|
||||||
self.addCleanup(self.tear_down_cleanup)
|
|
||||||
|
|
||||||
def tear_down_cleanup(self):
|
|
||||||
"""Stop everything that was started."""
|
|
||||||
self.hass.stop()
|
|
||||||
|
|
||||||
user_light_file = self.hass.config.path(light.LIGHT_PROFILES_FILE)
|
|
||||||
|
|
||||||
if os.path.isfile(user_light_file):
|
if os.path.isfile(user_light_file):
|
||||||
os.remove(user_light_file)
|
os.remove(user_light_file)
|
||||||
|
|
||||||
def test_methods(self):
|
|
||||||
|
async def test_methods(hass):
|
||||||
"""Test if methods call the services as expected."""
|
"""Test if methods call the services as expected."""
|
||||||
# Test is_on
|
# Test is_on
|
||||||
self.hass.states.set("light.test", STATE_ON)
|
hass.states.async_set("light.test", STATE_ON)
|
||||||
assert light.is_on(self.hass, "light.test")
|
assert light.is_on(hass, "light.test")
|
||||||
|
|
||||||
self.hass.states.set("light.test", STATE_OFF)
|
hass.states.async_set("light.test", STATE_OFF)
|
||||||
assert not light.is_on(self.hass, "light.test")
|
assert not light.is_on(hass, "light.test")
|
||||||
|
|
||||||
# Test turn_on
|
# Test turn_on
|
||||||
turn_on_calls = mock_service(self.hass, light.DOMAIN, SERVICE_TURN_ON)
|
turn_on_calls = async_mock_service(hass, light.DOMAIN, SERVICE_TURN_ON)
|
||||||
|
|
||||||
common.turn_on(
|
await common.async_turn_on(
|
||||||
self.hass,
|
hass,
|
||||||
entity_id="entity_id_val",
|
entity_id="entity_id_val",
|
||||||
transition="transition_val",
|
transition="transition_val",
|
||||||
brightness="brightness_val",
|
brightness="brightness_val",
|
||||||
@ -67,139 +59,139 @@ class TestLight(unittest.TestCase):
|
|||||||
white_value="white_val",
|
white_value="white_val",
|
||||||
)
|
)
|
||||||
|
|
||||||
self.hass.block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
assert 1 == len(turn_on_calls)
|
assert len(turn_on_calls) == 1
|
||||||
call = turn_on_calls[-1]
|
call = turn_on_calls[-1]
|
||||||
|
|
||||||
assert light.DOMAIN == call.domain
|
assert call.domain == light.DOMAIN
|
||||||
assert SERVICE_TURN_ON == call.service
|
assert call.service == SERVICE_TURN_ON
|
||||||
assert "entity_id_val" == call.data.get(ATTR_ENTITY_ID)
|
assert call.data.get(ATTR_ENTITY_ID) == "entity_id_val"
|
||||||
assert "transition_val" == call.data.get(light.ATTR_TRANSITION)
|
assert call.data.get(light.ATTR_TRANSITION) == "transition_val"
|
||||||
assert "brightness_val" == call.data.get(light.ATTR_BRIGHTNESS)
|
assert call.data.get(light.ATTR_BRIGHTNESS) == "brightness_val"
|
||||||
assert "rgb_color_val" == call.data.get(light.ATTR_RGB_COLOR)
|
assert call.data.get(light.ATTR_RGB_COLOR) == "rgb_color_val"
|
||||||
assert "xy_color_val" == call.data.get(light.ATTR_XY_COLOR)
|
assert call.data.get(light.ATTR_XY_COLOR) == "xy_color_val"
|
||||||
assert "profile_val" == call.data.get(light.ATTR_PROFILE)
|
assert call.data.get(light.ATTR_PROFILE) == "profile_val"
|
||||||
assert "color_name_val" == call.data.get(light.ATTR_COLOR_NAME)
|
assert call.data.get(light.ATTR_COLOR_NAME) == "color_name_val"
|
||||||
assert "white_val" == call.data.get(light.ATTR_WHITE_VALUE)
|
assert call.data.get(light.ATTR_WHITE_VALUE) == "white_val"
|
||||||
|
|
||||||
# Test turn_off
|
# Test turn_off
|
||||||
turn_off_calls = mock_service(self.hass, light.DOMAIN, SERVICE_TURN_OFF)
|
turn_off_calls = async_mock_service(hass, light.DOMAIN, SERVICE_TURN_OFF)
|
||||||
|
|
||||||
common.turn_off(
|
await common.async_turn_off(
|
||||||
self.hass, entity_id="entity_id_val", transition="transition_val"
|
hass, entity_id="entity_id_val", transition="transition_val"
|
||||||
)
|
)
|
||||||
|
|
||||||
self.hass.block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
assert 1 == len(turn_off_calls)
|
assert len(turn_off_calls) == 1
|
||||||
call = turn_off_calls[-1]
|
call = turn_off_calls[-1]
|
||||||
|
|
||||||
assert light.DOMAIN == call.domain
|
assert light.DOMAIN == call.domain
|
||||||
assert SERVICE_TURN_OFF == call.service
|
assert SERVICE_TURN_OFF == call.service
|
||||||
assert "entity_id_val" == call.data[ATTR_ENTITY_ID]
|
assert call.data[ATTR_ENTITY_ID] == "entity_id_val"
|
||||||
assert "transition_val" == call.data[light.ATTR_TRANSITION]
|
assert call.data[light.ATTR_TRANSITION] == "transition_val"
|
||||||
|
|
||||||
# Test toggle
|
# Test toggle
|
||||||
toggle_calls = mock_service(self.hass, light.DOMAIN, SERVICE_TOGGLE)
|
toggle_calls = async_mock_service(hass, light.DOMAIN, SERVICE_TOGGLE)
|
||||||
|
|
||||||
common.toggle(self.hass, entity_id="entity_id_val", transition="transition_val")
|
await common.async_toggle(
|
||||||
|
hass, entity_id="entity_id_val", transition="transition_val"
|
||||||
|
)
|
||||||
|
|
||||||
self.hass.block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
assert 1 == len(toggle_calls)
|
assert len(toggle_calls) == 1
|
||||||
call = toggle_calls[-1]
|
call = toggle_calls[-1]
|
||||||
|
|
||||||
assert light.DOMAIN == call.domain
|
assert call.domain == light.DOMAIN
|
||||||
assert SERVICE_TOGGLE == call.service
|
assert call.service == SERVICE_TOGGLE
|
||||||
assert "entity_id_val" == call.data[ATTR_ENTITY_ID]
|
assert call.data[ATTR_ENTITY_ID] == "entity_id_val"
|
||||||
assert "transition_val" == call.data[light.ATTR_TRANSITION]
|
assert call.data[light.ATTR_TRANSITION] == "transition_val"
|
||||||
|
|
||||||
def test_services(self):
|
|
||||||
|
async def test_services(hass):
|
||||||
"""Test the provided services."""
|
"""Test the provided services."""
|
||||||
platform = getattr(self.hass.components, "test.light")
|
platform = getattr(hass.components, "test.light")
|
||||||
|
|
||||||
platform.init()
|
platform.init()
|
||||||
assert setup_component(
|
assert await async_setup_component(
|
||||||
self.hass, light.DOMAIN, {light.DOMAIN: {CONF_PLATFORM: "test"}}
|
hass, light.DOMAIN, {light.DOMAIN: {CONF_PLATFORM: "test"}}
|
||||||
)
|
)
|
||||||
self.hass.block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
ent1, ent2, ent3 = platform.ENTITIES
|
ent1, ent2, ent3 = platform.ENTITIES
|
||||||
|
|
||||||
# Test init
|
# Test init
|
||||||
assert light.is_on(self.hass, ent1.entity_id)
|
assert light.is_on(hass, ent1.entity_id)
|
||||||
assert not light.is_on(self.hass, ent2.entity_id)
|
assert not light.is_on(hass, ent2.entity_id)
|
||||||
assert not light.is_on(self.hass, ent3.entity_id)
|
assert not light.is_on(hass, ent3.entity_id)
|
||||||
|
|
||||||
# Test basic turn_on, turn_off, toggle services
|
# Test basic turn_on, turn_off, toggle services
|
||||||
common.turn_off(self.hass, entity_id=ent1.entity_id)
|
await common.async_turn_off(hass, entity_id=ent1.entity_id)
|
||||||
common.turn_on(self.hass, entity_id=ent2.entity_id)
|
await common.async_turn_on(hass, entity_id=ent2.entity_id)
|
||||||
|
|
||||||
self.hass.block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
assert not light.is_on(self.hass, ent1.entity_id)
|
assert not light.is_on(hass, ent1.entity_id)
|
||||||
assert light.is_on(self.hass, ent2.entity_id)
|
assert light.is_on(hass, ent2.entity_id)
|
||||||
|
|
||||||
# turn on all lights
|
# turn on all lights
|
||||||
common.turn_on(self.hass)
|
await common.async_turn_on(hass)
|
||||||
|
|
||||||
self.hass.block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
assert light.is_on(self.hass, ent1.entity_id)
|
assert light.is_on(hass, ent1.entity_id)
|
||||||
assert light.is_on(self.hass, ent2.entity_id)
|
assert light.is_on(hass, ent2.entity_id)
|
||||||
assert light.is_on(self.hass, ent3.entity_id)
|
assert light.is_on(hass, ent3.entity_id)
|
||||||
|
|
||||||
# turn off all lights
|
# turn off all lights
|
||||||
common.turn_off(self.hass)
|
await common.async_turn_off(hass)
|
||||||
|
|
||||||
self.hass.block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
assert not light.is_on(self.hass, ent1.entity_id)
|
assert not light.is_on(hass, ent1.entity_id)
|
||||||
assert not light.is_on(self.hass, ent2.entity_id)
|
assert not light.is_on(hass, ent2.entity_id)
|
||||||
assert not light.is_on(self.hass, ent3.entity_id)
|
assert not light.is_on(hass, ent3.entity_id)
|
||||||
|
|
||||||
# turn off all lights by setting brightness to 0
|
# turn off all lights by setting brightness to 0
|
||||||
common.turn_on(self.hass)
|
await common.async_turn_on(hass)
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
self.hass.block_till_done()
|
await common.async_turn_on(hass, brightness=0)
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
common.turn_on(self.hass, brightness=0)
|
assert not light.is_on(hass, ent1.entity_id)
|
||||||
|
assert not light.is_on(hass, ent2.entity_id)
|
||||||
self.hass.block_till_done()
|
assert not light.is_on(hass, ent3.entity_id)
|
||||||
|
|
||||||
assert not light.is_on(self.hass, ent1.entity_id)
|
|
||||||
assert not light.is_on(self.hass, ent2.entity_id)
|
|
||||||
assert not light.is_on(self.hass, ent3.entity_id)
|
|
||||||
|
|
||||||
# toggle all lights
|
# toggle all lights
|
||||||
common.toggle(self.hass)
|
await common.async_toggle(hass)
|
||||||
|
|
||||||
self.hass.block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
assert light.is_on(self.hass, ent1.entity_id)
|
assert light.is_on(hass, ent1.entity_id)
|
||||||
assert light.is_on(self.hass, ent2.entity_id)
|
assert light.is_on(hass, ent2.entity_id)
|
||||||
assert light.is_on(self.hass, ent3.entity_id)
|
assert light.is_on(hass, ent3.entity_id)
|
||||||
|
|
||||||
# toggle all lights
|
# toggle all lights
|
||||||
common.toggle(self.hass)
|
await common.async_toggle(hass)
|
||||||
|
|
||||||
self.hass.block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
assert not light.is_on(self.hass, ent1.entity_id)
|
assert not light.is_on(hass, ent1.entity_id)
|
||||||
assert not light.is_on(self.hass, ent2.entity_id)
|
assert not light.is_on(hass, ent2.entity_id)
|
||||||
assert not light.is_on(self.hass, ent3.entity_id)
|
assert not light.is_on(hass, ent3.entity_id)
|
||||||
|
|
||||||
# Ensure all attributes process correctly
|
# Ensure all attributes process correctly
|
||||||
common.turn_on(
|
await common.async_turn_on(
|
||||||
self.hass, ent1.entity_id, transition=10, brightness=20, color_name="blue"
|
hass, ent1.entity_id, transition=10, brightness=20, color_name="blue"
|
||||||
)
|
)
|
||||||
common.turn_on(
|
await common.async_turn_on(
|
||||||
self.hass, ent2.entity_id, rgb_color=(255, 255, 255), white_value=255
|
hass, ent2.entity_id, rgb_color=(255, 255, 255), white_value=255
|
||||||
)
|
)
|
||||||
common.turn_on(self.hass, ent3.entity_id, xy_color=(0.4, 0.6))
|
await common.async_turn_on(hass, ent3.entity_id, xy_color=(0.4, 0.6))
|
||||||
|
await hass.async_block_till_done()
|
||||||
self.hass.block_till_done()
|
|
||||||
|
|
||||||
_, data = ent1.last_call("turn_on")
|
_, data = ent1.last_call("turn_on")
|
||||||
assert {
|
assert {
|
||||||
@ -215,23 +207,23 @@ class TestLight(unittest.TestCase):
|
|||||||
assert {light.ATTR_HS_COLOR: (71.059, 100)} == data
|
assert {light.ATTR_HS_COLOR: (71.059, 100)} == data
|
||||||
|
|
||||||
# Ensure attributes are filtered when light is turned off
|
# Ensure attributes are filtered when light is turned off
|
||||||
common.turn_on(
|
await common.async_turn_on(
|
||||||
self.hass, ent1.entity_id, transition=10, brightness=0, color_name="blue"
|
hass, ent1.entity_id, transition=10, brightness=0, color_name="blue"
|
||||||
)
|
)
|
||||||
common.turn_on(
|
await common.async_turn_on(
|
||||||
self.hass,
|
hass,
|
||||||
ent2.entity_id,
|
ent2.entity_id,
|
||||||
brightness=0,
|
brightness=0,
|
||||||
rgb_color=(255, 255, 255),
|
rgb_color=(255, 255, 255),
|
||||||
white_value=0,
|
white_value=0,
|
||||||
)
|
)
|
||||||
common.turn_on(self.hass, ent3.entity_id, brightness=0, xy_color=(0.4, 0.6))
|
await common.async_turn_on(hass, ent3.entity_id, brightness=0, xy_color=(0.4, 0.6))
|
||||||
|
|
||||||
self.hass.block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
assert not light.is_on(self.hass, ent1.entity_id)
|
assert not light.is_on(hass, ent1.entity_id)
|
||||||
assert not light.is_on(self.hass, ent2.entity_id)
|
assert not light.is_on(hass, ent2.entity_id)
|
||||||
assert not light.is_on(self.hass, ent3.entity_id)
|
assert not light.is_on(hass, ent3.entity_id)
|
||||||
|
|
||||||
_, data = ent1.last_call("turn_off")
|
_, data = ent1.last_call("turn_off")
|
||||||
assert {light.ATTR_TRANSITION: 10} == data
|
assert {light.ATTR_TRANSITION: 10} == data
|
||||||
@ -246,13 +238,13 @@ class TestLight(unittest.TestCase):
|
|||||||
prof_name, prof_h, prof_s, prof_bri, prof_t = "relax", 35.932, 69.412, 144, 0
|
prof_name, prof_h, prof_s, prof_bri, prof_t = "relax", 35.932, 69.412, 144, 0
|
||||||
|
|
||||||
# Test light profiles
|
# Test light profiles
|
||||||
common.turn_on(self.hass, ent1.entity_id, profile=prof_name)
|
await common.async_turn_on(hass, ent1.entity_id, profile=prof_name)
|
||||||
# Specify a profile and a brightness attribute to overwrite it
|
# Specify a profile and a brightness attribute to overwrite it
|
||||||
common.turn_on(
|
await common.async_turn_on(
|
||||||
self.hass, ent2.entity_id, profile=prof_name, brightness=100, transition=1
|
hass, ent2.entity_id, profile=prof_name, brightness=100, transition=1
|
||||||
)
|
)
|
||||||
|
|
||||||
self.hass.block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
_, data = ent1.last_call("turn_on")
|
_, data = ent1.last_call("turn_on")
|
||||||
assert {
|
assert {
|
||||||
@ -269,8 +261,10 @@ class TestLight(unittest.TestCase):
|
|||||||
} == data
|
} == data
|
||||||
|
|
||||||
# Test toggle with parameters
|
# Test toggle with parameters
|
||||||
common.toggle(self.hass, ent3.entity_id, profile=prof_name, brightness_pct=100)
|
await common.async_toggle(
|
||||||
self.hass.block_till_done()
|
hass, ent3.entity_id, profile=prof_name, brightness_pct=100
|
||||||
|
)
|
||||||
|
await hass.async_block_till_done()
|
||||||
_, data = ent3.last_call("turn_on")
|
_, data = ent3.last_call("turn_on")
|
||||||
assert {
|
assert {
|
||||||
light.ATTR_BRIGHTNESS: 255,
|
light.ATTR_BRIGHTNESS: 255,
|
||||||
@ -279,12 +273,14 @@ class TestLight(unittest.TestCase):
|
|||||||
} == data
|
} == data
|
||||||
|
|
||||||
# Test bad data
|
# Test bad data
|
||||||
common.turn_on(self.hass)
|
await common.async_turn_on(hass)
|
||||||
common.turn_on(self.hass, ent1.entity_id, profile="nonexisting")
|
await common.async_turn_on(hass, ent1.entity_id, profile="nonexisting")
|
||||||
common.turn_on(self.hass, ent2.entity_id, xy_color=["bla-di-bla", 5])
|
with pytest.raises(vol.MultipleInvalid):
|
||||||
common.turn_on(self.hass, ent3.entity_id, rgb_color=[255, None, 2])
|
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])
|
||||||
|
|
||||||
self.hass.block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
_, data = ent1.last_call("turn_on")
|
_, data = ent1.last_call("turn_on")
|
||||||
assert {} == data
|
assert {} == data
|
||||||
@ -296,13 +292,16 @@ class TestLight(unittest.TestCase):
|
|||||||
assert {} == data
|
assert {} == data
|
||||||
|
|
||||||
# faulty attributes will not trigger a service call
|
# faulty attributes will not trigger a service call
|
||||||
common.turn_on(
|
with pytest.raises(vol.MultipleInvalid):
|
||||||
self.hass, ent1.entity_id, profile=prof_name, brightness="bright"
|
await common.async_turn_on(
|
||||||
|
hass, ent1.entity_id, profile=prof_name, brightness="bright"
|
||||||
)
|
)
|
||||||
common.turn_on(self.hass, ent1.entity_id, rgb_color="yellowish")
|
with pytest.raises(vol.MultipleInvalid):
|
||||||
common.turn_on(self.hass, ent2.entity_id, white_value="high")
|
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")
|
||||||
|
|
||||||
self.hass.block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
_, data = ent1.last_call("turn_on")
|
_, data = ent1.last_call("turn_on")
|
||||||
assert {} == data
|
assert {} == data
|
||||||
@ -310,110 +309,114 @@ class TestLight(unittest.TestCase):
|
|||||||
_, data = ent2.last_call("turn_on")
|
_, data = ent2.last_call("turn_on")
|
||||||
assert {} == data
|
assert {} == data
|
||||||
|
|
||||||
def test_broken_light_profiles(self):
|
|
||||||
|
async def test_broken_light_profiles(hass, mock_storage):
|
||||||
"""Test light profiles."""
|
"""Test light profiles."""
|
||||||
platform = getattr(self.hass.components, "test.light")
|
platform = getattr(hass.components, "test.light")
|
||||||
platform.init()
|
platform.init()
|
||||||
|
|
||||||
user_light_file = self.hass.config.path(light.LIGHT_PROFILES_FILE)
|
user_light_file = hass.config.path(light.LIGHT_PROFILES_FILE)
|
||||||
|
|
||||||
# Setup a wrong light file
|
# Setup a wrong light file
|
||||||
with open(user_light_file, "w") as user_file:
|
with open(user_light_file, "w") as user_file:
|
||||||
user_file.write("id,x,y,brightness,transition\n")
|
user_file.write("id,x,y,brightness,transition\n")
|
||||||
user_file.write("I,WILL,NOT,WORK,EVER\n")
|
user_file.write("I,WILL,NOT,WORK,EVER\n")
|
||||||
|
|
||||||
assert not setup_component(
|
assert not await async_setup_component(
|
||||||
self.hass, light.DOMAIN, {light.DOMAIN: {CONF_PLATFORM: "test"}}
|
hass, light.DOMAIN, {light.DOMAIN: {CONF_PLATFORM: "test"}}
|
||||||
)
|
)
|
||||||
|
|
||||||
def test_light_profiles(self):
|
|
||||||
|
async def test_light_profiles(hass, mock_storage):
|
||||||
"""Test light profiles."""
|
"""Test light profiles."""
|
||||||
platform = getattr(self.hass.components, "test.light")
|
platform = getattr(hass.components, "test.light")
|
||||||
platform.init()
|
platform.init()
|
||||||
|
|
||||||
user_light_file = self.hass.config.path(light.LIGHT_PROFILES_FILE)
|
user_light_file = hass.config.path(light.LIGHT_PROFILES_FILE)
|
||||||
|
|
||||||
with open(user_light_file, "w") as user_file:
|
with open(user_light_file, "w") as user_file:
|
||||||
user_file.write("id,x,y,brightness\n")
|
user_file.write("id,x,y,brightness\n")
|
||||||
user_file.write("test,.4,.6,100\n")
|
user_file.write("test,.4,.6,100\n")
|
||||||
user_file.write("test_off,0,0,0\n")
|
user_file.write("test_off,0,0,0\n")
|
||||||
|
|
||||||
assert setup_component(
|
assert await async_setup_component(
|
||||||
self.hass, light.DOMAIN, {light.DOMAIN: {CONF_PLATFORM: "test"}}
|
hass, light.DOMAIN, {light.DOMAIN: {CONF_PLATFORM: "test"}}
|
||||||
)
|
)
|
||||||
self.hass.block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
ent1, _, _ = platform.ENTITIES
|
ent1, _, _ = platform.ENTITIES
|
||||||
|
|
||||||
common.turn_on(self.hass, ent1.entity_id, profile="test")
|
await common.async_turn_on(hass, ent1.entity_id, profile="test")
|
||||||
|
|
||||||
self.hass.block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
_, data = ent1.last_call("turn_on")
|
_, data = ent1.last_call("turn_on")
|
||||||
|
|
||||||
assert light.is_on(self.hass, ent1.entity_id)
|
assert light.is_on(hass, ent1.entity_id)
|
||||||
assert {
|
assert data == {
|
||||||
light.ATTR_HS_COLOR: (71.059, 100),
|
light.ATTR_HS_COLOR: (71.059, 100),
|
||||||
light.ATTR_BRIGHTNESS: 100,
|
light.ATTR_BRIGHTNESS: 100,
|
||||||
light.ATTR_TRANSITION: 0,
|
light.ATTR_TRANSITION: 0,
|
||||||
} == data
|
}
|
||||||
|
|
||||||
common.turn_on(self.hass, ent1.entity_id, profile="test_off")
|
await common.async_turn_on(hass, ent1.entity_id, profile="test_off")
|
||||||
|
|
||||||
self.hass.block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
_, data = ent1.last_call("turn_off")
|
_, data = ent1.last_call("turn_off")
|
||||||
|
|
||||||
assert not light.is_on(self.hass, ent1.entity_id)
|
assert not light.is_on(hass, ent1.entity_id)
|
||||||
assert {light.ATTR_TRANSITION: 0} == data
|
assert data == {light.ATTR_TRANSITION: 0}
|
||||||
|
|
||||||
def test_light_profiles_with_transition(self):
|
|
||||||
|
async def test_light_profiles_with_transition(hass, mock_storage):
|
||||||
"""Test light profiles with transition."""
|
"""Test light profiles with transition."""
|
||||||
platform = getattr(self.hass.components, "test.light")
|
platform = getattr(hass.components, "test.light")
|
||||||
platform.init()
|
platform.init()
|
||||||
|
|
||||||
user_light_file = self.hass.config.path(light.LIGHT_PROFILES_FILE)
|
user_light_file = hass.config.path(light.LIGHT_PROFILES_FILE)
|
||||||
|
|
||||||
with open(user_light_file, "w") as user_file:
|
with open(user_light_file, "w") as user_file:
|
||||||
user_file.write("id,x,y,brightness,transition\n")
|
user_file.write("id,x,y,brightness,transition\n")
|
||||||
user_file.write("test,.4,.6,100,2\n")
|
user_file.write("test,.4,.6,100,2\n")
|
||||||
user_file.write("test_off,0,0,0,0\n")
|
user_file.write("test_off,0,0,0,0\n")
|
||||||
|
|
||||||
assert setup_component(
|
assert await async_setup_component(
|
||||||
self.hass, light.DOMAIN, {light.DOMAIN: {CONF_PLATFORM: "test"}}
|
hass, light.DOMAIN, {light.DOMAIN: {CONF_PLATFORM: "test"}}
|
||||||
)
|
)
|
||||||
self.hass.block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
ent1, _, _ = platform.ENTITIES
|
ent1, _, _ = platform.ENTITIES
|
||||||
|
|
||||||
common.turn_on(self.hass, ent1.entity_id, profile="test")
|
await common.async_turn_on(hass, ent1.entity_id, profile="test")
|
||||||
|
|
||||||
self.hass.block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
_, data = ent1.last_call("turn_on")
|
_, data = ent1.last_call("turn_on")
|
||||||
|
|
||||||
assert light.is_on(self.hass, ent1.entity_id)
|
assert light.is_on(hass, ent1.entity_id)
|
||||||
assert {
|
assert data == {
|
||||||
light.ATTR_HS_COLOR: (71.059, 100),
|
light.ATTR_HS_COLOR: (71.059, 100),
|
||||||
light.ATTR_BRIGHTNESS: 100,
|
light.ATTR_BRIGHTNESS: 100,
|
||||||
light.ATTR_TRANSITION: 2,
|
light.ATTR_TRANSITION: 2,
|
||||||
} == data
|
}
|
||||||
|
|
||||||
common.turn_on(self.hass, ent1.entity_id, profile="test_off")
|
await common.async_turn_on(hass, ent1.entity_id, profile="test_off")
|
||||||
|
|
||||||
self.hass.block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
_, data = ent1.last_call("turn_off")
|
_, data = ent1.last_call("turn_off")
|
||||||
|
|
||||||
assert not light.is_on(self.hass, ent1.entity_id)
|
assert not light.is_on(hass, ent1.entity_id)
|
||||||
assert {light.ATTR_TRANSITION: 0} == data
|
assert data == {light.ATTR_TRANSITION: 0}
|
||||||
|
|
||||||
def test_default_profiles_group(self):
|
|
||||||
|
async def test_default_profiles_group(hass, mock_storage):
|
||||||
"""Test default turn-on light profile for all lights."""
|
"""Test default turn-on light profile for all lights."""
|
||||||
platform = getattr(self.hass.components, "test.light")
|
platform = getattr(hass.components, "test.light")
|
||||||
platform.init()
|
platform.init()
|
||||||
|
|
||||||
user_light_file = self.hass.config.path(light.LIGHT_PROFILES_FILE)
|
user_light_file = hass.config.path(light.LIGHT_PROFILES_FILE)
|
||||||
real_isfile = os.path.isfile
|
real_isfile = os.path.isfile
|
||||||
real_open = open
|
real_open = open
|
||||||
|
|
||||||
@ -427,33 +430,32 @@ class TestLight(unittest.TestCase):
|
|||||||
return StringIO(profile_data)
|
return StringIO(profile_data)
|
||||||
return real_open(path, *args, **kwargs)
|
return real_open(path, *args, **kwargs)
|
||||||
|
|
||||||
profile_data = (
|
profile_data = "id,x,y,brightness,transition\ngroup.all_lights.default,.4,.6,99,2\n"
|
||||||
"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(
|
with mock.patch("os.path.isfile", side_effect=_mock_isfile), mock.patch(
|
||||||
"builtins.open", side_effect=_mock_open
|
"builtins.open", side_effect=_mock_open
|
||||||
), mock_storage():
|
):
|
||||||
assert setup_component(
|
assert await async_setup_component(
|
||||||
self.hass, light.DOMAIN, {light.DOMAIN: {CONF_PLATFORM: "test"}}
|
hass, light.DOMAIN, {light.DOMAIN: {CONF_PLATFORM: "test"}}
|
||||||
)
|
)
|
||||||
self.hass.block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
ent, _, _ = platform.ENTITIES
|
ent, _, _ = platform.ENTITIES
|
||||||
common.turn_on(self.hass, ent.entity_id)
|
await common.async_turn_on(hass, ent.entity_id)
|
||||||
self.hass.block_till_done()
|
await hass.async_block_till_done()
|
||||||
_, data = ent.last_call("turn_on")
|
_, data = ent.last_call("turn_on")
|
||||||
assert {
|
assert data == {
|
||||||
light.ATTR_HS_COLOR: (71.059, 100),
|
light.ATTR_HS_COLOR: (71.059, 100),
|
||||||
light.ATTR_BRIGHTNESS: 99,
|
light.ATTR_BRIGHTNESS: 99,
|
||||||
light.ATTR_TRANSITION: 2,
|
light.ATTR_TRANSITION: 2,
|
||||||
} == data
|
}
|
||||||
|
|
||||||
def test_default_profiles_light(self):
|
|
||||||
|
async def test_default_profiles_light(hass, mock_storage):
|
||||||
"""Test default turn-on light profile for a specific light."""
|
"""Test default turn-on light profile for a specific light."""
|
||||||
platform = getattr(self.hass.components, "test.light")
|
platform = getattr(hass.components, "test.light")
|
||||||
platform.init()
|
platform.init()
|
||||||
|
|
||||||
user_light_file = self.hass.config.path(light.LIGHT_PROFILES_FILE)
|
user_light_file = hass.config.path(light.LIGHT_PROFILES_FILE)
|
||||||
real_isfile = os.path.isfile
|
real_isfile = os.path.isfile
|
||||||
real_open = open
|
real_open = open
|
||||||
|
|
||||||
@ -474,23 +476,21 @@ class TestLight(unittest.TestCase):
|
|||||||
)
|
)
|
||||||
with mock.patch("os.path.isfile", side_effect=_mock_isfile), mock.patch(
|
with mock.patch("os.path.isfile", side_effect=_mock_isfile), mock.patch(
|
||||||
"builtins.open", side_effect=_mock_open
|
"builtins.open", side_effect=_mock_open
|
||||||
), mock_storage():
|
):
|
||||||
assert setup_component(
|
assert await async_setup_component(
|
||||||
self.hass, light.DOMAIN, {light.DOMAIN: {CONF_PLATFORM: "test"}}
|
hass, light.DOMAIN, {light.DOMAIN: {CONF_PLATFORM: "test"}}
|
||||||
)
|
)
|
||||||
self.hass.block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
dev = next(
|
dev = next(filter(lambda x: x.entity_id == "light.ceiling_2", platform.ENTITIES))
|
||||||
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()
|
||||||
common.turn_on(self.hass, dev.entity_id)
|
|
||||||
self.hass.block_till_done()
|
|
||||||
_, data = dev.last_call("turn_on")
|
_, data = dev.last_call("turn_on")
|
||||||
assert {
|
assert data == {
|
||||||
light.ATTR_HS_COLOR: (50.353, 100),
|
light.ATTR_HS_COLOR: (50.353, 100),
|
||||||
light.ATTR_BRIGHTNESS: 100,
|
light.ATTR_BRIGHTNESS: 100,
|
||||||
light.ATTR_TRANSITION: 3,
|
light.ATTR_TRANSITION: 3,
|
||||||
} == data
|
}
|
||||||
|
|
||||||
|
|
||||||
async def test_light_context(hass, hass_admin_user):
|
async def test_light_context(hass, hass_admin_user):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user