Skip to content

Commit

Permalink
Merge cb000ab into d3b0bd8
Browse files Browse the repository at this point in the history
  • Loading branch information
bramstroker committed Apr 30, 2023
2 parents d3b0bd8 + cb000ab commit 5944dc8
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 21 deletions.
3 changes: 2 additions & 1 deletion custom_components/powercalc/sensor.py
Expand Up @@ -675,7 +675,8 @@ async def check_entity_not_already_configured(
if not is_discovered and source_entity.entity_id in discovered_entities:
entity_reg = er.async_get(hass)
for entity in discovered_entities.get(source_entity.entity_id) or []:
entity_reg.async_remove(entity.entity_id)
if entity_reg.async_get(entity.entity_id):
entity_reg.async_remove(entity.entity_id)
hass.states.async_remove(entity.entity_id)
discovered_entities[source_entity.entity_id] = []
return
Expand Down
15 changes: 9 additions & 6 deletions custom_components/powercalc/sensors/group.py
Expand Up @@ -3,11 +3,11 @@
import logging
from abc import abstractmethod
from datetime import timedelta
from decimal import Decimal
from decimal import Decimal, DecimalException
from typing import Any, Callable

import homeassistant.util.dt as dt_util
from homeassistant.components.sensor import ATTR_STATE_CLASS
from homeassistant.components.sensor import ATTR_STATE_CLASS, RestoreSensor
from homeassistant.components.sensor import DOMAIN as SENSOR_DOMAIN
from homeassistant.components.sensor import (
SensorDeviceClass,
Expand Down Expand Up @@ -40,7 +40,6 @@
async_track_time_interval,
)
from homeassistant.helpers.json import JSONEncoder
from homeassistant.helpers.restore_state import RestoreEntity
from homeassistant.helpers.singleton import singleton
from homeassistant.helpers.storage import Store
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
Expand Down Expand Up @@ -403,7 +402,7 @@ def create_grouped_energy_sensor(
)


class GroupedSensor(BaseEntity, RestoreEntity, SensorEntity):
class GroupedSensor(BaseEntity, RestoreSensor, SensorEntity):
"""Base class for grouped sensors"""

_attr_should_poll = False
Expand Down Expand Up @@ -438,8 +437,12 @@ async def async_added_to_hass(self) -> None:
"""Register state listeners."""
await super().async_added_to_hass()

if (state := await self.async_get_last_state()) is not None:
self._attr_native_value = state.state
if last_state := await self.async_get_last_sensor_data():
try:
if last_state.native_value:
self._attr_native_value = round(Decimal(last_state.native_value), self._rounding_digits)
except DecimalException as err:
_LOGGER.warning("Could not restore last state: %s", err)

self._prev_state_store = await PreviousStateStore.async_get_instance(self.hass)

Expand Down
4 changes: 2 additions & 2 deletions custom_components/powercalc/sensors/group_standby.py
Expand Up @@ -8,7 +8,7 @@
SensorEntity,
SensorStateClass,
)
from homeassistant.const import CONF_NAME, POWER_WATT, STATE_UNKNOWN
from homeassistant.const import CONF_NAME, POWER_WATT
from homeassistant.core import HomeAssistant
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity import Entity
Expand Down Expand Up @@ -82,5 +82,5 @@ async def _recalculate(self) -> None:
sum(self.standby_sensors.values()), self._rounding_digits
)
else:
self._attr_native_value = STATE_UNKNOWN
self._attr_native_value = None
self.async_schedule_update_ha_state(True)
2 changes: 1 addition & 1 deletion requirements.test.txt
@@ -1,7 +1,7 @@
black
flake8
freezegun
pytest-homeassistant-custom-component==0.13.23
pytest-homeassistant-custom-component==0.13.24
types-pytz
types-croniter
numpy
Expand Down
18 changes: 7 additions & 11 deletions tests/sensors/test_group.py
Expand Up @@ -17,7 +17,6 @@
POWER_WATT,
STATE_ON,
STATE_UNAVAILABLE,
STATE_UNKNOWN,
UnitOfEnergy,
UnitOfPower,
)
Expand All @@ -26,7 +25,7 @@
from homeassistant.helpers.entity_registry import EntityRegistry
from pytest_homeassistant_custom_component.common import (
MockConfigEntry,
mock_restore_cache,
mock_restore_cache_with_extra_data,
)

from custom_components.powercalc.const import (
Expand Down Expand Up @@ -265,14 +264,11 @@ async def test_reset_service(hass: HomeAssistant):
async def test_restore_state(hass: HomeAssistant):
await create_input_boolean(hass, "test1")

mock_restore_cache(
hass,
[
State(
"sensor.testgroup_energy",
"0.5000",
),
],
mock_restore_cache_with_extra_data(
hass, ((State(
"sensor.testgroup_energy",
"0.5000",
), {"native_unit_of_measurement": None, "native_value": 0.5}),)
)

await run_powercalc_setup(
Expand Down Expand Up @@ -333,7 +329,7 @@ async def test_group_unavailable_when_members_unavailable(hass: HomeAssistant):
assert power_state.state == STATE_UNAVAILABLE

energy_state = hass.states.get("sensor.testgroup_energy")
assert energy_state.state == STATE_UNKNOWN
assert energy_state.state == STATE_UNAVAILABLE

hass.states.async_set("input_boolean.test1", STATE_ON)
await hass.async_block_till_done()
Expand Down

0 comments on commit 5944dc8

Please sign in to comment.