From 2e5d24fc65e633e1c75e565fc44ecf191c90cab9 Mon Sep 17 00:00:00 2001 From: Christian Stangier Date: Wed, 12 Jun 2024 19:48:51 +0200 Subject: [PATCH] Fix deprecations in event handling and registry access --- custom_components/auto_areas/auto_area.py | 11 +++-- custom_components/auto_areas/auto_lights.py | 47 +++++++++++-------- custom_components/auto_areas/binary_sensor.py | 26 ++++++---- custom_components/auto_areas/sensor.py | 14 +++--- 4 files changed, 60 insertions(+), 38 deletions(-) diff --git a/custom_components/auto_areas/auto_area.py b/custom_components/auto_areas/auto_area.py index 1ceadda..2cc269d 100644 --- a/custom_components/auto_areas/auto_area.py +++ b/custom_components/auto_areas/auto_area.py @@ -1,6 +1,10 @@ """Core entity functionality.""" from __future__ import annotations from homeassistant.core import HomeAssistant +from homeassistant.helpers.area_registry import async_get as async_get_area_registry +from homeassistant.helpers.device_registry import async_get as async_get_device_registry +from homeassistant.helpers.entity_registry import async_get as async_get_entity_registry + from homeassistant.config_entries import ConfigEntry @@ -28,16 +32,15 @@ def __init__(self, hass: HomeAssistant, entry: ConfigEntry) -> None: self.hass = hass self.config_entry = entry - self.area_registry = self.hass.helpers.area_registry.async_get(self.hass) - self.device_registry = self.hass.helpers.device_registry.async_get(self.hass) - self.entity_registry = self.hass.helpers.entity_registry.async_get(self.hass) + self.area_registry = async_get_area_registry(self.hass) + self.device_registry = async_get_device_registry(self.hass) + self.entity_registry = async_get_entity_registry(self.hass) self.area_id: str = entry.data.get("area") self.area: AreaEntry = self.area_registry.async_get_area(self.area_id) LOGGER.info('🤖 Auto Area "%s" (%s)', entry.title, entry.options) - async def initialize(self): """Subscribe to area changes and reload if necessary.""" LOGGER.info("%s: Initializing after HA start", self.area.name) diff --git a/custom_components/auto_areas/auto_lights.py b/custom_components/auto_areas/auto_lights.py index 3d31517..1dde66d 100644 --- a/custom_components/auto_areas/auto_lights.py +++ b/custom_components/auto_areas/auto_lights.py @@ -1,7 +1,7 @@ """Auto lights.""" from homeassistant.core import State from homeassistant.components.light import DOMAIN as LIGHT_DOMAIN -from homeassistant.helpers.event import async_track_state_change +from homeassistant.helpers.event import async_track_state_change_event from homeassistant.const import ( STATE_ON, SERVICE_TURN_ON, @@ -9,6 +9,7 @@ ATTR_ENTITY_ID, ) from homeassistant.util import slugify +from homeassistant.core import Event from .ha_helpers import get_all_entities @@ -37,25 +38,31 @@ def __init__(self, auto_area) -> None: # Config self.illuminance_threshold = ( - self.auto_area.config_entry.options.get(CONFIG_AUTO_LIGHTS_MAX_ILLUMINANCE) + self.auto_area.config_entry.options.get( + CONFIG_AUTO_LIGHTS_MAX_ILLUMINANCE) or 0 ) self.is_sleeping_area = ( - self.auto_area.config_entry.options.get(CONFIG_IS_SLEEPING_AREA) or False + self.auto_area.config_entry.options.get( + CONFIG_IS_SLEEPING_AREA) or False ) self.excluded_light_entities = ( - self.auto_area.config_entry.options.get(CONFIG_EXCLUDED_LIGHT_ENTITIES) + self.auto_area.config_entry.options.get( + CONFIG_EXCLUDED_LIGHT_ENTITIES) or [] ) self.sleep_mode_entity_id = ( - f"{SLEEP_MODE_SWITCH_ENTITY_PREFIX}{slugify(self.auto_area.area.name)}" + f"{SLEEP_MODE_SWITCH_ENTITY_PREFIX}{ + slugify(self.auto_area.area.name)}" ) self.presence_entity_id = ( - f"{PRESENCE_BINARY_SENSOR_ENTITY_PREFIX}{slugify(self.auto_area.area.name)}" + f"{PRESENCE_BINARY_SENSOR_ENTITY_PREFIX}{ + slugify(self.auto_area.area.name)}" ) self.illuminance_entity_id = ( - f"{ILLUMINANCE_SENSOR_ENTITY_PREFIX}{slugify(self.auto_area.area.name)}" + f"{ILLUMINANCE_SENSOR_ENTITY_PREFIX}{ + slugify(self.auto_area.area.name)}" ) self.light_entity_ids = [ @@ -91,7 +98,7 @@ async def initialize(self): sleep_mode_state = self.hass.states.get(self.sleep_mode_entity_id) if sleep_mode_state: self.sleep_mode_enabled = sleep_mode_state.state == STATE_ON - self.unsubscribe_sleep_mode = async_track_state_change( + self.unsubscribe_sleep_mode = async_track_state_change_event( self.hass, self.sleep_mode_entity_id, self.handle_sleep_mode_state_change, @@ -115,22 +122,24 @@ async def initialize(self): ) self.lights_turned_on = False - self.unsubscribe_presence = async_track_state_change( + self.unsubscribe_presence = async_track_state_change_event( self.auto_area.hass, self.presence_entity_id, self.handle_presence_state_change, ) - self.unsubscribe_illuminance = async_track_state_change( + self.unsubscribe_illuminance = async_track_state_change_event( self.auto_area.hass, self.illuminance_entity_id, self.handle_illuminance_change, ) - async def handle_presence_state_change( - self, entity_id, from_state: State, to_state: State - ): + async def handle_presence_state_change(self, event: Event): """Handle changes in presence.""" + entity_id = event.data.get('entity_id') + from_state = event.data.get('old_state') + to_state = event.data.get('new_state') + previous_state = from_state.state if from_state else "" current_state = to_state.state @@ -194,10 +203,12 @@ async def handle_presence_state_change( ) self.lights_turned_on = False - async def handle_sleep_mode_state_change( - self, entity_id, from_state: State, to_state: State - ): + async def handle_sleep_mode_state_change(self, event: Event): """Handle changes in sleep mode.""" + entity_id = event.data.get('entity_id') + from_state = event.data.get('old_state') + to_state = event.data.get('new_state') + previous_state = from_state.state if from_state else "" current_state = to_state.state if to_state else "" @@ -245,9 +256,7 @@ async def handle_sleep_mode_state_change( ) self.lights_turned_on = True - async def handle_illuminance_change( - self, _entity_id, _from_state: State, _to_state: State - ): + async def handle_illuminance_change(self, _event: Event): """Handle changes in illuminance.""" # Check for presence diff --git a/custom_components/auto_areas/binary_sensor.py b/custom_components/auto_areas/binary_sensor.py index 180038a..f699409 100644 --- a/custom_components/auto_areas/binary_sensor.py +++ b/custom_components/auto_areas/binary_sensor.py @@ -6,10 +6,12 @@ BinarySensorEntity, ) from homeassistant.core import State -from homeassistant.helpers.event import async_track_state_change +from homeassistant.helpers.event import async_track_state_change_event from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity import DeviceInfo from homeassistant.util import slugify +from homeassistant.core import Event + from .ha_helpers import all_states_are_off @@ -43,7 +45,8 @@ def __init__(self, auto_area: AutoArea) -> None: self.presence: bool = None self.presence_entities: list[str] = self.get_presence_entities() self.unsubscribe = None - LOGGER.info("%s: Initialized presence binary sensor", self.auto_area.area.name) + LOGGER.info("%s: Initialized presence binary sensor", + self.auto_area.area.name) @property def name(self): @@ -79,7 +82,8 @@ def is_on(self) -> bool: def get_presence_entities(self) -> list(str): """Collect entities to be used for determining presence.""" entity_ids = [ - f"{PRESENCE_LOCK_SWITCH_ENTITY_PREFIX}{slugify(self.auto_area.area.name)}" + f"{PRESENCE_LOCK_SWITCH_ENTITY_PREFIX}{ + slugify(self.auto_area.area.name)}" ] # include relevant presence entities, but not this sensor: @@ -108,10 +112,11 @@ async def async_added_to_hass(self): ) self.schedule_update_ha_state() - LOGGER.info("%s: Initial presence %s", self.auto_area.area.name, self.presence) + LOGGER.info("%s: Initial presence %s", + self.auto_area.area.name, self.presence) # Subscribe to state changes - self.unsubscribe = async_track_state_change( + self.unsubscribe = async_track_state_change_event( self.hass, self.presence_entities, self.handle_presence_state_change, @@ -122,10 +127,12 @@ async def async_will_remove_from_hass(self) -> None: if self.unsubscribe: self.unsubscribe() - def handle_presence_state_change( - self, entity_id, from_state: State, to_state: State - ): + def handle_presence_state_change(self, event: Event): """Handle state change of any tracked presence sensors.""" + entity_id = event.data.get('entity_id') + from_state = event.data.get('old_state') + to_state = event.data.get('new_state') + previous_state = from_state.state if from_state else "" current_state = to_state.state if to_state else "" @@ -152,6 +159,7 @@ def handle_presence_state_change( PRESENCE_ON_STATES, ): if self.presence: - LOGGER.info("%s: Presence cleared", self.auto_area.area.name) + LOGGER.info("%s: Presence cleared", + self.auto_area.area.name) self.presence = False self.schedule_update_ha_state() diff --git a/custom_components/auto_areas/sensor.py b/custom_components/auto_areas/sensor.py index d1e10ca..c836c86 100644 --- a/custom_components/auto_areas/sensor.py +++ b/custom_components/auto_areas/sensor.py @@ -5,8 +5,9 @@ ) from homeassistant.core import State from homeassistant.helpers.entity_platform import AddEntitiesCallback -from homeassistant.helpers.event import async_track_state_change +from homeassistant.helpers.event import async_track_state_change_event from homeassistant.helpers.entity import DeviceInfo +from homeassistant.core import Event from .const import DOMAIN, LOGGER, ILLUMINANCE_SENSOR_PREFIX, VERSION, NAME from .auto_area import AutoArea @@ -28,7 +29,8 @@ def __init__(self, auto_area: AutoArea) -> None: self.illuminance_entities: list[str] = self.get_illuminance_entities() self.unsubscribe = None self.value = None - LOGGER.info("%s: Initialized illuminance sensor", self.auto_area.area.name) + LOGGER.info("%s: Initialized illuminance sensor", + self.auto_area.area.name) @property def name(self): @@ -102,7 +104,7 @@ async def async_added_to_hass(self): ) # Subscribe to state changes - self.unsubscribe = async_track_state_change( + self.unsubscribe = async_track_state_change_event( self.hass, self.illuminance_entities, self.handle_illuminance_change, @@ -113,10 +115,10 @@ async def async_will_remove_from_hass(self) -> None: if self.unsubscribe: self.unsubscribe() - def handle_illuminance_change( - self, _entity_id, _from_state: State, to_state: State - ): + def handle_illuminance_change(self, event: Event): """Handle state change of any tracked illuminance sensors.""" + to_state = event.data.get('new_state') + if to_state.state not in [ "unknown", "unavailable",