Migrate device_sun_light_trigger to use async_track_state_change_event (#115555)

* Migrate device_sun_light_trigger to use async_track_state_change_event

async_track_state_change is legacy and will eventually be deprecated after
all core usage is removed. There are only two places left

* coverage
This commit is contained in:
J. Nick Koston 2024-04-18 09:45:06 -05:00 committed by GitHub
parent 80d6cdad67
commit b18f1ac265
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 35 additions and 13 deletions

View file

@ -1,6 +1,7 @@
"""Support to turn on lights based on the states.""" """Support to turn on lights based on the states."""
from datetime import timedelta from datetime import timedelta
from functools import partial
import logging import logging
import voluptuous as vol import voluptuous as vol
@ -27,11 +28,11 @@ from homeassistant.const import (
SUN_EVENT_SUNRISE, SUN_EVENT_SUNRISE,
SUN_EVENT_SUNSET, SUN_EVENT_SUNSET,
) )
from homeassistant.core import HomeAssistant, callback from homeassistant.core import Event, EventStateChangedData, HomeAssistant, callback
import homeassistant.helpers.config_validation as cv import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.event import ( from homeassistant.helpers.event import (
async_track_point_in_utc_time, async_track_point_in_utc_time,
async_track_state_change, async_track_state_change_event,
) )
from homeassistant.helpers.sun import get_astral_event_next, is_up from homeassistant.helpers.sun import get_astral_event_next, is_up
from homeassistant.helpers.typing import ConfigType from homeassistant.helpers.typing import ConfigType
@ -195,8 +196,20 @@ async def activate_automation( # noqa: C901
schedule_light_turn_on(None) schedule_light_turn_on(None)
@callback @callback
def check_light_on_dev_state_change(entity, old_state, new_state): def check_light_on_dev_state_change(
from_state: str, to_state: str, event: Event[EventStateChangedData]
) -> None:
"""Handle tracked device state changes.""" """Handle tracked device state changes."""
event_data = event.data
if (
(old_state := event_data["old_state"]) is None
or (new_state := event_data["new_state"]) is None
or old_state.state != from_state
or new_state.state != to_state
):
return
entity = event_data["entity_id"]
lights_are_on = any_light_on() lights_are_on = any_light_on()
light_needed = not (lights_are_on or is_up(hass)) light_needed = not (lights_are_on or is_up(hass))
@ -237,12 +250,10 @@ async def activate_automation( # noqa: C901
# will all the following then, break. # will all the following then, break.
break break
async_track_state_change( async_track_state_change_event(
hass, hass,
device_entity_ids, device_entity_ids,
check_light_on_dev_state_change, partial(check_light_on_dev_state_change, STATE_NOT_HOME, STATE_HOME),
STATE_NOT_HOME,
STATE_HOME,
) )
if disable_turn_off: if disable_turn_off:
@ -266,12 +277,10 @@ async def activate_automation( # noqa: C901
) )
) )
async_track_state_change( async_track_state_change_event(
hass, hass,
device_entity_ids, device_entity_ids,
turn_off_lights_when_all_leave, partial(turn_off_lights_when_all_leave, STATE_HOME, STATE_NOT_HOME),
STATE_HOME,
STATE_NOT_HOME,
) )
return return

View file

@ -22,6 +22,7 @@ from homeassistant.const import (
STATE_NOT_HOME, STATE_NOT_HOME,
STATE_OFF, STATE_OFF,
STATE_ON, STATE_ON,
STATE_UNKNOWN,
) )
from homeassistant.core import CoreState, HomeAssistant from homeassistant.core import CoreState, HomeAssistant
from homeassistant.setup import async_setup_component from homeassistant.setup import async_setup_component
@ -150,10 +151,22 @@ async def test_lights_turn_on_when_coming_home_after_sun_set(
hass, device_sun_light_trigger.DOMAIN, {device_sun_light_trigger.DOMAIN: {}} hass, device_sun_light_trigger.DOMAIN, {device_sun_light_trigger.DOMAIN: {}}
) )
hass.states.async_set(f"{DOMAIN}.device_2", STATE_HOME) hass.states.async_set(f"{DOMAIN}.device_2", STATE_UNKNOWN)
await hass.async_block_till_done() await hass.async_block_till_done()
assert all(
hass.states.get(ent_id).state == STATE_OFF
for ent_id in hass.states.async_entity_ids("light")
)
hass.states.async_set(f"{DOMAIN}.device_2", STATE_NOT_HOME)
await hass.async_block_till_done()
assert all(
hass.states.get(ent_id).state == STATE_OFF
for ent_id in hass.states.async_entity_ids("light")
)
hass.states.async_set(f"{DOMAIN}.device_2", STATE_HOME)
await hass.async_block_till_done()
assert all( assert all(
hass.states.get(ent_id).state == light.STATE_ON hass.states.get(ent_id).state == light.STATE_ON
for ent_id in hass.states.async_entity_ids("light") for ent_id in hass.states.async_entity_ids("light")