add sensor filters

This commit is contained in:
Викторов Андрей Германович
2021-10-29 12:15:02 +03:00
parent 74235a39ad
commit 79e3f33345
3 changed files with 27 additions and 14 deletions

View File

@@ -54,7 +54,6 @@ class BaseMegaEntity(CoordinatorEntity, RestoreEntity):
smooth=None, smooth=None,
**kwargs, **kwargs,
): ):
super().__init__(mega.updater)
self._smooth = smooth self._smooth = smooth
self.http_cmd = http_cmd self.http_cmd = http_cmd
self._state: State = None self._state: State = None
@@ -85,6 +84,8 @@ class BaseMegaEntity(CoordinatorEntity, RestoreEntity):
self._can_smooth_hard = None self._can_smooth_hard = None
if self.http_cmd == 'ds2413': if self.http_cmd == 'ds2413':
self.mega.ds2413_ports |= {self.port} self.mega.ds2413_ports |= {self.port}
super().__init__(coordinator=mega.updater)
@property @property
def is_ws(self): def is_ws(self):
@@ -126,10 +127,14 @@ class BaseMegaEntity(CoordinatorEntity, RestoreEntity):
def customize(self): def customize(self):
if self._customize is not None: if self._customize is not None:
return self._customize return self._customize
if self.hass is None: if self.hass is None or self.entity_id is None:
return {} return {}
if self._customize is None: 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 = self.hass.data.get(DOMAIN, {}).get(CONF_CUSTOM) or {}
c = c.get(self._mega_id) or {} c = c.get(self._mega_id) or {}
c = c.get(self.port) or {} c = c.get(self.port) or {}
@@ -168,9 +173,9 @@ class BaseMegaEntity(CoordinatorEntity, RestoreEntity):
@property @property
def lg(self) -> logging.Logger: def lg(self) -> logging.Logger:
if self._lg is None: # if self._lg is None:
self._lg = self.mega.lg.getChild(self._name or self.unique_id) # self._lg = self.mega.lg.getChild(self._name or self.unique_id)
return self._lg return _LOGGER
@property @property
def available(self) -> bool: def available(self) -> bool:

View File

@@ -427,7 +427,7 @@ class MegaD:
def subscribe(self, port, callback): def subscribe(self, port, callback):
port = int_ignore(port) port = int_ignore(port)
self.lg.debug( self.lg.debug(
f'subscribe %s %s', port, callback f'subscribe %s %s', port, str(callback)
) )
self.http.callbacks[self.id][port].append(callback) self.http.callbacks[self.id][port].append(callback)

View File

@@ -94,7 +94,7 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry, asyn
hub.skip_ports |= {port} hub.skip_ports |= {port}
continue continue
for data in cfg: 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]( sensor = _constructors[tp](
mega=hub, mega=hub,
port=port, port=port,
@@ -111,15 +111,17 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry, asyn
class FilterBadValues(MegaPushEntity): class FilterBadValues(MegaPushEntity):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self._prev_value = None self._prev_value = None
super().__init__(*args, **kwargs)
def filter_value(self, value): def filter_value(self, value):
self.lg.debug( self.lg.debug(
'value=%s filter_low=%s filter_high=%s filter_scale=%s prev_value=%s filter_values=%s', '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_low is not None and value < self.filter_low) \
or (self.filter_high is not None and value > self.filter_high) \ or (self.filter_high is not None and value > self.filter_high) \
or ( 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 :param patt: pattern to extract value, must have at least one group that will contain parsed value
""" """
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
self._value = None
self.key = key self.key = key
lg.debug(f'my key: {key}')
self._value = None
self._device_class = device_class self._device_class = device_class
self._unit_of_measurement = unit_of_measurement self._unit_of_measurement = unit_of_measurement
self.mega.sensors.append(self) self.mega.sensors.append(self)
@@ -251,7 +254,7 @@ class Mega1WSensor(FilterBadValues):
if self.key: if self.key:
return super().unique_id + f'_{self.key}' return super().unique_id + f'_{self.key}'
else: else:
return super(Mega1WSensor, self).unique_id return super().unique_id
@property @property
def device_class(self): def device_class(self):
@@ -268,6 +271,8 @@ class Mega1WSensor(FilterBadValues):
@property @property
def state(self): def state(self):
ret = None ret = None
if not hasattr(self, 'key'):
return None
if self.key: if self.key:
try: try:
ret = self.mega.values.get(self.port, {}) ret = self.mega.values.get(self.port, {})
@@ -310,7 +315,10 @@ class Mega1WSensor(FilterBadValues):
n = super().name n = super().name
c = self.customize.get(CONF_NAME, {}) c = self.customize.get(CONF_NAME, {})
if isinstance(c, dict): 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 return c or n