Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion homeassistant/components/light/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,11 @@ async def async_handle_light_service(service):
update_tasks = []
for light in target_lights:
if service.service == SERVICE_TURN_ON:
await light.async_turn_on(**params)
pars = params.copy()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should put the copy inside if not pars: to avoid unnecessary copying.

if not pars:
pars[ATTR_PROFILE] = Profiles.get_default(light.entity_id)
preprocess_turn_on_alternatives(pars)
await light.async_turn_on(**pars)
elif service.service == SERVICE_TURN_OFF:
await light.async_turn_off(**params)
else:
Expand Down Expand Up @@ -431,6 +435,17 @@ def get(cls, name):
"""Return a named profile."""
return cls._all.get(name)

@classmethod
def get_default(cls, entity_id):
"""Return the default turn-on profile for the given light."""
name = entity_id + ".default"
if name in cls._all:
return name
name = ENTITY_ID_ALL_LIGHTS + ".default"
if name in cls._all:
return name
return None


class Light(ToggleEntity):
"""Representation of a light."""
Expand Down
78 changes: 78 additions & 0 deletions tests/components/light/test_init.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
"""The tests for the Light component."""
# pylint: disable=protected-access
import unittest
import unittest.mock as mock
import os
from io import StringIO

from homeassistant.setup import setup_component
import homeassistant.loader as loader
Expand Down Expand Up @@ -308,6 +310,82 @@ def test_light_profiles(self):
light.ATTR_BRIGHTNESS: 100
}, data)

def test_default_profiles_group(self):
"""Test default turn-on light profile for all lights."""
platform = loader.get_component(self.hass, 'light.test')
platform.init()

user_light_file = self.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):
if path == user_light_file:
return StringIO(profile_data)
return real_open(path)

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):
self.assertTrue(setup_component(
self.hass, light.DOMAIN,
{light.DOMAIN: {CONF_PLATFORM: 'test'}}
))

dev, _, _ = platform.DEVICES
light.turn_on(self.hass, dev.entity_id)
self.hass.block_till_done()
_, data = dev.last_call('turn_on')
self.assertEqual({
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 = loader.get_component(self.hass, 'light.test')
platform.init()

user_light_file = self.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):
if path == user_light_file:
return StringIO(profile_data)
return real_open(path)

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):
self.assertTrue(setup_component(
self.hass, light.DOMAIN,
{light.DOMAIN: {CONF_PLATFORM: 'test'}}
))

dev = next(filter(lambda x: x.entity_id == 'light.ceiling_2',
platform.DEVICES))
light.turn_on(self.hass, dev.entity_id)
self.hass.block_till_done()
_, data = dev.last_call('turn_on')
self.assertEqual({
light.ATTR_HS_COLOR: (50.353, 100),
light.ATTR_BRIGHTNESS: 100
}, data)


async def test_intent_set_color(hass):
"""Test the set color intent."""
Expand Down