From 4f42e6f889426493c6f085842fa96d78ca8544f5 Mon Sep 17 00:00:00 2001 From: Tom Matheussen Date: Tue, 14 Jun 2022 23:08:00 +0200 Subject: [PATCH] Use comparitor state if we can't restore state (#30) --- custom_components/battery_sim/sensor.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/custom_components/battery_sim/sensor.py b/custom_components/battery_sim/sensor.py index 68bf391..8402ecc 100644 --- a/custom_components/battery_sim/sensor.py +++ b/custom_components/battery_sim/sensor.py @@ -141,11 +141,7 @@ def __init__( self._device_name = device_name self._type_of_sensor = type_of_sensor self._last_reset = dt_util.utcnow() - if comparitor_sensor is not None: - self._comparitor_sensor = comparitor_sensor - self._state = float(self._comparitor_sensor.state) - else: - self._state = 0.0 + self._comparitor_sensor = comparitor_sensor async def async_added_to_hass(self): """Handle entity which will be added.""" @@ -154,6 +150,10 @@ async def async_added_to_hass(self): state = await self.async_get_last_state() if state: self._state = float(state.state) + elif self._comparitor_sensor is not None and self._comparitor_sensor.state not in [STATE_UNAVAILABLE, STATE_UNKNOWN]: + self._state = float(self._comparitor_sensor.state) + else: + self._state = 0.0 @callback def update_value(self, value):