Skip to content

Commit

Permalink
Merge pull request #121 from Snuffy2/Fix-recorder-errors-in-2024.7
Browse files Browse the repository at this point in the history
Use _unrecorded_attributes instead of recorder_history_prefilter
  • Loading branch information
enkama authored Jul 5, 2024
2 parents 46777ab + 6ce0e75 commit dd62d7a
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 420 deletions.
39 changes: 16 additions & 23 deletions custom_components/variable/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
import logging

from homeassistant.components.binary_sensor import PLATFORM_SCHEMA, BinarySensorEntity
from homeassistant.components.recorder import DATA_INSTANCE as RECORDER_INSTANCE
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import (
ATTR_FRIENDLY_NAME,
ATTR_ICON,
CONF_DEVICE_CLASS,
CONF_ICON,
CONF_NAME,
MATCH_ALL,
STATE_OFF,
STATE_ON,
Platform,
Expand All @@ -35,10 +35,10 @@
CONF_VALUE,
CONF_VARIABLE_ID,
CONF_YAML_VARIABLE,
DEFAULT_EXCLUDE_FROM_RECORDER,
DEFAULT_REPLACE_ATTRIBUTES,
DOMAIN,
)
from .recorder_history_prefilter import recorder_prefilter

_LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -103,7 +103,14 @@ async def async_setup_entry(
# _LOGGER.debug(f"[async_setup_entry] config: {config}")
# _LOGGER.debug(f"[async_setup_entry] unique_id: {unique_id}")

async_add_entities([Variable(hass, config, config_entry, unique_id)])
if config.get(CONF_EXCLUDE_FROM_RECORDER, DEFAULT_EXCLUDE_FROM_RECORDER):
_LOGGER.debug(
f"({config.get(CONF_NAME, config.get(CONF_VARIABLE_ID, None))}) "
"Excluding from Recorder"
)
async_add_entities([VariableNoRecorder(hass, config, config_entry, unique_id)])
else:
async_add_entities([Variable(hass, config, config_entry, unique_id)])

return True

Expand All @@ -119,9 +126,7 @@ def __init__(
unique_id,
):
"""Initialize a Binary Sensor Variable."""
_LOGGER.debug(
f"({config.get(CONF_NAME, config.get(CONF_VARIABLE_ID))}) [init] config: {config}"
)
# _LOGGER.debug(f"({config.get(CONF_NAME, config.get(CONF_VARIABLE_ID))}) [init] config: {config}")
if config.get(CONF_VALUE) is None or (
isinstance(config.get(CONF_VALUE), str)
and config.get(CONF_VALUE).lower() in ["", "none", "unknown", "unavailable"]
Expand All @@ -140,10 +145,7 @@ def __init__(
self._attr_has_entity_name = True
self._variable_id = slugify(config.get(CONF_VARIABLE_ID).lower())
self._attr_unique_id = unique_id
if config.get(CONF_NAME) is not None:
self._attr_name = config.get(CONF_NAME)
else:
self._attr_name = config.get(CONF_VARIABLE_ID)
self._attr_name = config.get(CONF_NAME, config.get(CONF_VARIABLE_ID, None))
self._attr_icon = config.get(CONF_ICON)
self._attr_device_class = config.get(CONF_DEVICE_CLASS)
self._restore = config.get(CONF_RESTORE)
Expand Down Expand Up @@ -174,11 +176,6 @@ def __init__(
if self._exclude_from_recorder:
self.disable_recorder()

def disable_recorder(self):
if RECORDER_INSTANCE in self._hass.data:
_LOGGER.info(f"({self._attr_name}) [disable_recorder] Disabling Recorder")
recorder_prefilter.add_filter(self._hass, self.entity_id)

async def async_added_to_hass(self):
"""Run when entity about to be added."""
await super().async_added_to_hass()
Expand Down Expand Up @@ -229,14 +226,6 @@ async def async_added_to_hass(self):
+ f"{self._config_entry.data.get(CONF_UPDATED)}"
)

async def async_will_remove_from_hass(self) -> None:
"""Run when entity will be removed from hass."""
if RECORDER_INSTANCE in self._hass.data and self._exclude_from_recorder:
_LOGGER.debug(
f"({self._attr_name}) Removing entity exclusion from recorder: {self.entity_id}"
)
recorder_prefilter.remove_filter(self._hass, self.entity_id)

@property
def should_poll(self):
"""If entity should be polled."""
Expand Down Expand Up @@ -387,3 +376,7 @@ async def async_toggle_variable(self, **kwargs) -> None:
)

self.async_write_ha_state()


class VariableNoRecorder(Variable):
_unrecorded_attributes = frozenset({MATCH_ALL})
42 changes: 16 additions & 26 deletions custom_components/variable/device_tracker.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
SourceType,
TrackerEntity,
)
from homeassistant.components.recorder import DATA_INSTANCE as RECORDER_INSTANCE
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import (
ATTR_BATTERY_LEVEL,
Expand All @@ -21,6 +20,7 @@
ATTR_LONGITUDE,
CONF_ICON,
CONF_NAME,
MATCH_ALL,
Platform,
)
from homeassistant.core import HomeAssistant
Expand All @@ -44,10 +44,10 @@
CONF_UPDATED,
CONF_VARIABLE_ID,
CONF_YAML_VARIABLE,
DEFAULT_EXCLUDE_FROM_RECORDER,
DEFAULT_REPLACE_ATTRIBUTES,
DOMAIN,
)
from .recorder_history_prefilter import recorder_prefilter

_LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -103,7 +103,14 @@ async def async_setup_entry(
# _LOGGER.debug(f"[async_setup_entry] config: {config}")
# _LOGGER.debug(f"[async_setup_entry] unique_id: {unique_id}")

async_add_entities([Variable(hass, config, config_entry, unique_id)])
if config.get(CONF_EXCLUDE_FROM_RECORDER, DEFAULT_EXCLUDE_FROM_RECORDER):
_LOGGER.debug(
f"({config.get(CONF_NAME, config.get(CONF_VARIABLE_ID, None))}) "
"Excluding from Recorder."
)
async_add_entities([VariableNoRecorder(hass, config, config_entry, unique_id)])
else:
async_add_entities([Variable(hass, config, config_entry, unique_id)])

return True

Expand All @@ -119,20 +126,14 @@ def __init__(
unique_id,
):
"""Initialize a Device Tracker Variable."""
_LOGGER.debug(
f"({config.get(CONF_NAME, config.get(CONF_VARIABLE_ID))}) [init] config: {config}"
)

# _LOGGER.debug(f"({config.get(CONF_NAME, config.get(CONF_VARIABLE_ID))}) [init] config: {config}")
self._hass = hass
self._config = config
self._config_entry = config_entry
self._attr_has_entity_name = True
self._variable_id = slugify(config.get(CONF_VARIABLE_ID).lower())
self._attr_unique_id = unique_id
if config.get(CONF_NAME) is not None:
self._attr_name = config.get(CONF_NAME)
else:
self._attr_name = config.get(CONF_VARIABLE_ID)
self._attr_name = config.get(CONF_NAME, config.get(CONF_VARIABLE_ID, None))
self._attr_icon = config.get(CONF_ICON)
self._restore = config.get(CONF_RESTORE)
self._force_update = config.get(CONF_FORCE_UPDATE)
Expand Down Expand Up @@ -165,13 +166,6 @@ def __init__(
self._attr_battery_level = config.get(ATTR_BATTERY_LEVEL)
self._attr_location_name = config.get(ATTR_LOCATION_NAME)
self._attr_gps_accuracy = config.get(ATTR_GPS_ACCURACY)
if self._exclude_from_recorder:
self.disable_recorder()

def disable_recorder(self):
if RECORDER_INSTANCE in self._hass.data:
_LOGGER.info(f"({self._attr_name}) [disable_recorder] Disabling Recorder")
recorder_prefilter.add_filter(self._hass, self.entity_id)

async def async_added_to_hass(self):
"""Run when entity about to be added."""
Expand Down Expand Up @@ -207,14 +201,6 @@ async def async_added_to_hass(self):
+ f"{self._config_entry.data.get(CONF_UPDATED)}"
)

async def async_will_remove_from_hass(self) -> None:
"""Run when entity will be removed from hass."""
if RECORDER_INSTANCE in self._hass.data and self._exclude_from_recorder:
_LOGGER.debug(
f"({self._attr_name}) Removing entity exclusion from recorder: {self.entity_id}"
)
recorder_prefilter.remove_filter(self._hass, self.entity_id)

def _update_attr_settings(self, new_attributes=None, just_pop=False):
if new_attributes is not None:
_LOGGER.debug(
Expand Down Expand Up @@ -355,3 +341,7 @@ def state_attributes(self) -> dict[str, StateType]:
if self._attr_location_name is not None:
attr[ATTR_LOCATION_NAME] = self._attr_location_name
return attr


class VariableNoRecorder(Variable):
_unrecorded_attributes = frozenset({MATCH_ALL})
6 changes: 3 additions & 3 deletions custom_components/variable/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
],
"config_flow": true,
"dependencies": [],
"documentation": "https://github.com/Wibias/hass-variables",
"documentation": "https://github.com/enkama/hass-variables",
"iot_class": "local_push",
"issue_tracker": "https://github.com/Wibias/hass-variables/issues",
"requirements": ["iso4217==1.11.20220401"],
"issue_tracker": "https://github.com/enkama/hass-variables/issues",
"requirements": ["iso4217>=1.11.20220401"],
"version": "3.4.3"
}

This file was deleted.

21 changes: 0 additions & 21 deletions custom_components/variable/recorder_history_prefilter/LICENSE

This file was deleted.

121 changes: 0 additions & 121 deletions custom_components/variable/recorder_history_prefilter/README.md

This file was deleted.

Loading

0 comments on commit dd62d7a

Please sign in to comment.