Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix utility meter restore state #66490

Merged
merged 12 commits into from
Feb 15, 2022
Prev Previous commit
always a Decimal
  • Loading branch information
dgomes committed Feb 15, 2022
commit 0f4675d1b72f425c6d0145be7c5f0514619256c9
10 changes: 5 additions & 5 deletions homeassistant/components/utility_meter/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ def __init__(
self._parent_meter = parent_meter
self._sensor_source_id = source_entity
self._state = None
self._last_period = 0
self._last_period = Decimal(0)
self._last_reset = dt_util.utcnow()
self._collecting = None
self._name = name
Expand Down Expand Up @@ -278,7 +278,7 @@ async def async_reset_meter(self, entity_id):
return
_LOGGER.debug("Reset utility meter <%s>", self.entity_id)
self._last_reset = dt_util.utcnow()
self._last_period = str(self._state) if self._state else 0
self._last_period = Decimal(self._state) if self._state else Decimal(0)
self._state = 0
self.async_write_ha_state()

Expand Down Expand Up @@ -315,10 +315,10 @@ async def async_added_to_hass(self):
ATTR_UNIT_OF_MEASUREMENT
)
self._last_period = (
float(state.attributes[ATTR_LAST_PERIOD])
Decimal(state.attributes[ATTR_LAST_PERIOD])
if state.attributes.get(ATTR_LAST_PERIOD)
and is_number(state.attributes[ATTR_LAST_PERIOD])
else 0
else Decimal(0)
)
self._last_reset = dt_util.as_utc(
dt_util.parse_datetime(state.attributes.get(ATTR_LAST_RESET))
Expand Down Expand Up @@ -396,7 +396,7 @@ def extra_state_attributes(self):
state_attr = {
ATTR_SOURCE_ID: self._sensor_source_id,
ATTR_STATUS: PAUSED if self._collecting is None else COLLECTING,
ATTR_LAST_PERIOD: self._last_period,
ATTR_LAST_PERIOD: str(self._last_period),
}
if self._period is not None:
state_attr[ATTR_PERIOD] = self._period
Expand Down
4 changes: 2 additions & 2 deletions tests/components/utility_meter/test_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -537,7 +537,7 @@ async def _test_self_reset(hass, config, start_time, expect_reset=True):
assert state.attributes.get("last_reset") == now.isoformat()
assert state.state == "3"
else:
assert state.attributes.get("last_period") == 0
assert state.attributes.get("last_period") == "0"
assert state.state == "5"
start_time_str = dt_util.parse_datetime(start_time).isoformat()
assert state.attributes.get("last_reset") == start_time_str
Expand Down Expand Up @@ -566,7 +566,7 @@ async def _test_self_reset(hass, config, start_time, expect_reset=True):
assert state.attributes.get("last_period") == "2"
assert state.state == "7"
else:
assert state.attributes.get("last_period") == 0
assert state.attributes.get("last_period") == "0"
assert state.state == "9"


Expand Down