Ignore utility_meter restore state if state is invalid (#57010)
Co-authored-by: Paulus Schoutsen <balloob@gmail.com>
This commit is contained in:
parent
255ffe801b
commit
79b10c43d8
1 changed files with 24 additions and 13 deletions
|
@ -1,5 +1,6 @@
|
||||||
"""Utility meter from sensors providing raw data."""
|
"""Utility meter from sensors providing raw data."""
|
||||||
from datetime import date, datetime, timedelta
|
from datetime import date, datetime, timedelta
|
||||||
|
import decimal
|
||||||
from decimal import Decimal, DecimalException
|
from decimal import Decimal, DecimalException
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
|
@ -323,8 +324,18 @@ class UtilityMeterSensor(RestoreEntity, SensorEntity):
|
||||||
|
|
||||||
state = await self.async_get_last_state()
|
state = await self.async_get_last_state()
|
||||||
if state:
|
if state:
|
||||||
|
try:
|
||||||
self._state = Decimal(state.state)
|
self._state = Decimal(state.state)
|
||||||
self._unit_of_measurement = state.attributes.get(ATTR_UNIT_OF_MEASUREMENT)
|
except decimal.InvalidOperation:
|
||||||
|
_LOGGER.error(
|
||||||
|
"Could not restore state <%s>. Resetting utility_meter.%s",
|
||||||
|
state.state,
|
||||||
|
self.name,
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
self._unit_of_measurement = state.attributes.get(
|
||||||
|
ATTR_UNIT_OF_MEASUREMENT
|
||||||
|
)
|
||||||
self._last_period = (
|
self._last_period = (
|
||||||
float(state.attributes.get(ATTR_LAST_PERIOD))
|
float(state.attributes.get(ATTR_LAST_PERIOD))
|
||||||
if state.attributes.get(ATTR_LAST_PERIOD)
|
if state.attributes.get(ATTR_LAST_PERIOD)
|
||||||
|
|
Loading…
Add table
Reference in a new issue