Skip to content

Commit

Permalink
Fix deprecations in event handling and registry access
Browse files Browse the repository at this point in the history
  • Loading branch information
c-st committed Jun 12, 2024
1 parent 18797ff commit 2e5d24f
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 38 deletions.
11 changes: 7 additions & 4 deletions custom_components/auto_areas/auto_area.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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)
Expand Down
47 changes: 28 additions & 19 deletions custom_components/auto_areas/auto_lights.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
"""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,
SERVICE_TURN_OFF,
ATTR_ENTITY_ID,
)
from homeassistant.util import slugify
from homeassistant.core import Event

from .ha_helpers import get_all_entities

Expand Down Expand Up @@ -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 = [
Expand Down Expand Up @@ -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,
Expand All @@ -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

Expand Down Expand Up @@ -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 ""

Expand Down Expand Up @@ -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
Expand Down
26 changes: 17 additions & 9 deletions custom_components/auto_areas/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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,
Expand All @@ -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 ""

Expand All @@ -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()
14 changes: 8 additions & 6 deletions custom_components/auto_areas/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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):
Expand Down Expand Up @@ -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,
Expand All @@ -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",
Expand Down

0 comments on commit 2e5d24f

Please sign in to comment.