mirror of
https://github.com/home-assistant/core.git
synced 2025-07-15 17:27:10 +00:00
Apply default light profile only when light is toggled from off to on (#49376)
Co-authored-by: Franck Nijhof <git@frenck.dev>
This commit is contained in:
parent
042099de14
commit
2dedf3dd07
@ -205,6 +205,8 @@ LIGHT_TURN_ON_SCHEMA = {
|
|||||||
ATTR_EFFECT: cv.string,
|
ATTR_EFFECT: cv.string,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
LIGHT_TURN_OFF_SCHEMA = {ATTR_TRANSITION: VALID_TRANSITION, ATTR_FLASH: VALID_FLASH}
|
||||||
|
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
@ -295,8 +297,10 @@ async def async_setup(hass, config): # noqa: C901
|
|||||||
|
|
||||||
preprocess_turn_on_alternatives(hass, params)
|
preprocess_turn_on_alternatives(hass, params)
|
||||||
|
|
||||||
if ATTR_PROFILE not in params:
|
if (not params or not light.is_on) or (
|
||||||
profiles.apply_default(light.entity_id, params)
|
params and ATTR_TRANSITION not in params
|
||||||
|
):
|
||||||
|
profiles.apply_default(light.entity_id, light.is_on, params)
|
||||||
|
|
||||||
supported_color_modes = light.supported_color_modes
|
supported_color_modes = light.supported_color_modes
|
||||||
# Backwards compatibility: if an RGBWW color is specified, convert to RGB + W
|
# Backwards compatibility: if an RGBWW color is specified, convert to RGB + W
|
||||||
@ -366,17 +370,24 @@ async def async_setup(hass, config): # noqa: C901
|
|||||||
if supported_color_modes:
|
if supported_color_modes:
|
||||||
params.pop(ATTR_WHITE_VALUE, None)
|
params.pop(ATTR_WHITE_VALUE, None)
|
||||||
|
|
||||||
# Zero brightness: Light will be turned off
|
|
||||||
if params.get(ATTR_BRIGHTNESS) == 0:
|
if params.get(ATTR_BRIGHTNESS) == 0:
|
||||||
await light.async_turn_off(**filter_turn_off_params(params))
|
await async_handle_light_off_service(light, call)
|
||||||
else:
|
else:
|
||||||
await light.async_turn_on(**params)
|
await light.async_turn_on(**params)
|
||||||
|
|
||||||
|
async def async_handle_light_off_service(light, call):
|
||||||
|
"""Handle turning off a light."""
|
||||||
|
params = dict(call.data["params"])
|
||||||
|
|
||||||
|
if ATTR_TRANSITION not in params:
|
||||||
|
profiles.apply_default(light.entity_id, True, params)
|
||||||
|
|
||||||
|
await light.async_turn_off(**filter_turn_off_params(params))
|
||||||
|
|
||||||
async def async_handle_toggle_service(light, call):
|
async def async_handle_toggle_service(light, call):
|
||||||
"""Handle toggling a light."""
|
"""Handle toggling a light."""
|
||||||
if light.is_on:
|
if light.is_on:
|
||||||
off_params = filter_turn_off_params(call.data["params"])
|
await async_handle_light_off_service(light, call)
|
||||||
await light.async_turn_off(**off_params)
|
|
||||||
else:
|
else:
|
||||||
await async_handle_light_on_service(light, call)
|
await async_handle_light_on_service(light, call)
|
||||||
|
|
||||||
@ -390,8 +401,8 @@ async def async_setup(hass, config): # noqa: C901
|
|||||||
|
|
||||||
component.async_register_entity_service(
|
component.async_register_entity_service(
|
||||||
SERVICE_TURN_OFF,
|
SERVICE_TURN_OFF,
|
||||||
{ATTR_TRANSITION: VALID_TRANSITION, ATTR_FLASH: VALID_FLASH},
|
vol.All(cv.make_entity_service_schema(LIGHT_TURN_OFF_SCHEMA), preprocess_data),
|
||||||
"async_turn_off",
|
async_handle_light_off_service,
|
||||||
)
|
)
|
||||||
|
|
||||||
component.async_register_entity_service(
|
component.async_register_entity_service(
|
||||||
@ -519,13 +530,15 @@ class Profiles:
|
|||||||
self.data = await self.hass.async_add_executor_job(self._load_profile_data)
|
self.data = await self.hass.async_add_executor_job(self._load_profile_data)
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
def apply_default(self, entity_id: str, params: dict) -> None:
|
def apply_default(self, entity_id: str, state_on: bool, params: dict) -> None:
|
||||||
"""Return the default turn-on profile for the given light."""
|
"""Return the default profile for the given light."""
|
||||||
for _entity_id in (entity_id, "group.all_lights"):
|
for _entity_id in (entity_id, "group.all_lights"):
|
||||||
name = f"{_entity_id}.default"
|
name = f"{_entity_id}.default"
|
||||||
if name in self.data:
|
if name in self.data:
|
||||||
|
if not state_on or not params:
|
||||||
self.apply_profile(name, params)
|
self.apply_profile(name, params)
|
||||||
return
|
elif self.data[name].transition is not None:
|
||||||
|
params.setdefault(ATTR_TRANSITION, self.data[name].transition)
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
def apply_profile(self, name: str, params: dict) -> None:
|
def apply_profile(self, name: str, params: dict) -> None:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user