Skip to content

Commit

Permalink
move restoring attributes to list
Browse files Browse the repository at this point in the history
  • Loading branch information
Ernst79 committed Jan 13, 2022
1 parent b027488 commit 64f9cb0
Showing 1 changed file with 32 additions and 45 deletions.
77 changes: 32 additions & 45 deletions custom_components/ble_monitor/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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."""
Expand All @@ -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:
Expand Down

0 comments on commit 64f9cb0

Please sign in to comment.