mirror of
https://github.com/home-assistant/core.git
synced 2025-07-16 01:37:08 +00:00
Convert proximity to be async (#93241)
This commit is contained in:
parent
3cdcfb1654
commit
be08bb5ba6
@ -13,10 +13,10 @@ from homeassistant.const import (
|
||||
CONF_ZONE,
|
||||
UnitOfLength,
|
||||
)
|
||||
from homeassistant.core import HomeAssistant, State
|
||||
from homeassistant.core import HomeAssistant, State, callback
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
from homeassistant.helpers.entity import Entity
|
||||
from homeassistant.helpers.event import track_state_change
|
||||
from homeassistant.helpers.event import async_track_state_change
|
||||
from homeassistant.helpers.typing import ConfigType
|
||||
from homeassistant.util.location import distance
|
||||
from homeassistant.util.unit_conversion import DistanceConverter
|
||||
@ -62,7 +62,8 @@ CONFIG_SCHEMA = vol.Schema(
|
||||
)
|
||||
|
||||
|
||||
def setup_proximity_component(
|
||||
@callback
|
||||
def async_setup_proximity_component(
|
||||
hass: HomeAssistant, name: str, config: ConfigType
|
||||
) -> bool:
|
||||
"""Set up the individual proximity component."""
|
||||
@ -89,17 +90,19 @@ def setup_proximity_component(
|
||||
)
|
||||
proximity.entity_id = f"{DOMAIN}.{proximity_zone}"
|
||||
|
||||
proximity.schedule_update_ha_state()
|
||||
proximity.async_write_ha_state()
|
||||
|
||||
track_state_change(hass, proximity_devices, proximity.check_proximity_state_change)
|
||||
async_track_state_change(
|
||||
hass, proximity_devices, proximity.async_check_proximity_state_change
|
||||
)
|
||||
|
||||
return True
|
||||
|
||||
|
||||
def setup(hass: HomeAssistant, config: ConfigType) -> bool:
|
||||
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
|
||||
"""Get the zones and offsets from configuration.yaml."""
|
||||
for zone, proximity_config in config[DOMAIN].items():
|
||||
setup_proximity_component(hass, zone, proximity_config)
|
||||
async_setup_proximity_component(hass, zone, proximity_config)
|
||||
|
||||
return True
|
||||
|
||||
@ -152,7 +155,8 @@ class Proximity(Entity):
|
||||
"""Return the state attributes."""
|
||||
return {ATTR_DIR_OF_TRAVEL: self.dir_of_travel, ATTR_NEAREST: self.nearest}
|
||||
|
||||
def check_proximity_state_change(
|
||||
@callback
|
||||
def async_check_proximity_state_change(
|
||||
self, entity: str, old_state: State | None, new_state: State | None
|
||||
) -> None:
|
||||
"""Perform the proximity checking."""
|
||||
@ -192,7 +196,7 @@ class Proximity(Entity):
|
||||
self.dist_to = "not set"
|
||||
self.dir_of_travel = "not set"
|
||||
self.nearest = "not set"
|
||||
self.schedule_update_ha_state()
|
||||
self.async_write_ha_state()
|
||||
return
|
||||
|
||||
# At least one device is in the monitored zone so update the entity.
|
||||
@ -200,7 +204,7 @@ class Proximity(Entity):
|
||||
self.dist_to = 0
|
||||
self.dir_of_travel = "arrived"
|
||||
self.nearest = devices_in_zone
|
||||
self.schedule_update_ha_state()
|
||||
self.async_write_ha_state()
|
||||
return
|
||||
|
||||
# We can't check proximity because latitude and longitude don't exist.
|
||||
@ -254,7 +258,7 @@ class Proximity(Entity):
|
||||
device_state = self.hass.states.get(closest_device)
|
||||
assert device_state
|
||||
self.nearest = device_state.name
|
||||
self.schedule_update_ha_state()
|
||||
self.async_write_ha_state()
|
||||
return
|
||||
|
||||
# Stop if we cannot calculate the direction of travel (i.e. we don't
|
||||
@ -263,7 +267,7 @@ class Proximity(Entity):
|
||||
self.dist_to = round(distances_to_zone[entity])
|
||||
self.dir_of_travel = "unknown"
|
||||
self.nearest = entity_name
|
||||
self.schedule_update_ha_state()
|
||||
self.async_write_ha_state()
|
||||
return
|
||||
|
||||
# Reset the variables
|
||||
@ -299,7 +303,7 @@ class Proximity(Entity):
|
||||
)
|
||||
self.dir_of_travel = direction_of_travel
|
||||
self.nearest = entity_name
|
||||
self.schedule_update_ha_state()
|
||||
self.async_write_ha_state()
|
||||
_LOGGER.debug(
|
||||
"proximity.%s update entity: distance=%s: direction=%s: device=%s",
|
||||
self.friendly_name,
|
||||
|
Loading…
x
Reference in New Issue
Block a user