From bbbc97ca071338755127ee3031e3729dc7f8e9cd Mon Sep 17 00:00:00 2001 From: GitHub Actions Date: Mon, 19 Jun 2023 20:19:18 +0000 Subject: [PATCH 1/5] Handle legacy <2023.6 --- custom_components/places/sensor.py | 36 ++++++++++++++++++------------ 1 file changed, 22 insertions(+), 14 deletions(-) diff --git a/custom_components/places/sensor.py b/custom_components/places/sensor.py index 4dffa6b5..3ff262c7 100644 --- a/custom_components/places/sensor.py +++ b/custom_components/places/sensor.py @@ -520,22 +520,25 @@ def disable_recorder(self): f"({self.get_attr(CONF_NAME)}) [disable_recorder] Extended Attributes is True, Disabling Recorder" ) if self.entity_id: + # Legacy disable recorder if HA <2023.6 try: ha_history_recorder.entity_filter._exclude_e.add(self.entity_id) - - except AttributeError as e: - _LOGGER.warning( - f"({self.get_attr(CONF_NAME)}) [disable_recorder] AttributeError trying to disable Recorder: {e}" - ) + except AttributeError: + pass else: _LOGGER.debug( f"({self.get_attr(CONF_NAME)}) [disable_recorder] _exclude_e: {ha_history_recorder.entity_filter._exclude_e}" ) - - ha_history_recorder.exclude_event_types.add(EVENT_TYPE) - _LOGGER.debug( - f"({self.get_attr(CONF_NAME)}) [disable_recorder] exclude_event_types: {ha_history_recorder.exclude_event_types}" - ) + try: + ha_history_recorder.exclude_event_types.add(EVENT_TYPE) + except AttributeError as e: + _LOGGER.warning( + f"({self.get_attr(CONF_NAME)}) [disable_recorder] AttributeError trying to exclude event from Recorder: {e}" + ) + else: + _LOGGER.debug( + f"({self.get_attr(CONF_NAME)}) [disable_recorder] exclude_event_types: {ha_history_recorder.exclude_event_types}" + ) def get_dict_from_json_file(self): sensor_attributes = {} @@ -596,10 +599,15 @@ async def async_will_remove_from_hass(self) -> None: if RECORDER_INSTANCE in self._hass.data: ha_history_recorder = self._hass.data[RECORDER_INSTANCE] if self.entity_id: - _LOGGER.debug( - f"({self.get_attr(CONF_NAME)}) Removing entity exclusion from recorder: {self.entity_id}" - ) - ha_history_recorder.entity_filter._exclude_e.discard(self.entity_id) + # Remove legacy recorder if HA <2023.6 + try: + ha_history_recorder.entity_filter._exclude_e.discard(self.entity_id) + except AttributeError: + pass + else: + _LOGGER.debug( + f"({self._attr_name}) Removing entity exclusion from recorder: {self.entity_id}" + ) # Only do this if no places entities with extended_attr exist ex_attr_count = 0 From d1d8f5546a04526eb182841614e9aad2334aa95d Mon Sep 17 00:00:00 2001 From: Snuffy2 Date: Thu, 22 Jun 2023 21:44:28 -0400 Subject: [PATCH 2/5] Add recorder_history_prefilter Submodule Co-Authored-By: Gary Cobb --- .gitmodules | 3 +++ custom_components/places/recorder_history_prefilter | 1 + 2 files changed, 4 insertions(+) create mode 100644 .gitmodules create mode 160000 custom_components/places/recorder_history_prefilter diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 00000000..d3f49d74 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "custom_components/places/recorder_history_prefilter"] + path = custom_components/places/recorder_history_prefilter + url = https://github.com/gcobb321/recorder_history_prefilter diff --git a/custom_components/places/recorder_history_prefilter b/custom_components/places/recorder_history_prefilter new file mode 160000 index 00000000..9a1e1426 --- /dev/null +++ b/custom_components/places/recorder_history_prefilter @@ -0,0 +1 @@ +Subproject commit 9a1e1426aa4fb5f1bcc82a868f989fdb4ebe9451 From 202459d6e19a7ebdcf70f6cb67da884fb6f89479 Mon Sep 17 00:00:00 2001 From: Snuffy2 Date: Thu, 22 Jun 2023 22:20:07 -0400 Subject: [PATCH 3/5] Implement recorder_prefilter --- custom_components/places/sensor.py | 50 ++++++++++-------------------- 1 file changed, 16 insertions(+), 34 deletions(-) diff --git a/custom_components/places/sensor.py b/custom_components/places/sensor.py index 3ff262c7..d2578791 100644 --- a/custom_components/places/sensor.py +++ b/custom_components/places/sensor.py @@ -1,8 +1,8 @@ """ Place Support for OpenStreetMap Geocode sensors. -Original Author: Jim Thompson -Subsequent Authors: Ian Richardson & Snuffy2 +Previous Authors: Jim Thompson, Ian Richardson +Current Author: Snuffy2 Description: Provides a sensor with a variable state consisting of reverse geocode (place) details for a linked device_tracker entity that provides GPS co-ordinates (ie owntracks, icloud) @@ -141,6 +141,7 @@ TRACKING_DOMAINS_NEED_LATLONG, VERSION, ) +from .recorder_history_prefilter import recorder_prefilter _LOGGER = logging.getLogger(__name__) try: @@ -514,31 +515,18 @@ def __init__(self, hass, config, config_entry, name, unique_id): ) def disable_recorder(self): + if RECORDER_INSTANCE in self._hass.data: - ha_history_recorder = self._hass.data[RECORDER_INSTANCE] _LOGGER.info( f"({self.get_attr(CONF_NAME)}) [disable_recorder] Extended Attributes is True, Disabling Recorder" ) - if self.entity_id: - # Legacy disable recorder if HA <2023.6 - try: - ha_history_recorder.entity_filter._exclude_e.add(self.entity_id) - except AttributeError: - pass - else: - _LOGGER.debug( - f"({self.get_attr(CONF_NAME)}) [disable_recorder] _exclude_e: {ha_history_recorder.entity_filter._exclude_e}" - ) - try: - ha_history_recorder.exclude_event_types.add(EVENT_TYPE) - except AttributeError as e: - _LOGGER.warning( - f"({self.get_attr(CONF_NAME)}) [disable_recorder] AttributeError trying to exclude event from Recorder: {e}" - ) - else: - _LOGGER.debug( - f"({self.get_attr(CONF_NAME)}) [disable_recorder] exclude_event_types: {ha_history_recorder.exclude_event_types}" - ) + + recorder_prefilter.add_filter(self._hass, self.entity_id) + ha_history_recorder = self._hass.data[RECORDER_INSTANCE] + ha_history_recorder.exclude_event_types.add(EVENT_TYPE) + _LOGGER.debug( + f"({self.get_attr(CONF_NAME)}) [disable_recorder] exclude_event_types: {ha_history_recorder.exclude_event_types}" + ) def get_dict_from_json_file(self): sensor_attributes = {} @@ -597,17 +585,10 @@ async def async_will_remove_from_hass(self) -> None: + f"{self.get_attr(ATTR_JSON_FILENAME)}" ) if RECORDER_INSTANCE in self._hass.data: - ha_history_recorder = self._hass.data[RECORDER_INSTANCE] - if self.entity_id: - # Remove legacy recorder if HA <2023.6 - try: - ha_history_recorder.entity_filter._exclude_e.discard(self.entity_id) - except AttributeError: - pass - else: - _LOGGER.debug( - f"({self._attr_name}) Removing entity exclusion from recorder: {self.entity_id}" - ) + _LOGGER.debug( + f"({self._attr_name}) Removing entity exclusion from recorder: {self.entity_id}" + ) + recorder_prefilter.remove_filter(self._hass, self.entity_id) # Only do this if no places entities with extended_attr exist ex_attr_count = 0 @@ -621,6 +602,7 @@ async def async_will_remove_from_hass(self) -> None: _LOGGER.debug( f"({self.get_attr(CONF_NAME)}) Removing event exclusion from recorder: {EVENT_TYPE}" ) + ha_history_recorder = self._hass.data[RECORDER_INSTANCE] ha_history_recorder.exclude_event_types.discard(EVENT_TYPE) @property From 371b053874d44bd3c4d4811d7fc85e33d2025167 Mon Sep 17 00:00:00 2001 From: Snuffy2 Date: Tue, 27 Jun 2023 21:45:56 -0400 Subject: [PATCH 4/5] Get current entity_id in config --- custom_components/places/const.py | 1 + custom_components/places/sensor.py | 16 +++++++++++++--- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/custom_components/places/const.py b/custom_components/places/const.py index a6932bcd..1f34f2f4 100644 --- a/custom_components/places/const.py +++ b/custom_components/places/const.py @@ -11,6 +11,7 @@ DOMAIN = "places" VERSION = "v2.4" EVENT_TYPE = DOMAIN + "_state_update" +PLATFORM = Platform.SENSOR ENTITY_ID_FORMAT = Platform.SENSOR + ".{}" # Defaults diff --git a/custom_components/places/sensor.py b/custom_components/places/sensor.py index d2578791..3ba351cb 100644 --- a/custom_components/places/sensor.py +++ b/custom_components/places/sensor.py @@ -21,6 +21,7 @@ from datetime import datetime, timedelta import homeassistant.helpers.config_validation as cv +import homeassistant.helpers.entity_registry as er import requests import voluptuous as vol from homeassistant import config_entries, core @@ -136,6 +137,7 @@ JSON_ATTRIBUTE_LIST, JSON_IGNORE_ATTRIBUTE_LIST, PLACE_NAME_DUPLICATE_LIST, + PLATFORM, RESET_ATTRIBUTE_LIST, TRACKING_DOMAINS, TRACKING_DOMAINS_NEED_LATLONG, @@ -383,13 +385,21 @@ def __init__(self, hass, config, config_entry, name, unique_id): self._config = config self._config_entry = config_entry self._hass = hass - self.entity_id = generate_entity_id( - ENTITY_ID_FORMAT, slugify(name.lower()), hass=self._hass - ) self.set_attr(CONF_NAME, name) self._attr_name = name self.set_attr(CONF_UNIQUE_ID, unique_id) self._attr_unique_id = unique_id + registry = er.async_get(self._hass) + current_entity_id = registry.async_get_entity_id( + PLATFORM, DOMAIN, self._attr_unique_id + ) + if current_entity_id is not None: + self.entity_id = current_entity_id + else: + self.entity_id = generate_entity_id( + ENTITY_ID_FORMAT, slugify(name.lower()), hass=self._hass + ) + _LOGGER.debug(f"({self._attr_name}) entity_id: {self.entity_id}") self.set_attr(CONF_ICON, DEFAULT_ICON) self._attr_icon = DEFAULT_ICON self.set_attr(CONF_API_KEY, config.get(CONF_API_KEY)) From 9a0726c0ba85fce019e2237117d53a0f464390c1 Mon Sep 17 00:00:00 2001 From: Snuffy2 Date: Tue, 4 Jul 2023 12:33:14 -0400 Subject: [PATCH 5/5] On Unload, only remove from recorder_prefilter if Extended Attr true --- custom_components/places/sensor.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/custom_components/places/sensor.py b/custom_components/places/sensor.py index 3ba351cb..1f207499 100644 --- a/custom_components/places/sensor.py +++ b/custom_components/places/sensor.py @@ -594,7 +594,7 @@ async def async_will_remove_from_hass(self) -> None: f"({self.get_attr(CONF_NAME)}) JSON sensor file removed: " + f"{self.get_attr(ATTR_JSON_FILENAME)}" ) - if RECORDER_INSTANCE in self._hass.data: + if RECORDER_INSTANCE in self._hass.data and self.get_attr(CONF_EXTENDED_ATTR): _LOGGER.debug( f"({self._attr_name}) Removing entity exclusion from recorder: {self.entity_id}" )