|
| 1 | +#<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> |
| 2 | +# |
| 3 | +# HA RECORDER - EXCLUDE entities FROM BEING ADDED TO HISTORY DATABASE |
| 4 | +# |
| 5 | +# |
| 6 | +# The HA Recorder module was modified in HA 2023.6.0 to no longer allow a custom |
| 7 | +# component to insert entity entity names in the '_exclude_e' list that defined |
| 8 | +# entity entities to not be added to the History database (home_assistant_v2.db). |
| 9 | +# |
| 10 | +# This module fixes that problem by using a code injection process to provide a |
| 11 | +# local prefilter to determine if an entity should be added before the Recorder filter. |
| 12 | +# |
| 13 | +# |
| 14 | +# This injection has two methods: |
| 15 | +# add_filter - Add entities to the filter list |
| 16 | +# ---------- |
| 17 | +# hass - HomeAssistant |
| 18 | +# entities to be filtered - |
| 19 | +# single entity - entity_id (string) |
| 20 | +# multiple entities - list of entity ids |
| 21 | +# |
| 22 | +# 'sensor.' will be added to the beginning of the entity id if |
| 23 | +# it's type is not specifid |
| 24 | +# |
| 25 | +# recorder_prefilter.add_filter(hass, 'filter_id1') |
| 26 | +# recorder_prefilter.add_filter(hass, ['filter_entity2', 'filter_entity3']) |
| 27 | +# |
| 28 | +# |
| 29 | +# remove_filter - Remove entities from the filter list |
| 30 | +# ------------- |
| 31 | +# Same arguments for add_filter |
| 32 | +# |
| 33 | +# recorder_prefilter.remove_filter(hass, 'filter_id1') |
| 34 | +# recorder_prefilter.remove_filter(hass, ['filter_entity2', 'filter_entity3']) |
| 35 | +# |
| 36 | +# |
| 37 | +# Gary Cobb, iCloud3 iDevice Tracker, aka geekstergary |
| 38 | +#<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> |
| 39 | + |
| 40 | +from homeassistant.core import HomeAssistant |
| 41 | +from inspect import getframeinfo, stack |
| 42 | +import logging |
| 43 | +_LOGGER = logging.getLogger(__name__) |
| 44 | + |
| 45 | +VERSION = 1.0 |
| 46 | + |
| 47 | +def add_filter(hass: HomeAssistant, entities=None): |
| 48 | + ''' |
| 49 | + Inject the entity prefilter into the Recorder, remove Recorder listeners, |
| 50 | + reinitialize the Recorder |
| 51 | +
|
| 52 | + Arguments: |
| 53 | + hass - HomeAssistant |
| 54 | + entities - A list of entity entities |
| 55 | + (['gary_last_update', 'lillian_last_update', '*_next_update']) |
| 56 | + - A single entity entity ('gary_last_zone') |
| 57 | +
|
| 58 | + Returns: |
| 59 | + True - The injection was successful |
| 60 | + False - The injection was not successful |
| 61 | + ''' |
| 62 | + |
| 63 | + ha_recorder = hass.data['recorder_instance'] |
| 64 | + |
| 65 | + if ha_recorder is None: |
| 66 | + return False |
| 67 | + |
| 68 | + if hass.data.get('recorder_prefilter') is None: |
| 69 | + rp_data = hass.data['recorder_prefilter'] = {} |
| 70 | + rp_data['injected'] = True |
| 71 | + rp_data['legacy'] = True |
| 72 | + rp_data['exclude_entities'] = [] |
| 73 | + |
| 74 | + try: |
| 75 | + ha_recorder.entity_filter._exclude_e.add(entities) |
| 76 | + return True |
| 77 | + except: |
| 78 | + pass |
| 79 | + |
| 80 | + rp_data['legacy'] = False |
| 81 | + |
| 82 | + if _inject_filter(hass) is False: |
| 83 | + return |
| 84 | + |
| 85 | + _update_filter(hass, entities) |
| 86 | + |
| 87 | + |
| 88 | +def remove_filter(hass: HomeAssistant, entities): |
| 89 | + if hass.data['recorder_prefilter']['legacy']: |
| 90 | + try: |
| 91 | + ha_recorder = hass.data['recorder_instance'] |
| 92 | + ha_recorder.entity_filter._exclude_e.discard(entities) |
| 93 | + return True |
| 94 | + except Exception as err: |
| 95 | + _LOGGER.exception(err) |
| 96 | + |
| 97 | + _update_filter(hass, entities, remove=True) |
| 98 | + |
| 99 | + |
| 100 | +def _inject_filter(hass: HomeAssistant): |
| 101 | + ha_recorder = hass.data['recorder_instance'] |
| 102 | + rp_data = hass.data['recorder_prefilter'] |
| 103 | + recorder_entity_filter = ha_recorder.entity_filter |
| 104 | + recorder_remove_listener = ha_recorder._event_listener |
| 105 | + |
| 106 | + def entity_filter(entity_id): |
| 107 | + """ |
| 108 | + Prefilter an entity to see if it should be excluded from |
| 109 | + the recorder history. |
| 110 | +
|
| 111 | + This function is injected into the recorder, replacing the |
| 112 | + original HA recorder_entity_filter module. |
| 113 | +
|
| 114 | + Return: |
| 115 | + False - The entity should is in the filter list |
| 116 | + Run the original HA recorder_entity_filter function - |
| 117 | + The entity is not in the filter list. |
| 118 | + """ |
| 119 | + if (entity_id |
| 120 | + and entity_id in hass.data['recorder_prefilter']['exclude_entities']): |
| 121 | + return False |
| 122 | + |
| 123 | + return recorder_entity_filter(entity_id) |
| 124 | + |
| 125 | + try: |
| 126 | + _LOGGER.info("Recorder Prefilter Injection Started") |
| 127 | + _LOGGER.debug("Injecting Custom Exclude Entity Prefilter into Recorder") |
| 128 | + ha_recorder.entity_filter = entity_filter |
| 129 | + |
| 130 | + _LOGGER.debug("Removing Recorder Event Listener") |
| 131 | + recorder_remove_listener() |
| 132 | + |
| 133 | + _LOGGER.debug("Reinitializing Recorder Event Listener") |
| 134 | + hass.add_job(ha_recorder.async_initialize) |
| 135 | + |
| 136 | + _LOGGER.info(f"Recorder Prefilter Injection Completed") |
| 137 | + |
| 138 | + return True |
| 139 | + |
| 140 | + except Exception as err: |
| 141 | + _LOGGER.info(f"Recorder Prefilter Injection Failed ({err})") |
| 142 | + _LOGGER.exception(err) |
| 143 | + |
| 144 | + return False |
| 145 | + |
| 146 | + |
| 147 | +def _update_filter(hass: HomeAssistant, entities=None, remove=False): |
| 148 | + """ Update the filtered entity list """ |
| 149 | + |
| 150 | + mode = 'Removed' if remove else 'Added' |
| 151 | + cust_component = _called_from() |
| 152 | + entities_cnt = 1 if type(entities) is str else len(entities) |
| 153 | + |
| 154 | + _LOGGER.debug(f"{mode} Prefilter Entities ({cust_component})-{entities}") |
| 155 | + _LOGGER.info(f"{mode} Recorder Prefilter Entities " |
| 156 | + f"({cust_component})-{entities_cnt}") |
| 157 | + |
| 158 | + entities = [entities] if type(entities) is str else \ |
| 159 | + entities if type(entities) is list else \ |
| 160 | + [] |
| 161 | + |
| 162 | + |
| 163 | + rp_data = hass.data.get('recorder_prefilter') |
| 164 | + rp_exclude_entities = rp_data['exclude_entities'] |
| 165 | + |
| 166 | + for entity in entities: |
| 167 | + if entity.find('.') == -1: |
| 168 | + entity = f"sensor.{entity}" |
| 169 | + if entity not in rp_exclude_entities: |
| 170 | + if remove is False: |
| 171 | + rp_exclude_entities.append(entity) |
| 172 | + elif entity in rp_exclude_entities: |
| 173 | + rp_exclude_entities.remove(entity) |
| 174 | + |
| 175 | + _LOGGER.debug(f"All Prefiltered Entities-{sorted(rp_exclude_entities)}") |
| 176 | + _LOGGER.info(f"Recorder Prefilter Entities Updated, " |
| 177 | + f"Entities Filtered-{len(rp_exclude_entities)}") |
| 178 | + |
| 179 | + |
| 180 | +def _called_from(): |
| 181 | + cust_component = getframeinfo(stack()[0][0]).filename |
| 182 | + return cust_component.split('custom_components/')[1].split('/')[0] |
0 commit comments