From 79e3f3334558beb17f7a5a0badb181f7d993a255 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=92=D0=B8=D0=BA=D1=82=D0=BE=D1=80=D0=BE=D0=B2=20=D0=90?= =?UTF-8?q?=D0=BD=D0=B4=D1=80=D0=B5=D0=B9=20=D0=93=D0=B5=D1=80=D0=BC=D0=B0?= =?UTF-8?q?=D0=BD=D0=BE=D0=B2=D0=B8=D1=87?= Date: Fri, 29 Oct 2021 12:15:02 +0300 Subject: [PATCH] add sensor filters --- custom_components/mega/entities.py | 17 +++++++++++------ custom_components/mega/hub.py | 2 +- custom_components/mega/sensor.py | 22 +++++++++++++++------- 3 files changed, 27 insertions(+), 14 deletions(-) diff --git a/custom_components/mega/entities.py b/custom_components/mega/entities.py index 2375c3e..e7eaff8 100644 --- a/custom_components/mega/entities.py +++ b/custom_components/mega/entities.py @@ -54,7 +54,6 @@ class BaseMegaEntity(CoordinatorEntity, RestoreEntity): smooth=None, **kwargs, ): - super().__init__(mega.updater) self._smooth = smooth self.http_cmd = http_cmd self._state: State = None @@ -85,6 +84,8 @@ class BaseMegaEntity(CoordinatorEntity, RestoreEntity): self._can_smooth_hard = None if self.http_cmd == 'ds2413': self.mega.ds2413_ports |= {self.port} + super().__init__(coordinator=mega.updater) + @property def is_ws(self): @@ -126,10 +127,14 @@ class BaseMegaEntity(CoordinatorEntity, RestoreEntity): def customize(self): if self._customize is not None: return self._customize - if self.hass is None: + if self.hass is None or self.entity_id is None: return {} if self._customize is None: - c_entity_id = self.hass.data.get(DOMAIN, {}).get('entities', {}).get(self.entity_id, {}) + + c_entity_id = self.hass.data.get(DOMAIN, {}).get(CONF_CUSTOM).get('entities', {}).get(self.entity_id, {}) + self.lg.debug( + 'customize %s with %s', self.entity_id, c_entity_id + ) c = self.hass.data.get(DOMAIN, {}).get(CONF_CUSTOM) or {} c = c.get(self._mega_id) or {} c = c.get(self.port) or {} @@ -168,9 +173,9 @@ class BaseMegaEntity(CoordinatorEntity, RestoreEntity): @property def lg(self) -> logging.Logger: - if self._lg is None: - self._lg = self.mega.lg.getChild(self._name or self.unique_id) - return self._lg + # if self._lg is None: + # self._lg = self.mega.lg.getChild(self._name or self.unique_id) + return _LOGGER @property def available(self) -> bool: diff --git a/custom_components/mega/hub.py b/custom_components/mega/hub.py index 69b5fe3..9f0fc2e 100644 --- a/custom_components/mega/hub.py +++ b/custom_components/mega/hub.py @@ -427,7 +427,7 @@ class MegaD: def subscribe(self, port, callback): port = int_ignore(port) self.lg.debug( - f'subscribe %s %s', port, callback + f'subscribe %s %s', port, str(callback) ) self.http.callbacks[self.id][port].append(callback) diff --git a/custom_components/mega/sensor.py b/custom_components/mega/sensor.py index de8d204..198bbf5 100644 --- a/custom_components/mega/sensor.py +++ b/custom_components/mega/sensor.py @@ -94,7 +94,7 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry, asyn hub.skip_ports |= {port} continue for data in cfg: - hub.lg.debug(f'add sensor on port %s with data %s', port, data) + hub.lg.debug(f'add sensor on port %s with data %s, constructor: %s', port, data, _constructors[tp]) sensor = _constructors[tp]( mega=hub, port=port, @@ -111,15 +111,17 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry, asyn class FilterBadValues(MegaPushEntity): def __init__(self, *args, **kwargs): - super().__init__(*args, **kwargs) self._prev_value = None + super().__init__(*args, **kwargs) def filter_value(self, value): self.lg.debug( 'value=%s filter_low=%s filter_high=%s filter_scale=%s prev_value=%s filter_values=%s', - (value, self.filter_low, self.filter_high, self.filter_scale, self._prev_value, self.filter_values) + value, self.filter_low, self.filter_high, self.filter_scale, self._prev_value, self.filter_values ) - if value in self.filter_values \ + self.lg.debug(f'{self.customize} {self.entity_id}') + if value \ + in self.filter_values \ or (self.filter_low is not None and value < self.filter_low) \ or (self.filter_high is not None and value > self.filter_high) \ or ( @@ -227,8 +229,9 @@ class Mega1WSensor(FilterBadValues): :param patt: pattern to extract value, must have at least one group that will contain parsed value """ super().__init__(*args, **kwargs) - self._value = None self.key = key + lg.debug(f'my key: {key}') + self._value = None self._device_class = device_class self._unit_of_measurement = unit_of_measurement self.mega.sensors.append(self) @@ -251,7 +254,7 @@ class Mega1WSensor(FilterBadValues): if self.key: return super().unique_id + f'_{self.key}' else: - return super(Mega1WSensor, self).unique_id + return super().unique_id @property def device_class(self): @@ -268,6 +271,8 @@ class Mega1WSensor(FilterBadValues): @property def state(self): ret = None + if not hasattr(self, 'key'): + return None if self.key: try: ret = self.mega.values.get(self.port, {}) @@ -310,7 +315,10 @@ class Mega1WSensor(FilterBadValues): n = super().name c = self.customize.get(CONF_NAME, {}) if isinstance(c, dict): - c = c.get(self.key) + try: + c = c.get(self.key) + except AttributeError: + lg.debug(dir(self)) return c or n