This commit is contained in:
Paulus Schoutsen committed 2019-07-31 12:25:30 -07:00
1 parent da05dfe708
commit 4de97abc3a
2676 files changed
+163166 -140084

No files matched your search

+84 -32
View File
@@ -4,34 +4,90 @@ All containing methods are legacy helpers that should not be used by new
components. Instead call the service directly.
"""
from homeassistant.components.light import (
ATTR_BRIGHTNESS, ATTR_BRIGHTNESS_PCT, ATTR_COLOR_NAME, ATTR_COLOR_TEMP,
ATTR_EFFECT, ATTR_FLASH, ATTR_HS_COLOR, ATTR_KELVIN, ATTR_PROFILE,
ATTR_RGB_COLOR, ATTR_TRANSITION, ATTR_WHITE_VALUE, ATTR_XY_COLOR, DOMAIN)
ATTR_BRIGHTNESS,
ATTR_BRIGHTNESS_PCT,
ATTR_COLOR_NAME,
ATTR_COLOR_TEMP,
ATTR_EFFECT,
ATTR_FLASH,
ATTR_HS_COLOR,
ATTR_KELVIN,
ATTR_PROFILE,
ATTR_RGB_COLOR,
ATTR_TRANSITION,
ATTR_WHITE_VALUE,
ATTR_XY_COLOR,
DOMAIN,
)
from homeassistant.const import (
ATTR_ENTITY_ID, SERVICE_TOGGLE, SERVICE_TURN_OFF, SERVICE_TURN_ON)
ATTR_ENTITY_ID,
SERVICE_TOGGLE,
SERVICE_TURN_OFF,
SERVICE_TURN_ON,
)
from homeassistant.loader import bind_hass
@bind_hass
def turn_on(hass, entity_id=None, transition=None, brightness=None,
brightness_pct=None, rgb_color=None, xy_color=None, hs_color=None,
color_temp=None, kelvin=None, white_value=None,
profile=None, flash=None, effect=None, color_name=None):
def turn_on(
hass,
entity_id=None,
transition=None,
brightness=None,
brightness_pct=None,
rgb_color=None,
xy_color=None,
hs_color=None,
color_temp=None,
kelvin=None,
white_value=None,
profile=None,
flash=None,
effect=None,
color_name=None,
):
"""Turn all or specified light on."""
hass.add_job(
async_turn_on, hass, entity_id, transition, brightness, brightness_pct,
rgb_color, xy_color, hs_color, color_temp, kelvin, white_value,
profile, flash, effect, color_name)
async_turn_on,
hass,
entity_id,
transition,
brightness,
brightness_pct,
rgb_color,
xy_color,
hs_color,
color_temp,
kelvin,
white_value,
profile,
flash,
effect,
color_name,
)
async def async_turn_on(hass, entity_id=None, transition=None, brightness=None,
brightness_pct=None, rgb_color=None, xy_color=None,
hs_color=None, color_temp=None, kelvin=None,
white_value=None, profile=None, flash=None,
effect=None, color_name=None):
async def async_turn_on(
hass,
entity_id=None,
transition=None,
brightness=None,
brightness_pct=None,
rgb_color=None,
xy_color=None,
hs_color=None,
color_temp=None,
kelvin=None,
white_value=None,
profile=None,
flash=None,
effect=None,
color_name=None,
):
"""Turn all or specified light on."""
data = {
key: value for key, value in [
key: value
for key, value in [
(ATTR_ENTITY_ID, entity_id),
(ATTR_PROFILE, profile),
(ATTR_TRANSITION, transition),
@@ -46,11 +102,11 @@ async def async_turn_on(hass, entity_id=None, transition=None, brightness=None,
(ATTR_FLASH, flash),
(ATTR_EFFECT, effect),
(ATTR_COLOR_NAME, color_name),
] if value is not None
]
if value is not None
}
await hass.services.async_call(
DOMAIN, SERVICE_TURN_ON, data, blocking=True)
await hass.services.async_call(DOMAIN, SERVICE_TURN_ON, data, blocking=True)
@bind_hass
@@ -62,14 +118,12 @@ def turn_off(hass, entity_id=None, transition=None):
async def async_turn_off(hass, entity_id=None, transition=None):
"""Turn all or specified light off."""
data = {
key: value for key, value in [
(ATTR_ENTITY_ID, entity_id),
(ATTR_TRANSITION, transition),
] if value is not None
key: value
for key, value in [(ATTR_ENTITY_ID, entity_id), (ATTR_TRANSITION, transition)]
if value is not None
}
await hass.services.async_call(
DOMAIN, SERVICE_TURN_OFF, data, blocking=True)
await hass.services.async_call(DOMAIN, SERVICE_TURN_OFF, data, blocking=True)
@bind_hass
@@ -81,11 +135,9 @@ def toggle(hass, entity_id=None, transition=None):
async def async_toggle(hass, entity_id=None, transition=None):
"""Toggle all or specified light."""
data = {
key: value for key, value in [
(ATTR_ENTITY_ID, entity_id),
(ATTR_TRANSITION, transition),
] if value is not None
key: value
for key, value in [(ATTR_ENTITY_ID, entity_id), (ATTR_TRANSITION, transition)]
if value is not None
}
await hass.services.async_call(
DOMAIN, SERVICE_TOGGLE, data, blocking=True)
await hass.services.async_call(DOMAIN, SERVICE_TOGGLE, data, blocking=True)
@@ -2,17 +2,21 @@
import pytest
from homeassistant.components import light
from homeassistant.const import (
STATE_ON, STATE_OFF, CONF_PLATFORM)
from homeassistant.const import STATE_ON, STATE_OFF, CONF_PLATFORM
from homeassistant.setup import async_setup_component
import homeassistant.components.automation as automation
from homeassistant.components.device_automation import (
async_get_device_automation_triggers)
async_get_device_automation_triggers,
)
from homeassistant.helpers import device_registry
from tests.common import (
MockConfigEntry, async_mock_service, mock_device_registry, mock_registry)
MockConfigEntry,
async_mock_service,
mock_device_registry,
mock_registry,
)
@pytest.fixture
@@ -30,7 +34,7 @@ def entity_reg(hass):
@pytest.fixture
def calls(hass):
"""Track calls to a mock serivce."""
return async_mock_service(hass, 'test', 'automation')
return async_mock_service(hass, "test", "automation")
def _same_triggers(a, b):
@@ -45,72 +49,98 @@ def _same_triggers(a, b):
async def test_get_triggers(hass, device_reg, entity_reg):
"""Test we get the expected triggers from a light."""
config_entry = MockConfigEntry(domain='test', data={})
config_entry = MockConfigEntry(domain="test", data={})
config_entry.add_to_hass(hass)
device_entry = device_reg.async_get_or_create(
config_entry_id=config_entry.entry_id,
connections={
(device_registry.CONNECTION_NETWORK_MAC, '12:34:56:AB:CD:EF')
})
entity_reg.async_get_or_create(
'light', 'test', '5678', device_id=device_entry.id)
connections={(device_registry.CONNECTION_NETWORK_MAC, "12:34:56:AB:CD:EF")},
)
entity_reg.async_get_or_create("light", "test", "5678", device_id=device_entry.id)
expected_triggers = [
{'platform': 'device', 'domain': 'light', 'type': 'turn_off',
'device_id': device_entry.id, 'entity_id': 'light.test_5678'},
{'platform': 'device', 'domain': 'light', 'type': 'turn_on',
'device_id': device_entry.id, 'entity_id': 'light.test_5678'},
{
"platform": "device",
"domain": "light",
"type": "turn_off",
"device_id": device_entry.id,
"entity_id": "light.test_5678",
},
{
"platform": "device",
"domain": "light",
"type": "turn_on",
"device_id": device_entry.id,
"entity_id": "light.test_5678",
},
]
triggers = await async_get_device_automation_triggers(hass,
device_entry.id)
triggers = await async_get_device_automation_triggers(hass, device_entry.id)
assert _same_triggers(triggers, expected_triggers)
async def test_if_fires_on_state_change(hass, calls):
"""Test for turn_on and turn_off triggers firing."""
platform = getattr(hass.components, 'test.light')
platform = getattr(hass.components, "test.light")
platform.init()
assert await async_setup_component(hass, light.DOMAIN,
{light.DOMAIN: {CONF_PLATFORM: 'test'}})
assert await async_setup_component(
hass, light.DOMAIN, {light.DOMAIN: {CONF_PLATFORM: "test"}}
)
dev1, dev2, dev3 = platform.DEVICES
assert await async_setup_component(hass, automation.DOMAIN, {
automation.DOMAIN: [{
'trigger': {
'platform': 'device',
'domain': light.DOMAIN,
'entity_id': dev1.entity_id,
'type': 'turn_on'
},
'action': {
'service': 'test.automation',
'data_template': {
'some':
'turn_on {{ trigger.%s }}' % '}} - {{ trigger.'.join((
'platform', 'entity_id',
'from_state.state', 'to_state.state',
'for'))
assert await async_setup_component(
hass,
automation.DOMAIN,
{
automation.DOMAIN: [
{
"trigger": {
"platform": "device",
"domain": light.DOMAIN,
"entity_id": dev1.entity_id,
"type": "turn_on",
},
"action": {
"service": "test.automation",
"data_template": {
"some": "turn_on {{ trigger.%s }}"
% "}} - {{ trigger.".join(
(
"platform",
"entity_id",
"from_state.state",
"to_state.state",
"for",
)
)
},
},
},
}},
{'trigger': {
'platform': 'device',
'domain': light.DOMAIN,
'entity_id': dev1.entity_id,
'type': 'turn_off'
},
'action': {
'service': 'test.automation',
'data_template': {
'some':
'turn_off {{ trigger.%s }}' % '}} - {{ trigger.'.join((
'platform', 'entity_id',
'from_state.state', 'to_state.state',
'for'))
{
"trigger": {
"platform": "device",
"domain": light.DOMAIN,
"entity_id": dev1.entity_id,
"type": "turn_off",
},
"action": {
"service": "test.automation",
"data_template": {
"some": "turn_off {{ trigger.%s }}"
% "}} - {{ trigger.".join(
(
"platform",
"entity_id",
"from_state.state",
"to_state.state",
"for",
)
)
},
},
},
}},
]
})
]
},
)
await hass.async_block_till_done()
assert hass.states.get(dev1.entity_id).state == STATE_ON
assert len(calls) == 0
@@ -118,11 +148,13 @@ async def test_if_fires_on_state_change(hass, calls):
hass.states.async_set(dev1.entity_id, STATE_OFF)
await hass.async_block_till_done()
assert len(calls) == 1
assert calls[0].data['some'] == \
'turn_off state - {} - on - off - None'.format(dev1.entity_id)
assert calls[0].data["some"] == "turn_off state - {} - on - off - None".format(
dev1.entity_id
)
hass.states.async_set(dev1.entity_id, STATE_ON)
await hass.async_block_till_done()
assert len(calls) == 2
assert calls[1].data['some'] == \
'turn_on state - {} - off - on - None'.format(dev1.entity_id)
assert calls[1].data["some"] == "turn_on state - {} - off - on - None".format(
dev1.entity_id
)
+178 -195
View File
@@ -11,13 +11,24 @@ from homeassistant import core
from homeassistant.exceptions import Unauthorized
from homeassistant.setup import setup_component, async_setup_component
from homeassistant.const import (
ATTR_ENTITY_ID, STATE_ON, STATE_OFF, CONF_PLATFORM,
SERVICE_TURN_ON, SERVICE_TURN_OFF, SERVICE_TOGGLE, ATTR_SUPPORTED_FEATURES)
ATTR_ENTITY_ID,
STATE_ON,
STATE_OFF,
CONF_PLATFORM,
SERVICE_TURN_ON,
SERVICE_TURN_OFF,
SERVICE_TOGGLE,
ATTR_SUPPORTED_FEATURES,
)
from homeassistant.components import light
from homeassistant.helpers.intent import IntentHandleError
from tests.common import (
async_mock_service, mock_service, get_test_home_assistant, mock_storage)
async_mock_service,
mock_service,
get_test_home_assistant,
mock_storage,
)
from tests.components.light import common
@@ -42,11 +53,11 @@ class TestLight(unittest.TestCase):
def test_methods(self):
"""Test if methods call the services as expected."""
# Test is_on
self.hass.states.set('light.test', STATE_ON)
assert light.is_on(self.hass, 'light.test')
self.hass.states.set("light.test", STATE_ON)
assert light.is_on(self.hass, "light.test")
self.hass.states.set('light.test', STATE_OFF)
assert not light.is_on(self.hass, 'light.test')
self.hass.states.set("light.test", STATE_OFF)
assert not light.is_on(self.hass, "light.test")
self.hass.states.set(light.ENTITY_ID_ALL_LIGHTS, STATE_ON)
assert light.is_on(self.hass)
@@ -55,19 +66,19 @@ class TestLight(unittest.TestCase):
assert not light.is_on(self.hass)
# Test turn_on
turn_on_calls = mock_service(
self.hass, light.DOMAIN, SERVICE_TURN_ON)
turn_on_calls = mock_service(self.hass, light.DOMAIN, SERVICE_TURN_ON)
common.turn_on(
self.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')
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",
)
self.hass.block_till_done()
@@ -76,21 +87,21 @@ class TestLight(unittest.TestCase):
assert light.DOMAIN == call.domain
assert SERVICE_TURN_ON == call.service
assert 'entity_id_val' == call.data.get(ATTR_ENTITY_ID)
assert 'transition_val' == call.data.get(light.ATTR_TRANSITION)
assert 'brightness_val' == call.data.get(light.ATTR_BRIGHTNESS)
assert 'rgb_color_val' == call.data.get(light.ATTR_RGB_COLOR)
assert 'xy_color_val' == call.data.get(light.ATTR_XY_COLOR)
assert 'profile_val' == call.data.get(light.ATTR_PROFILE)
assert 'color_name_val' == call.data.get(light.ATTR_COLOR_NAME)
assert 'white_val' == call.data.get(light.ATTR_WHITE_VALUE)
assert "entity_id_val" == call.data.get(ATTR_ENTITY_ID)
assert "transition_val" == call.data.get(light.ATTR_TRANSITION)
assert "brightness_val" == call.data.get(light.ATTR_BRIGHTNESS)
assert "rgb_color_val" == call.data.get(light.ATTR_RGB_COLOR)
assert "xy_color_val" == call.data.get(light.ATTR_XY_COLOR)
assert "profile_val" == call.data.get(light.ATTR_PROFILE)
assert "color_name_val" == call.data.get(light.ATTR_COLOR_NAME)
assert "white_val" == call.data.get(light.ATTR_WHITE_VALUE)
# Test turn_off
turn_off_calls = mock_service(
self.hass, light.DOMAIN, SERVICE_TURN_OFF)
turn_off_calls = mock_service(self.hass, light.DOMAIN, SERVICE_TURN_OFF)
common.turn_off(
self.hass, entity_id='entity_id_val', transition='transition_val')
self.hass, entity_id="entity_id_val", transition="transition_val"
)
self.hass.block_till_done()
@@ -99,15 +110,13 @@ class TestLight(unittest.TestCase):
assert light.DOMAIN == call.domain
assert SERVICE_TURN_OFF == call.service
assert 'entity_id_val' == call.data[ATTR_ENTITY_ID]
assert 'transition_val' == call.data[light.ATTR_TRANSITION]
assert "entity_id_val" == call.data[ATTR_ENTITY_ID]
assert "transition_val" == call.data[light.ATTR_TRANSITION]
# Test toggle
toggle_calls = mock_service(
self.hass, light.DOMAIN, SERVICE_TOGGLE)
toggle_calls = mock_service(self.hass, light.DOMAIN, SERVICE_TOGGLE)
common.toggle(
self.hass, entity_id='entity_id_val', transition='transition_val')
common.toggle(self.hass, entity_id="entity_id_val", transition="transition_val")
self.hass.block_till_done()
@@ -116,16 +125,17 @@ class TestLight(unittest.TestCase):
assert light.DOMAIN == call.domain
assert SERVICE_TOGGLE == call.service
assert 'entity_id_val' == call.data[ATTR_ENTITY_ID]
assert 'transition_val' == call.data[light.ATTR_TRANSITION]
assert "entity_id_val" == call.data[ATTR_ENTITY_ID]
assert "transition_val" == call.data[light.ATTR_TRANSITION]
def test_services(self):
"""Test the provided services."""
platform = getattr(self.hass.components, 'test.light')
platform = getattr(self.hass.components, "test.light")
platform.init()
assert setup_component(self.hass, light.DOMAIN,
{light.DOMAIN: {CONF_PLATFORM: 'test'}})
assert setup_component(
self.hass, light.DOMAIN, {light.DOMAIN: {CONF_PLATFORM: "test"}}
)
dev1, dev2, dev3 = platform.DEVICES
@@ -193,41 +203,41 @@ class TestLight(unittest.TestCase):
assert not light.is_on(self.hass, dev3.entity_id)
# Ensure all attributes process correctly
common.turn_on(self.hass, dev1.entity_id,
transition=10, brightness=20, color_name='blue')
common.turn_on(
self.hass, dev2.entity_id, rgb_color=(255, 255, 255),
white_value=255)
common.turn_on(self.hass, dev3.entity_id, xy_color=(.4, .6))
self.hass, dev1.entity_id, transition=10, brightness=20, color_name="blue"
)
common.turn_on(
self.hass, dev2.entity_id, rgb_color=(255, 255, 255), white_value=255
)
common.turn_on(self.hass, dev3.entity_id, xy_color=(0.4, 0.6))
self.hass.block_till_done()
_, data = dev1.last_call('turn_on')
_, data = dev1.last_call("turn_on")
assert {
light.ATTR_TRANSITION: 10,
light.ATTR_BRIGHTNESS: 20,
light.ATTR_HS_COLOR: (240, 100),
} == data
_, data = dev2.last_call('turn_on')
assert {
light.ATTR_HS_COLOR: (0, 0),
light.ATTR_WHITE_VALUE: 255,
} == data
_, data = dev2.last_call("turn_on")
assert {light.ATTR_HS_COLOR: (0, 0), light.ATTR_WHITE_VALUE: 255} == data
_, data = dev3.last_call('turn_on')
assert {
light.ATTR_HS_COLOR: (71.059, 100),
} == data
_, data = dev3.last_call("turn_on")
assert {light.ATTR_HS_COLOR: (71.059, 100)} == data
# Ensure attributes are filtered when light is turned off
common.turn_on(self.hass, dev1.entity_id,
transition=10, brightness=0, color_name='blue')
common.turn_on(
self.hass, dev2.entity_id, brightness=0, rgb_color=(255, 255, 255),
white_value=0)
common.turn_on(self.hass, dev3.entity_id, brightness=0,
xy_color=(.4, .6))
self.hass, dev1.entity_id, transition=10, brightness=0, color_name="blue"
)
common.turn_on(
self.hass,
dev2.entity_id,
brightness=0,
rgb_color=(255, 255, 255),
white_value=0,
)
common.turn_on(self.hass, dev3.entity_id, brightness=0, xy_color=(0.4, 0.6))
self.hass.block_till_done()
@@ -235,36 +245,32 @@ class TestLight(unittest.TestCase):
assert not light.is_on(self.hass, dev2.entity_id)
assert not light.is_on(self.hass, dev3.entity_id)
_, data = dev1.last_call('turn_off')
assert {
light.ATTR_TRANSITION: 10,
} == data
_, data = dev1.last_call("turn_off")
assert {light.ATTR_TRANSITION: 10} == data
_, data = dev2.last_call('turn_off')
_, data = dev2.last_call("turn_off")
assert {} == data
_, data = dev3.last_call('turn_off')
_, data = dev3.last_call("turn_off")
assert {} == data
# One of the light profiles
prof_name, prof_h, prof_s, prof_bri = 'relax', 35.932, 69.412, 144
prof_name, prof_h, prof_s, prof_bri = "relax", 35.932, 69.412, 144
# Test light profiles
common.turn_on(self.hass, dev1.entity_id, profile=prof_name)
# Specify a profile and a brightness attribute to overwrite it
common.turn_on(
self.hass, dev2.entity_id,
profile=prof_name, brightness=100)
common.turn_on(self.hass, dev2.entity_id, profile=prof_name, brightness=100)
self.hass.block_till_done()
_, data = dev1.last_call('turn_on')
_, data = dev1.last_call("turn_on")
assert {
light.ATTR_BRIGHTNESS: prof_bri,
light.ATTR_HS_COLOR: (prof_h, prof_s),
} == data
_, data = dev2.last_call('turn_on')
_, data = dev2.last_call("turn_on")
assert {
light.ATTR_BRIGHTNESS: 100,
light.ATTR_HS_COLOR: (prof_h, prof_s),
@@ -278,91 +284,85 @@ class TestLight(unittest.TestCase):
self.hass.block_till_done()
_, data = dev1.last_call('turn_on')
_, data = dev1.last_call("turn_on")
assert {} == data
_, data = dev2.last_call('turn_on')
_, data = dev2.last_call("turn_on")
assert {} == data
_, data = dev3.last_call('turn_on')
_, data = dev3.last_call("turn_on")
assert {} == data
# faulty attributes will not trigger a service call
common.turn_on(
self.hass, dev1.entity_id,
profile=prof_name, brightness='bright')
common.turn_on(
self.hass, dev1.entity_id,
rgb_color='yellowish')
common.turn_on(
self.hass, dev2.entity_id,
white_value='high')
self.hass, dev1.entity_id, profile=prof_name, brightness="bright"
)
common.turn_on(self.hass, dev1.entity_id, rgb_color="yellowish")
common.turn_on(self.hass, dev2.entity_id, white_value="high")
self.hass.block_till_done()
_, data = dev1.last_call('turn_on')
_, data = dev1.last_call("turn_on")
assert {} == data
_, data = dev2.last_call('turn_on')
_, data = dev2.last_call("turn_on")
assert {} == data
def test_broken_light_profiles(self):
"""Test light profiles."""
platform = getattr(self.hass.components, 'test.light')
platform = getattr(self.hass.components, "test.light")
platform.init()
user_light_file = self.hass.config.path(light.LIGHT_PROFILES_FILE)
# Setup a wrong light file
with open(user_light_file, 'w') as user_file:
user_file.write('id,x,y,brightness\n')
user_file.write('I,WILL,NOT,WORK\n')
with open(user_light_file, "w") as user_file:
user_file.write("id,x,y,brightness\n")
user_file.write("I,WILL,NOT,WORK\n")
assert not setup_component(
self.hass, light.DOMAIN, {light.DOMAIN: {CONF_PLATFORM: 'test'}})
self.hass, light.DOMAIN, {light.DOMAIN: {CONF_PLATFORM: "test"}}
)
def test_light_profiles(self):
"""Test light profiles."""
platform = getattr(self.hass.components, 'test.light')
platform = getattr(self.hass.components, "test.light")
platform.init()
user_light_file = self.hass.config.path(light.LIGHT_PROFILES_FILE)
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')
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")
assert setup_component(
self.hass, light.DOMAIN, {light.DOMAIN: {CONF_PLATFORM: 'test'}}
self.hass, light.DOMAIN, {light.DOMAIN: {CONF_PLATFORM: "test"}}
)
dev1, _, _ = platform.DEVICES
common.turn_on(self.hass, dev1.entity_id, profile='test')
common.turn_on(self.hass, dev1.entity_id, profile="test")
self.hass.block_till_done()
_, data = dev1.last_call('turn_on')
_, data = dev1.last_call("turn_on")
assert light.is_on(self.hass, dev1.entity_id)
assert {
light.ATTR_HS_COLOR: (71.059, 100),
light.ATTR_BRIGHTNESS: 100
} == data
assert {light.ATTR_HS_COLOR: (71.059, 100), light.ATTR_BRIGHTNESS: 100} == data
common.turn_on(self.hass, dev1.entity_id, profile='test_off')
common.turn_on(self.hass, dev1.entity_id, profile="test_off")
self.hass.block_till_done()
_, data = dev1.last_call('turn_off')
_, data = dev1.last_call("turn_off")
assert not light.is_on(self.hass, dev1.entity_id)
assert {} == data
def test_default_profiles_group(self):
"""Test default turn-on light profile for all lights."""
platform = getattr(self.hass.components, 'test.light')
platform = getattr(self.hass.components, "test.light")
platform.init()
user_light_file = self.hass.config.path(light.LIGHT_PROFILES_FILE)
@@ -379,28 +379,23 @@ class TestLight(unittest.TestCase):
return StringIO(profile_data)
return real_open(path, *args, **kwargs)
profile_data = "id,x,y,brightness\n" +\
"group.all_lights.default,.4,.6,99\n"
with mock.patch('os.path.isfile', side_effect=_mock_isfile):
with mock.patch('builtins.open', side_effect=_mock_open):
profile_data = "id,x,y,brightness\n" + "group.all_lights.default,.4,.6,99\n"
with mock.patch("os.path.isfile", side_effect=_mock_isfile):
with mock.patch("builtins.open", side_effect=_mock_open):
with mock_storage():
assert setup_component(
self.hass, light.DOMAIN,
{light.DOMAIN: {CONF_PLATFORM: 'test'}}
self.hass, light.DOMAIN, {light.DOMAIN: {CONF_PLATFORM: "test"}}
)
dev, _, _ = platform.DEVICES
common.turn_on(self.hass, dev.entity_id)
self.hass.block_till_done()
_, data = dev.last_call('turn_on')
assert {
light.ATTR_HS_COLOR: (71.059, 100),
light.ATTR_BRIGHTNESS: 99
} == data
_, data = dev.last_call("turn_on")
assert {light.ATTR_HS_COLOR: (71.059, 100), light.ATTR_BRIGHTNESS: 99} == data
def test_default_profiles_light(self):
"""Test default turn-on light profile for a specific light."""
platform = getattr(self.hass.components, 'test.light')
platform = getattr(self.hass.components, "test.light")
platform.init()
user_light_file = self.hass.config.path(light.LIGHT_PROFILES_FILE)
@@ -417,134 +412,122 @@ class TestLight(unittest.TestCase):
return StringIO(profile_data)
return real_open(path, *args, **kwargs)
profile_data = "id,x,y,brightness\n" +\
"group.all_lights.default,.3,.5,200\n" +\
"light.ceiling_2.default,.6,.6,100\n"
with mock.patch('os.path.isfile', side_effect=_mock_isfile):
with mock.patch('builtins.open', side_effect=_mock_open):
profile_data = (
"id,x,y,brightness\n"
+ "group.all_lights.default,.3,.5,200\n"
+ "light.ceiling_2.default,.6,.6,100\n"
)
with mock.patch("os.path.isfile", side_effect=_mock_isfile):
with mock.patch("builtins.open", side_effect=_mock_open):
with mock_storage():
assert setup_component(
self.hass, light.DOMAIN,
{light.DOMAIN: {CONF_PLATFORM: 'test'}}
self.hass, light.DOMAIN, {light.DOMAIN: {CONF_PLATFORM: "test"}}
)
dev = next(filter(lambda x: x.entity_id == 'light.ceiling_2',
platform.DEVICES))
dev = next(filter(lambda x: x.entity_id == "light.ceiling_2", platform.DEVICES))
common.turn_on(self.hass, dev.entity_id)
self.hass.block_till_done()
_, data = dev.last_call('turn_on')
assert {
light.ATTR_HS_COLOR: (50.353, 100),
light.ATTR_BRIGHTNESS: 100
} == data
_, data = dev.last_call("turn_on")
assert {light.ATTR_HS_COLOR: (50.353, 100), light.ATTR_BRIGHTNESS: 100} == data
async def test_intent_set_color(hass):
"""Test the set color intent."""
hass.states.async_set('light.hello_2', 'off', {
ATTR_SUPPORTED_FEATURES: light.SUPPORT_COLOR
})
hass.states.async_set('switch.hello', 'off')
hass.states.async_set(
"light.hello_2", "off", {ATTR_SUPPORTED_FEATURES: light.SUPPORT_COLOR}
)
hass.states.async_set("switch.hello", "off")
calls = async_mock_service(hass, light.DOMAIN, light.SERVICE_TURN_ON)
hass.helpers.intent.async_register(light.SetIntentHandler())
result = await hass.helpers.intent.async_handle(
'test', light.INTENT_SET, {
'name': {
'value': 'Hello',
},
'color': {
'value': 'blue'
}
})
"test",
light.INTENT_SET,
{"name": {"value": "Hello"}, "color": {"value": "blue"}},
)
await hass.async_block_till_done()
assert result.speech['plain']['speech'] == \
'Changed hello 2 to the color blue'
assert result.speech["plain"]["speech"] == "Changed hello 2 to the color blue"
assert len(calls) == 1
call = calls[0]
assert call.domain == light.DOMAIN
assert call.service == SERVICE_TURN_ON
assert call.data.get(ATTR_ENTITY_ID) == 'light.hello_2'
assert call.data.get(ATTR_ENTITY_ID) == "light.hello_2"
assert call.data.get(light.ATTR_RGB_COLOR) == (0, 0, 255)
async def test_intent_set_color_tests_feature(hass):
"""Test the set color intent."""
hass.states.async_set('light.hello', 'off')
hass.states.async_set("light.hello", "off")
calls = async_mock_service(hass, light.DOMAIN, light.SERVICE_TURN_ON)
hass.helpers.intent.async_register(light.SetIntentHandler())
try:
await hass.helpers.intent.async_handle(
'test', light.INTENT_SET, {
'name': {
'value': 'Hello',
},
'color': {
'value': 'blue'
}
})
assert False, 'handling intent should have raised'
"test",
light.INTENT_SET,
{"name": {"value": "Hello"}, "color": {"value": "blue"}},
)
assert False, "handling intent should have raised"
except IntentHandleError as err:
assert str(err) == 'Entity hello does not support changing colors'
assert str(err) == "Entity hello does not support changing colors"
assert len(calls) == 0
async def test_intent_set_color_and_brightness(hass):
"""Test the set color intent."""
hass.states.async_set('light.hello_2', 'off', {
ATTR_SUPPORTED_FEATURES: (
light.SUPPORT_COLOR | light.SUPPORT_BRIGHTNESS)
})
hass.states.async_set('switch.hello', 'off')
hass.states.async_set(
"light.hello_2",
"off",
{ATTR_SUPPORTED_FEATURES: (light.SUPPORT_COLOR | light.SUPPORT_BRIGHTNESS)},
)
hass.states.async_set("switch.hello", "off")
calls = async_mock_service(hass, light.DOMAIN, light.SERVICE_TURN_ON)
hass.helpers.intent.async_register(light.SetIntentHandler())
result = await hass.helpers.intent.async_handle(
'test', light.INTENT_SET, {
'name': {
'value': 'Hello',
},
'color': {
'value': 'blue'
},
'brightness': {
'value': '20'
}
})
"test",
light.INTENT_SET,
{
"name": {"value": "Hello"},
"color": {"value": "blue"},
"brightness": {"value": "20"},
},
)
await hass.async_block_till_done()
assert result.speech['plain']['speech'] == \
'Changed hello 2 to the color blue and 20% brightness'
assert (
result.speech["plain"]["speech"]
== "Changed hello 2 to the color blue and 20% brightness"
)
assert len(calls) == 1
call = calls[0]
assert call.domain == light.DOMAIN
assert call.service == SERVICE_TURN_ON
assert call.data.get(ATTR_ENTITY_ID) == 'light.hello_2'
assert call.data.get(ATTR_ENTITY_ID) == "light.hello_2"
assert call.data.get(light.ATTR_RGB_COLOR) == (0, 0, 255)
assert call.data.get(light.ATTR_BRIGHTNESS_PCT) == 20
async def test_light_context(hass, hass_admin_user):
"""Test that light context works."""
assert await async_setup_component(hass, 'light', {
'light': {
'platform': 'test'
}
})
assert await async_setup_component(hass, "light", {"light": {"platform": "test"}})
state = hass.states.get('light.ceiling')
state = hass.states.get("light.ceiling")
assert state is not None
await hass.services.async_call('light', 'toggle', {
'entity_id': state.entity_id,
}, True, core.Context(user_id=hass_admin_user.id))
await hass.services.async_call(
"light",
"toggle",
{"entity_id": state.entity_id},
True,
core.Context(user_id=hass_admin_user.id),
)
state2 = hass.states.get('light.ceiling')
state2 = hass.states.get("light.ceiling")
assert state2 is not None
assert state.state != state2.state
assert state2.context.user_id == hass_admin_user.id
@@ -552,18 +535,18 @@ async def test_light_context(hass, hass_admin_user):
async def test_light_turn_on_auth(hass, hass_admin_user):
"""Test that light context works."""
assert await async_setup_component(hass, 'light', {
'light': {
'platform': 'test'
}
})
assert await async_setup_component(hass, "light", {"light": {"platform": "test"}})
state = hass.states.get('light.ceiling')
state = hass.states.get("light.ceiling")
assert state is not None
hass_admin_user.mock_policy({})
with pytest.raises(Unauthorized):
await hass.services.async_call('light', 'turn_on', {
'entity_id': state.entity_id,
}, True, core.Context(user_id=hass_admin_user.id))
await hass.services.async_call(
"light",
"turn_on",
{"entity_id": state.entity_id},
True,
core.Context(user_id=hass_admin_user.id),
)