From a2157f8a8a50c6031f11882de77ce36f25533c78 Mon Sep 17 00:00:00 2001 From: Phil Bruckner Date: Tue, 4 Jun 2024 15:05:15 -0500 Subject: [PATCH] Add raw attributes to address sensor (#15) --- README.md | 10 +++++++++- custom_components/entity_tz/sensor.py | 3 +++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 77279b5..880883c 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ Creates sensors that have details (time zone, address, etc.) about another entit Type | Description -|- -address | The address where the entity is located +address | The address where the entity is located. See also [Address Sensor](#address-sensor). country | The country the entity is in. Includes a `country_code` attribute. different country | Is `on` when the country where entity is located is different than Home Assistant's country configuration different time | Is `on` when the local time where entity is located is different than Home Assistant's local time @@ -15,6 +15,14 @@ All entities are disabled by default, exept for the "local time" sensor, which is enabled by default for static time zones and zone entities, and the "time zone" sensor, which is enabled by default for all other entities. +### Address Sensor +The address sensor's state will typically be a "combined" address string, containing house number, road, etc. +It will also have attributes for each of the address "details". +The list of possible attributes is documented [here](https://nominatim.org/release-docs/develop/api/Output/#addressdetails). +The exact set of attributes can change over time. +Do not count on any particular attribute always being present. +E.g., in templates, always check for the attribute's presence, or provide a `|default()` filter. + ## Installation ### With HACS [![hacs_badge](https://img.shields.io/badge/HACS-Custom-41BDF5.svg)](https://hacs.xyz/) diff --git a/custom_components/entity_tz/sensor.py b/custom_components/entity_tz/sensor.py index 3dd1c6c..bdc0c35 100644 --- a/custom_components/entity_tz/sensor.py +++ b/custom_components/entity_tz/sensor.py @@ -41,10 +41,13 @@ def __init__(self, entry: ConfigEntry) -> None: async def async_update(self) -> None: """Update sensor.""" + self._attr_extra_state_attributes = {} if not self._sources_valid: return self._attr_native_value = self._entity_loc.address + for attr, value in self._entity_loc.raw["address"].items(): + self._attr_extra_state_attributes[attr] = value class EntityCountrySensor(ETZEntity, SensorEntity):