Add device_trackers attribute to person entities (#91312)

pull/91867/head
David Poll 2023-04-22 12:26:12 -05:00 committed by GitHub
parent 94a1429c65
commit 93a87d3c82
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 92 additions and 1 deletions

View File

@ -56,6 +56,7 @@ _LOGGER = logging.getLogger(__name__)
ATTR_SOURCE = "source"
ATTR_USER_ID = "user_id"
ATTR_DEVICE_TRACKERS = "device_trackers"
CONF_DEVICE_TRACKERS = "device_trackers"
CONF_USER_ID = "user_id"
@ -446,6 +447,7 @@ class Person(collection.CollectionEntity, RestoreEntity):
data[ATTR_SOURCE] = self._source
if (user_id := self._config.get(CONF_USER_ID)) is not None:
data[ATTR_USER_ID] = user_id
data[ATTR_DEVICE_TRACKERS] = self.device_trackers
return data
@property

View File

@ -0,0 +1,12 @@
"""Integration platform for recorder."""
from __future__ import annotations
from homeassistant.core import HomeAssistant, callback
from . import ATTR_DEVICE_TRACKERS
@callback
def exclude_attributes(hass: HomeAssistant) -> set[str]:
"""Exclude large and chatty update attributes from being recorded."""
return {ATTR_DEVICE_TRACKERS}

View File

@ -6,6 +6,23 @@
"state": {
"home": "[%key:common::state::home%]",
"not_home": "[%key:common::state::not_home%]"
},
"state_attributes": {
"device_trackers": {
"name": "Device trackers"
},
"gps_accuracy": {
"name": "[%key:component::device_tracker::entity_component::_::state_attributes::gps_accuracy::name%]"
},
"latitude": {
"name": "[%key:component::device_tracker::entity_component::_::state_attributes::latitude::name%]"
},
"longitude": {
"name": "[%key:component::device_tracker::entity_component::_::state_attributes::longitude::name%]"
},
"source": {
"name": "Source"
}
}
}
}

View File

@ -7,7 +7,12 @@ import pytest
from homeassistant.components import person
from homeassistant.components.device_tracker import ATTR_SOURCE_TYPE, SourceType
from homeassistant.components.person import ATTR_SOURCE, ATTR_USER_ID, DOMAIN
from homeassistant.components.person import (
ATTR_DEVICE_TRACKERS,
ATTR_SOURCE,
ATTR_USER_ID,
DOMAIN,
)
from homeassistant.const import (
ATTR_ENTITY_PICTURE,
ATTR_GPS_ACCURACY,
@ -165,6 +170,7 @@ async def test_setup_tracker(hass: HomeAssistant, hass_admin_user: MockUser) ->
assert state.attributes.get(ATTR_LONGITUDE) is None
assert state.attributes.get(ATTR_SOURCE) == DEVICE_TRACKER
assert state.attributes.get(ATTR_USER_ID) == user_id
assert state.attributes.get(ATTR_DEVICE_TRACKERS) == [DEVICE_TRACKER]
hass.states.async_set(
DEVICE_TRACKER,
@ -181,6 +187,7 @@ async def test_setup_tracker(hass: HomeAssistant, hass_admin_user: MockUser) ->
assert state.attributes.get(ATTR_GPS_ACCURACY) == 10
assert state.attributes.get(ATTR_SOURCE) == DEVICE_TRACKER
assert state.attributes.get(ATTR_USER_ID) == user_id
assert state.attributes.get(ATTR_DEVICE_TRACKERS) == [DEVICE_TRACKER]
async def test_setup_two_trackers(
@ -220,6 +227,10 @@ async def test_setup_two_trackers(
assert state.attributes.get(ATTR_GPS_ACCURACY) is None
assert state.attributes.get(ATTR_SOURCE) == DEVICE_TRACKER
assert state.attributes.get(ATTR_USER_ID) == user_id
assert state.attributes.get(ATTR_DEVICE_TRACKERS) == [
DEVICE_TRACKER,
DEVICE_TRACKER_2,
]
hass.states.async_set(
DEVICE_TRACKER_2,
@ -245,6 +256,10 @@ async def test_setup_two_trackers(
assert state.attributes.get(ATTR_GPS_ACCURACY) == 12
assert state.attributes.get(ATTR_SOURCE) == DEVICE_TRACKER_2
assert state.attributes.get(ATTR_USER_ID) == user_id
assert state.attributes.get(ATTR_DEVICE_TRACKERS) == [
DEVICE_TRACKER,
DEVICE_TRACKER_2,
]
hass.states.async_set(DEVICE_TRACKER_2, "zone1", {ATTR_SOURCE_TYPE: SourceType.GPS})
await hass.async_block_till_done()

View File

@ -0,0 +1,45 @@
"""The tests for update recorder."""
from __future__ import annotations
from datetime import timedelta
from homeassistant.components.person import ATTR_DEVICE_TRACKERS, DOMAIN
from homeassistant.components.recorder import Recorder
from homeassistant.components.recorder.history import get_significant_states
from homeassistant.core import HomeAssistant
from homeassistant.setup import async_setup_component
from homeassistant.util import dt as dt_util
from tests.common import async_fire_time_changed
from tests.components.recorder.common import async_wait_recording_done
async def test_exclude_attributes(
recorder_mock: Recorder,
hass: HomeAssistant,
enable_custom_integrations: None,
) -> None:
"""Test update attributes to be excluded."""
now = dt_util.utcnow()
config = {
DOMAIN: {
"id": "1234",
"name": "test person",
"user_id": "test_user_id",
"device_trackers": ["device_tracker.test"],
}
}
assert await async_setup_component(hass, DOMAIN, config)
await hass.async_block_till_done()
async_fire_time_changed(hass, dt_util.utcnow() + timedelta(minutes=5))
await hass.async_block_till_done()
await async_wait_recording_done(hass)
states = await hass.async_add_executor_job(
get_significant_states, hass, now, None, hass.states.async_entity_ids()
)
assert len(states) >= 1
for entity_states in states.values():
for state in entity_states:
assert ATTR_DEVICE_TRACKERS not in state.attributes