From 64f9cb064781c6ddc5089657d3710b31d10fe1ba Mon Sep 17 00:00:00 2001 From: Ernst Klamer Date: Thu, 13 Jan 2022 19:50:06 +0100 Subject: [PATCH] move restoring attributes to list --- custom_components/ble_monitor/sensor.py | 77 ++++++++++--------------- 1 file changed, 32 insertions(+), 45 deletions(-) diff --git a/custom_components/ble_monitor/sensor.py b/custom_components/ble_monitor/sensor.py index 011cdd5c3..baf2b60e8 100644 --- a/custom_components/ble_monitor/sensor.py +++ b/custom_components/ble_monitor/sensor.py @@ -50,6 +50,32 @@ _LOGGER = logging.getLogger(__name__) +RESTORE_ATTRIBUTES = [ + "median", + "mean", + "last median of", + "last mean of", + "rssi", + "firmware", + "last packet id", + "last button press", + "last remote button pressed", + "last type of press", + "dimmer value", + "constant", + "volume start", + "keg size", + "port name", + "port state", + "port index", + "impedance", + "acceleration x", + "acceleration y", + "acceleration z", + "light level", + ATTR_BATTERY_LEVEL, +] + async def async_setup_platform(hass, conf, add_entities, discovery_info=None): """Set up the sensor platform.""" @@ -354,50 +380,9 @@ async def async_added_to_hass(self): except AttributeError: pass self._state = old_state.state - if "median" in old_state.attributes: - self._extra_state_attributes["median"] = old_state.attributes["median"] - if "mean" in old_state.attributes: - self._extra_state_attributes["mean"] = old_state.attributes["mean"] - if "last median of" in old_state.attributes: - self._extra_state_attributes["last median of"] = old_state.attributes[ - "last median of" - ] - self._state = old_state.attributes["median"] - if "last mean of" in old_state.attributes: - self._extra_state_attributes["last mean of"] = old_state.attributes[ - "last mean of" - ] - self._state = old_state.attributes["mean"] - if "rssi" in old_state.attributes: - self._extra_state_attributes["rssi"] = old_state.attributes["rssi"] - if "firmware" in old_state.attributes: - self._extra_state_attributes["firmware"] = old_state.attributes["firmware"] - if "last packet id" in old_state.attributes: - self._extra_state_attributes["last packet id"] = old_state.attributes[ - "last packet id" - ] - if "last button press" in old_state.attributes: - self._extra_state_attributes["last button press"] = old_state.attributes[ - "last button press" - ] - if "last remote button pressed" in old_state.attributes: - self._extra_state_attributes[ - "last remote button pressed" - ] = old_state.attributes["last remote button pressed"] - if "last type of press" in old_state.attributes: - self._extra_state_attributes["last type of press"] = old_state.attributes[ - "last type of press" - ] - if "dimmer value" in old_state.attributes: - self._extra_state_attributes["dimmer value"] = old_state.attributes[ - "dimmer value" - ] - if "constant" in old_state.attributes: - self._extra_state_attributes["constant"] = old_state.attributes["constant"] - if ATTR_BATTERY_LEVEL in old_state.attributes: - self._extra_state_attributes[ATTR_BATTERY_LEVEL] = old_state.attributes[ - ATTR_BATTERY_LEVEL - ] + for attr in RESTORE_ATTRIBUTES: + if attr in old_state.attributes: + self._extra_state_attributes[attr] = old_state.attributes[attr] self.ready_for_update = True @property @@ -729,7 +714,7 @@ def collect(self, data, period_cnt, batt_attr=None): class WeightSensor(InstantUpdateSensor): - """Representation of a non-stabilized Weight sensor.""" + """Representation of a Weight sensor.""" def __init__(self, config, mac, devtype, firmware, description): """Initialize the sensor.""" @@ -749,6 +734,8 @@ def collect(self, data, period_cnt, batt_attr=None): self._extra_state_attributes["weight removed"] = bool( data["weight removed"] ) + if "impedance" in data: + self._extra_state_attributes["impedance"] = data["impedance"] if "weight unit" in data: self._attr_native_unit_of_measurement = data["weight unit"] else: