Track number of persons in a Zone (#68473)
This commit is contained in:
parent
7c71beaa61
commit
28d3117a88
2 changed files with 119 additions and 6 deletions
|
@ -1,6 +1,7 @@
|
||||||
"""Support for the definition of zones."""
|
"""Support for the definition of zones."""
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from collections.abc import Callable
|
||||||
import logging
|
import logging
|
||||||
from typing import Any, cast
|
from typing import Any, cast
|
||||||
|
|
||||||
|
@ -9,6 +10,7 @@ import voluptuous as vol
|
||||||
from homeassistant import config_entries
|
from homeassistant import config_entries
|
||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
ATTR_EDITABLE,
|
ATTR_EDITABLE,
|
||||||
|
ATTR_ENTITY_ID,
|
||||||
ATTR_LATITUDE,
|
ATTR_LATITUDE,
|
||||||
ATTR_LONGITUDE,
|
ATTR_LONGITUDE,
|
||||||
CONF_ICON,
|
CONF_ICON,
|
||||||
|
@ -19,7 +21,10 @@ from homeassistant.const import (
|
||||||
CONF_RADIUS,
|
CONF_RADIUS,
|
||||||
EVENT_CORE_CONFIG_UPDATE,
|
EVENT_CORE_CONFIG_UPDATE,
|
||||||
SERVICE_RELOAD,
|
SERVICE_RELOAD,
|
||||||
|
STATE_HOME,
|
||||||
|
STATE_NOT_HOME,
|
||||||
STATE_UNAVAILABLE,
|
STATE_UNAVAILABLE,
|
||||||
|
STATE_UNKNOWN,
|
||||||
)
|
)
|
||||||
from homeassistant.core import Event, HomeAssistant, ServiceCall, State, callback
|
from homeassistant.core import Event, HomeAssistant, ServiceCall, State, callback
|
||||||
from homeassistant.helpers import (
|
from homeassistant.helpers import (
|
||||||
|
@ -27,6 +32,7 @@ from homeassistant.helpers import (
|
||||||
config_validation as cv,
|
config_validation as cv,
|
||||||
entity,
|
entity,
|
||||||
entity_component,
|
entity_component,
|
||||||
|
event,
|
||||||
service,
|
service,
|
||||||
storage,
|
storage,
|
||||||
)
|
)
|
||||||
|
@ -284,7 +290,10 @@ class Zone(entity.Entity):
|
||||||
"""Initialize the zone."""
|
"""Initialize the zone."""
|
||||||
self._config = config
|
self._config = config
|
||||||
self.editable = True
|
self.editable = True
|
||||||
|
self._attrs: dict | None = None
|
||||||
|
self._remove_listener: Callable[[], None] | None = None
|
||||||
self._generate_attrs()
|
self._generate_attrs()
|
||||||
|
self._persons_in_zone: set[str] = set()
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def from_yaml(cls, config: dict) -> Zone:
|
def from_yaml(cls, config: dict) -> Zone:
|
||||||
|
@ -295,9 +304,9 @@ class Zone(entity.Entity):
|
||||||
return zone
|
return zone
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def state(self) -> str:
|
def state(self) -> int:
|
||||||
"""Return the state property really does nothing for a zone."""
|
"""Return the state property really does nothing for a zone."""
|
||||||
return "zoning"
|
return len(self._persons_in_zone)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def name(self) -> str:
|
def name(self) -> str:
|
||||||
|
@ -327,6 +336,35 @@ class Zone(entity.Entity):
|
||||||
self._generate_attrs()
|
self._generate_attrs()
|
||||||
self.async_write_ha_state()
|
self.async_write_ha_state()
|
||||||
|
|
||||||
|
@callback
|
||||||
|
def _person_state_change_listener(self, evt: Event) -> None:
|
||||||
|
person_entity_id = evt.data[ATTR_ENTITY_ID]
|
||||||
|
cur_count = len(self._persons_in_zone)
|
||||||
|
if self._state_is_in_zone(evt.data.get("new_state")):
|
||||||
|
self._persons_in_zone.add(person_entity_id)
|
||||||
|
elif person_entity_id in self._persons_in_zone:
|
||||||
|
self._persons_in_zone.remove(person_entity_id)
|
||||||
|
|
||||||
|
if len(self._persons_in_zone) != cur_count:
|
||||||
|
self.async_write_ha_state()
|
||||||
|
|
||||||
|
async def async_added_to_hass(self) -> None:
|
||||||
|
"""Run when entity about to be added to hass."""
|
||||||
|
await super().async_added_to_hass()
|
||||||
|
person_domain = "person" # avoid circular import
|
||||||
|
persons = self.hass.states.async_entity_ids(person_domain)
|
||||||
|
for person in persons:
|
||||||
|
if self._state_is_in_zone(self.hass.states.get(person)):
|
||||||
|
self._persons_in_zone.add(person)
|
||||||
|
|
||||||
|
self.async_on_remove(
|
||||||
|
event.async_track_state_change_filtered(
|
||||||
|
self.hass,
|
||||||
|
event.TrackStates(False, set(), {person_domain}),
|
||||||
|
self._person_state_change_listener,
|
||||||
|
).async_remove
|
||||||
|
)
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
def _generate_attrs(self) -> None:
|
def _generate_attrs(self) -> None:
|
||||||
"""Generate new attrs based on config."""
|
"""Generate new attrs based on config."""
|
||||||
|
@ -337,3 +375,20 @@ class Zone(entity.Entity):
|
||||||
ATTR_PASSIVE: self._config[CONF_PASSIVE],
|
ATTR_PASSIVE: self._config[CONF_PASSIVE],
|
||||||
ATTR_EDITABLE: self.editable,
|
ATTR_EDITABLE: self.editable,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@callback
|
||||||
|
def _state_is_in_zone(self, state: State | None) -> bool:
|
||||||
|
"""Return if given state is in zone."""
|
||||||
|
return (
|
||||||
|
state is not None
|
||||||
|
and state.state
|
||||||
|
not in (
|
||||||
|
STATE_NOT_HOME,
|
||||||
|
STATE_UNKNOWN,
|
||||||
|
STATE_UNAVAILABLE,
|
||||||
|
)
|
||||||
|
and (
|
||||||
|
state.state.casefold() == self.name.casefold()
|
||||||
|
or (state.state == STATE_HOME and self.entity_id == ENTITY_ID_HOME)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
|
@ -316,7 +316,7 @@ async def test_load_from_storage(hass, storage_setup):
|
||||||
"""Test set up from storage."""
|
"""Test set up from storage."""
|
||||||
assert await storage_setup()
|
assert await storage_setup()
|
||||||
state = hass.states.get(f"{DOMAIN}.from_storage")
|
state = hass.states.get(f"{DOMAIN}.from_storage")
|
||||||
assert state.state == "zoning"
|
assert state.state == "0"
|
||||||
assert state.name == "from storage"
|
assert state.name == "from storage"
|
||||||
assert state.attributes.get(ATTR_EDITABLE)
|
assert state.attributes.get(ATTR_EDITABLE)
|
||||||
|
|
||||||
|
@ -328,12 +328,12 @@ async def test_editable_state_attribute(hass, storage_setup):
|
||||||
)
|
)
|
||||||
|
|
||||||
state = hass.states.get(f"{DOMAIN}.from_storage")
|
state = hass.states.get(f"{DOMAIN}.from_storage")
|
||||||
assert state.state == "zoning"
|
assert state.state == "0"
|
||||||
assert state.attributes.get(ATTR_FRIENDLY_NAME) == "from storage"
|
assert state.attributes.get(ATTR_FRIENDLY_NAME) == "from storage"
|
||||||
assert state.attributes.get(ATTR_EDITABLE)
|
assert state.attributes.get(ATTR_EDITABLE)
|
||||||
|
|
||||||
state = hass.states.get(f"{DOMAIN}.yaml_option")
|
state = hass.states.get(f"{DOMAIN}.yaml_option")
|
||||||
assert state.state == "zoning"
|
assert state.state == "0"
|
||||||
assert not state.attributes.get(ATTR_EDITABLE)
|
assert not state.attributes.get(ATTR_EDITABLE)
|
||||||
|
|
||||||
|
|
||||||
|
@ -457,7 +457,7 @@ async def test_ws_create(hass, hass_ws_client, storage_setup):
|
||||||
assert resp["success"]
|
assert resp["success"]
|
||||||
|
|
||||||
state = hass.states.get(input_entity_id)
|
state = hass.states.get(input_entity_id)
|
||||||
assert state.state == "zoning"
|
assert state.state == "0"
|
||||||
assert state.attributes["latitude"] == 3
|
assert state.attributes["latitude"] == 3
|
||||||
assert state.attributes["longitude"] == 4
|
assert state.attributes["longitude"] == 4
|
||||||
assert state.attributes["passive"] is True
|
assert state.attributes["passive"] is True
|
||||||
|
@ -503,3 +503,61 @@ async def test_unavailable_zone(hass):
|
||||||
assert zone.async_active_zone(hass, 0.0, 0.01) is None
|
assert zone.async_active_zone(hass, 0.0, 0.01) is None
|
||||||
|
|
||||||
assert zone.in_zone(hass.states.get("zone.bla"), 0, 0) is False
|
assert zone.in_zone(hass.states.get("zone.bla"), 0, 0) is False
|
||||||
|
|
||||||
|
|
||||||
|
async def test_state(hass):
|
||||||
|
"""Test the state of a zone."""
|
||||||
|
info = {
|
||||||
|
"name": "Test Zone",
|
||||||
|
"latitude": 32.880837,
|
||||||
|
"longitude": -117.237561,
|
||||||
|
"radius": 250,
|
||||||
|
"passive": False,
|
||||||
|
}
|
||||||
|
assert await setup.async_setup_component(hass, zone.DOMAIN, {"zone": info})
|
||||||
|
|
||||||
|
assert len(hass.states.async_entity_ids("zone")) == 2
|
||||||
|
state = hass.states.get("zone.test_zone")
|
||||||
|
assert state.state == "0"
|
||||||
|
|
||||||
|
# Person entity enters zone
|
||||||
|
hass.states.async_set(
|
||||||
|
"person.person1",
|
||||||
|
"Test Zone",
|
||||||
|
)
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
assert hass.states.get("zone.test_zone").state == "1"
|
||||||
|
assert hass.states.get("zone.home").state == "0"
|
||||||
|
|
||||||
|
# Person entity enters zone (case insensitive)
|
||||||
|
hass.states.async_set(
|
||||||
|
"person.person2",
|
||||||
|
"TEST zone",
|
||||||
|
)
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
assert hass.states.get("zone.test_zone").state == "2"
|
||||||
|
assert hass.states.get("zone.home").state == "0"
|
||||||
|
|
||||||
|
# Person entity enters another zone
|
||||||
|
hass.states.async_set(
|
||||||
|
"person.person1",
|
||||||
|
"home",
|
||||||
|
)
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
assert hass.states.get("zone.test_zone").state == "1"
|
||||||
|
assert hass.states.get("zone.home").state == "1"
|
||||||
|
|
||||||
|
# Person entity enters not_home
|
||||||
|
hass.states.async_set(
|
||||||
|
"person.person1",
|
||||||
|
"not_home",
|
||||||
|
)
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
assert hass.states.get("zone.test_zone").state == "1"
|
||||||
|
assert hass.states.get("zone.home").state == "0"
|
||||||
|
|
||||||
|
# Person entity removed
|
||||||
|
hass.states.async_remove("person.person2")
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
assert hass.states.get("zone.test_zone").state == "0"
|
||||||
|
assert hass.states.get("zone.home").state == "0"
|
||||||
|
|
Loading…
Add table
Reference in a new issue