mirror of
https://github.com/home-assistant/core.git
synced 2025-07-14 00:37:13 +00:00
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
This commit is contained in:
parent
f6e9dd4832
commit
4d1633807c
@ -175,6 +175,17 @@ def preprocess_turn_on_alternatives(params):
|
|||||||
params[ATTR_HS_COLOR] = color_util.color_RGB_to_hs(*rgb_color)
|
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):
|
class SetIntentHandler(intent.IntentHandler):
|
||||||
"""Handle set color intents."""
|
"""Handle set color intents."""
|
||||||
|
|
||||||
@ -272,16 +283,23 @@ async def async_setup(hass, config):
|
|||||||
)
|
)
|
||||||
|
|
||||||
preprocess_turn_on_alternatives(params)
|
preprocess_turn_on_alternatives(params)
|
||||||
|
turn_lights_off, off_params = preprocess_turn_off(params)
|
||||||
|
|
||||||
update_tasks = []
|
update_tasks = []
|
||||||
for light in target_lights:
|
for light in target_lights:
|
||||||
light.async_set_context(service.context)
|
light.async_set_context(service.context)
|
||||||
|
|
||||||
pars = params
|
pars = params
|
||||||
|
off_pars = off_params
|
||||||
|
turn_light_off = turn_lights_off
|
||||||
if not pars:
|
if not pars:
|
||||||
pars = params.copy()
|
pars = params.copy()
|
||||||
pars[ATTR_PROFILE] = Profiles.get_default(light.entity_id)
|
pars[ATTR_PROFILE] = Profiles.get_default(light.entity_id)
|
||||||
preprocess_turn_on_alternatives(pars)
|
preprocess_turn_on_alternatives(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)
|
await light.async_turn_on(**pars)
|
||||||
|
|
||||||
if not light.should_poll:
|
if not light.should_poll:
|
||||||
|
@ -263,8 +263,6 @@ class TradfriLight(Light):
|
|||||||
brightness = kwargs[ATTR_BRIGHTNESS]
|
brightness = kwargs[ATTR_BRIGHTNESS]
|
||||||
if brightness > 254:
|
if brightness > 254:
|
||||||
brightness = 254
|
brightness = 254
|
||||||
elif brightness < 0:
|
|
||||||
brightness = 0
|
|
||||||
dimmer_data = {ATTR_DIMMER: brightness, ATTR_TRANSITION_TIME:
|
dimmer_data = {ATTR_DIMMER: brightness, ATTR_TRANSITION_TIME:
|
||||||
transition_time}
|
transition_time}
|
||||||
dimmer_command = self._light_control.set_dimmer(**dimmer_data)
|
dimmer_command = self._light_control.set_dimmer(**dimmer_data)
|
||||||
|
@ -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, dev2.entity_id)
|
||||||
assert not light.is_on(self.hass, dev3.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
|
# toggle all lights
|
||||||
common.toggle(self.hass)
|
common.toggle(self.hass)
|
||||||
|
|
||||||
@ -207,6 +220,32 @@ class TestLight(unittest.TestCase):
|
|||||||
light.ATTR_HS_COLOR: (71.059, 100),
|
light.ATTR_HS_COLOR: (71.059, 100),
|
||||||
} == data
|
} == 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
|
# 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
|
||||||
|
|
||||||
@ -292,6 +331,7 @@ class TestLight(unittest.TestCase):
|
|||||||
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')
|
||||||
|
|
||||||
assert setup_component(
|
assert setup_component(
|
||||||
self.hass, light.DOMAIN, {light.DOMAIN: {CONF_PLATFORM: 'test'}}
|
self.hass, light.DOMAIN, {light.DOMAIN: {CONF_PLATFORM: 'test'}}
|
||||||
@ -305,11 +345,21 @@ class TestLight(unittest.TestCase):
|
|||||||
|
|
||||||
_, data = dev1.last_call('turn_on')
|
_, data = dev1.last_call('turn_on')
|
||||||
|
|
||||||
|
assert light.is_on(self.hass, dev1.entity_id)
|
||||||
assert {
|
assert {
|
||||||
light.ATTR_HS_COLOR: (71.059, 100),
|
light.ATTR_HS_COLOR: (71.059, 100),
|
||||||
light.ATTR_BRIGHTNESS: 100
|
light.ATTR_BRIGHTNESS: 100
|
||||||
} == data
|
} == 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):
|
def test_default_profiles_group(self):
|
||||||
"""Test default turn-on light profile for all lights."""
|
"""Test default turn-on light profile for all lights."""
|
||||||
platform = loader.get_component(self.hass, 'light.test')
|
platform = loader.get_component(self.hass, 'light.test')
|
||||||
|
@ -35,20 +35,12 @@ TURN_ON_TEST_CASES = [
|
|||||||
'brightness': 100
|
'brightness': 100
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
# Brightness == 0
|
# Brightness == 1
|
||||||
[
|
[
|
||||||
{'can_set_dimmer': True},
|
{'can_set_dimmer': True},
|
||||||
{'brightness': 0},
|
{'brightness': 1},
|
||||||
{
|
{
|
||||||
'brightness': 0
|
'brightness': 1
|
||||||
}
|
|
||||||
],
|
|
||||||
# Brightness < 0
|
|
||||||
[
|
|
||||||
{'can_set_dimmer': True},
|
|
||||||
{'brightness': -1},
|
|
||||||
{
|
|
||||||
'brightness': 0
|
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
# Brightness > 254
|
# Brightness > 254
|
||||||
|
Loading…
x
Reference in New Issue
Block a user