Skip to content

Commit

Permalink
Merge pull request #185 from Snuffy2/Fix-Disable-Recorder
Browse files Browse the repository at this point in the history
Fix-Disable-Recorder
  • Loading branch information
Snuffy2 committed Jul 6, 2023
2 parents 88e52b0 + 9a0726c commit 7c9de27
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 25 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions custom_components/places/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
DOMAIN = "places"
VERSION = "v2.4"
EVENT_TYPE = DOMAIN + "_state_update"
PLATFORM = Platform.SENSOR
ENTITY_ID_FORMAT = Platform.SENSOR + ".{}"

# Defaults
Expand Down
1 change: 1 addition & 0 deletions custom_components/places/recorder_history_prefilter
50 changes: 25 additions & 25 deletions custom_components/places/sensor.py
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -136,11 +137,13 @@
JSON_ATTRIBUTE_LIST,
JSON_IGNORE_ATTRIBUTE_LIST,
PLACE_NAME_DUPLICATE_LIST,
PLATFORM,
RESET_ATTRIBUTE_LIST,
TRACKING_DOMAINS,
TRACKING_DOMAINS_NEED_LATLONG,
VERSION,
)
from .recorder_history_prefilter import recorder_prefilter

_LOGGER = logging.getLogger(__name__)
try:
Expand Down Expand Up @@ -382,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))
Expand Down Expand Up @@ -514,24 +525,14 @@ 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:
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}"
)
else:
_LOGGER.debug(
f"({self.get_attr(CONF_NAME)}) [disable_recorder] _exclude_e: {ha_history_recorder.entity_filter._exclude_e}"
)

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}"
Expand Down Expand Up @@ -593,13 +594,11 @@ 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:
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)
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}"
)
recorder_prefilter.remove_filter(self._hass, self.entity_id)

# Only do this if no places entities with extended_attr exist
ex_attr_count = 0
Expand All @@ -613,6 +612,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
Expand Down

0 comments on commit 7c9de27

Please sign in to comment.