Fix light toggle service attributes (#35483)

This commit is contained in:
Xiaonan Shen authored and GitHub committed 2020-05-11 12:58:59 +02:00
1 parent 823a44cd23
commit 5d8e6d5432
3 files changed
+102 -18

No files matched your search

+24 -13
View File
@@ -167,12 +167,19 @@ def preprocess_turn_on_alternatives(params):
if rgb_color is not None:
params[ATTR_HS_COLOR] = color_util.color_RGB_to_hs(*rgb_color)
return params
def filter_turn_off_params(params):
"""Filter out params not used in turn off."""
return {k: v for k, v in params.items() if k in (ATTR_TRANSITION, ATTR_FLASH)}
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)}
params = filter_turn_off_params(params)
return (True, params) # Light should be turned off
return (False, None) # Light should be turned on
@@ -198,13 +205,7 @@ async def async_setup(hass, config):
if entity_field in data
}
preprocess_turn_on_alternatives(data)
turn_lights_off, off_params = preprocess_turn_off(data)
base["params"] = data
base["turn_lights_off"] = turn_lights_off
base["off_params"] = off_params
base["params"] = preprocess_turn_on_alternatives(data)
return base
async def async_handle_light_on_service(light, call):
@@ -213,8 +214,6 @@ async def async_setup(hass, config):
If brightness is set to 0, this service will turn the light off.
"""
params = call.data["params"]
turn_light_off = call.data["turn_lights_off"]
off_params = call.data["off_params"]
if not params:
default_profile = Profiles.get_default(light.entity_id)
@@ -222,7 +221,6 @@ async def async_setup(hass, config):
if default_profile is not None:
params = {ATTR_PROFILE: default_profile}
preprocess_turn_on_alternatives(params)
turn_light_off, off_params = preprocess_turn_off(params)
elif ATTR_BRIGHTNESS_STEP in params or ATTR_BRIGHTNESS_STEP_PCT in params:
brightness = light.brightness if light.is_on else 0
@@ -236,13 +234,24 @@ async def async_setup(hass, config):
brightness += round(params.pop(ATTR_BRIGHTNESS_STEP_PCT) / 100 * 255)
params[ATTR_BRIGHTNESS] = max(0, min(255, brightness))
turn_light_off, off_params = preprocess_turn_off(params)
turn_light_off, off_params = preprocess_turn_off(params)
if turn_light_off:
await light.async_turn_off(**off_params)
else:
await light.async_turn_on(**params)
async def async_handle_toggle_service(light, call):
"""Handle toggling a light.
If brightness is set to 0, this service will turn the light off.
"""
if light.is_on:
off_params = filter_turn_off_params(call.data["params"])
await light.async_turn_off(**off_params)
else:
await async_handle_light_on_service(light, call)
# Listen for light on and light off service calls.
component.async_register_entity_service(
@@ -258,7 +267,9 @@ async def async_setup(hass, config):
)
component.async_register_entity_service(
SERVICE_TOGGLE, LIGHT_TURN_ON_SCHEMA, "async_toggle"
SERVICE_TOGGLE,
vol.All(cv.make_entity_service_schema(LIGHT_TURN_ON_SCHEMA), preprocess_data),
async_handle_toggle_service,
)
return True