Turn light off if brightness is 0 (#22400)

* Turn light off if brightness is 0

* Lint

* Review comments

* Lint

* Fixup, add tests

* Fix trådfri light + test
pull/22555/head^2
emontnemery 2019-03-31 06:04:32 +02:00 committed by Paulus Schoutsen
parent f6e9dd4832
commit 4d1633807c
4 changed files with 72 additions and 14 deletions

View File

@ -175,6 +175,17 @@ def preprocess_turn_on_alternatives(params):
params[ATTR_HS_COLOR] = color_util.color_RGB_to_hs(*rgb_color)
def preprocess_turn_off(params):
"""Process data for turning light off if brightness is 0."""
if ATTR_BRIGHTNESS in params and params[ATTR_BRIGHTNESS] == 0:
# Zero brightness: Light will be turned off
params = {k: v for k, v in params.items() if k in [ATTR_TRANSITION,
ATTR_FLASH]}
return (True, params) # Light should be turned off
return (False, None) # Light should be turned on
class SetIntentHandler(intent.IntentHandler):
"""Handle set color intents."""
@ -272,17 +283,24 @@ async def async_setup(hass, config):
)
preprocess_turn_on_alternatives(params)
turn_lights_off, off_params = preprocess_turn_off(params)
update_tasks = []
for light in target_lights:
light.async_set_context(service.context)
pars = params
off_pars = off_params
turn_light_off = turn_lights_off
if not pars:
pars = params.copy()
pars[ATTR_PROFILE] = Profiles.get_default(light.entity_id)
preprocess_turn_on_alternatives(pars)
await light.async_turn_on(**pars)
turn_light_off, off_pars = preprocess_turn_off(pars)
if turn_light_off:
await light.async_turn_off(**off_pars)
else:
await light.async_turn_on(**pars)
if not light.should_poll:
continue

View File

@ -263,8 +263,6 @@ class TradfriLight(Light):
brightness = kwargs[ATTR_BRIGHTNESS]
if brightness > 254:
brightness = 254
elif brightness < 0:
brightness = 0
dimmer_data = {ATTR_DIMMER: brightness, ATTR_TRANSITION_TIME:
transition_time}
dimmer_command = self._light_control.set_dimmer(**dimmer_data)

View File

@ -161,6 +161,19 @@ class TestLight(unittest.TestCase):
assert not light.is_on(self.hass, dev2.entity_id)
assert not light.is_on(self.hass, dev3.entity_id)
# turn off all lights by setting brightness to 0
common.turn_on(self.hass)
self.hass.block_till_done()
common.turn_on(self.hass, brightness=0)
self.hass.block_till_done()
assert not light.is_on(self.hass, dev1.entity_id)
assert not light.is_on(self.hass, dev2.entity_id)
assert not light.is_on(self.hass, dev3.entity_id)
# toggle all lights
common.toggle(self.hass)
@ -207,6 +220,32 @@ class TestLight(unittest.TestCase):
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.block_till_done()
assert not light.is_on(self.hass, dev1.entity_id)
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 = dev2.last_call('turn_off')
assert {} == data
_, 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
@ -292,6 +331,7 @@ class TestLight(unittest.TestCase):
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'}}
@ -305,11 +345,21 @@ class TestLight(unittest.TestCase):
_, 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
common.turn_on(self.hass, dev1.entity_id, profile='test_off')
self.hass.block_till_done()
_, 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 = loader.get_component(self.hass, 'light.test')

View File

@ -35,20 +35,12 @@ TURN_ON_TEST_CASES = [
'brightness': 100
}
],
# Brightness == 0
# Brightness == 1
[
{'can_set_dimmer': True},
{'brightness': 0},
{'brightness': 1},
{
'brightness': 0
}
],
# Brightness < 0
[
{'can_set_dimmer': True},
{'brightness': -1},
{
'brightness': 0
'brightness': 1
}
],
# Brightness > 254