Skip to content

Commit

Permalink
🗑️ add purge info entities task (#2320)
Browse files Browse the repository at this point in the history
  • Loading branch information
al-one committed Feb 10, 2025
1 parent f339013 commit 3627da6
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
20 changes: 19 additions & 1 deletion custom_components/xiaomi_miot/core/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from homeassistant.const import CONF_HOST, CONF_TOKEN, CONF_MODEL, CONF_USERNAME, EntityCategory
from homeassistant.util import dt
from homeassistant.components import persistent_notification
from homeassistant.helpers.event import async_call_later
from homeassistant.helpers.event import async_call_later, async_track_time_interval
import homeassistant.helpers.device_registry as dr

from .const import (
Expand Down Expand Up @@ -166,6 +166,7 @@ class Device(CustomConfigHelper):
_exclude_miot_services = None
_exclude_miot_properties = None
_unreadable_properties = None
_unsub_purge = None

def __init__(self, info: DeviceInfo, entry: HassEntry):
self.data = {}
Expand Down Expand Up @@ -206,13 +207,20 @@ async def async_init(self):
if not self.coordinators:
await self.init_coordinators()

if not self._unsub_purge:
self._unsub_purge = async_track_time_interval(self.hass, self.async_purge_entities, timedelta(hours=12))

async def async_unload(self):
for coo in self.coordinators:
await coo.async_shutdown()

self.spec = None
self.hass.data[DOMAIN].setdefault('miot_specs', {}).pop(self.model, None)

if self._unsub_purge:
self._unsub_purge()
self._unsub_purge = None

@cached_property
def did(self):
return self.info.did
Expand Down Expand Up @@ -960,6 +968,16 @@ async def offline_notify(self):
)
self.data['offline_times'] = offline_times

async def async_purge_entities(self, _now):
if not self.spec:
return
glob = self.spec.generate_entity_id_by_mac(self.info.unique_id, 'info', 'button')
await self.hass.services.async_call('recorder', 'purge_entities', {
'keep_days': 1,
'entity_globs': [glob],
})
self.log.info('Purge entities: %s', glob)

async def async_get_properties(self, mapping, update_entity=True, throw=False, **kwargs):
if not self.spec:
return {'error': 'No spec'}
Expand Down
5 changes: 4 additions & 1 deletion custom_components/xiaomi_miot/core/miot_spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,10 +254,13 @@ def get_properties(self, *args):
return lst

def generate_entity_id(self, entity, suffix=None, domain=None):
return self.generate_entity_id_by_mac(entity.unique_mac, suffix, domain)

def generate_entity_id_by_mac(self, mac, suffix=None, domain=None):
mod = f'{self.type}::::'.split(':')[5]
if not mod:
return None
mac = re.sub(r'[\W_]+', '', entity.unique_mac)
mac = re.sub(r'[\W_]+', '', mac)
eid = f'{mod}_{mac[-4:]}'
if suffix:
eid = f'{eid}_{suffix}'
Expand Down

0 comments on commit 3627da6

Please sign in to comment.