Skip to content

Commit

Permalink
fix: energy sensors were broken for some groups since v1.14 (#2446)
Browse files Browse the repository at this point in the history
  • Loading branch information
bramstroker authored Aug 12, 2024
1 parent 6a9d82a commit 326ef57
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
21 changes: 18 additions & 3 deletions custom_components/powercalc/sensors/group/custom.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,11 +173,14 @@ async def create_group_sensors_custom(
if CONF_NAME not in sensor_config:
sensor_config[CONF_NAME] = group_name

group_type: GroupType = GroupType(sensor_config.get(CONF_GROUP_TYPE, GroupType.CUSTOM))

power_sensor = None
if power_sensor_ids or force_create:
power_sensor = create_grouped_power_sensor(
hass,
group_name,
group_type,
sensor_config,
set(power_sensor_ids),
)
Expand All @@ -188,6 +191,7 @@ async def create_group_sensors_custom(
energy_sensor = create_grouped_energy_sensor(
hass,
group_name,
group_type,
sensor_config,
set(energy_sensor_ids),
power_sensor,
Expand Down Expand Up @@ -296,6 +300,7 @@ async def resolve_entity_ids_recursively(
def create_grouped_power_sensor(
hass: HomeAssistant,
group_name: str,
group_type: GroupType,
sensor_config: dict,
power_sensor_ids: set[str],
) -> GroupedPowerSensor:
Expand All @@ -319,6 +324,7 @@ def create_grouped_power_sensor(
unique_id=unique_id,
sensor_config=sensor_config,
rounding_digits=sensor_config.get(CONF_POWER_SENSOR_PRECISION) or DEFAULT_POWER_SENSOR_PRECISION,
group_type=group_type,
entity_id=entity_id,
device_id=sensor_config.get(CONF_DEVICE),
)
Expand All @@ -328,6 +334,7 @@ def create_grouped_power_sensor(
def create_grouped_energy_sensor(
hass: HomeAssistant,
group_name: str,
group_type: GroupType,
sensor_config: dict,
energy_sensor_ids: set[str],
power_sensor: GroupedPowerSensor | None,
Expand All @@ -346,8 +353,12 @@ def create_grouped_energy_sensor(

_LOGGER.debug("Creating grouped energy sensor: %s (entity_id=%s)", name, entity_id)

force_calculate_energy = bool(sensor_config.get(CONF_FORCE_CALCULATE_GROUP_ENERGY, False))
if power_sensor and force_calculate_energy:
should_create_riemann = bool(sensor_config.get(CONF_FORCE_CALCULATE_GROUP_ENERGY, False))
if not should_create_riemann and not energy_sensor_ids:
should_create_riemann = True
if group_type == GroupType.DOMAIN and sensor_config.get(CONF_DOMAIN) == "all":
should_create_riemann = False
if power_sensor and should_create_riemann:
return VirtualEnergySensor(
source_entity=power_sensor.entity_id,
entity_id=entity_id,
Expand All @@ -365,6 +376,7 @@ def create_grouped_energy_sensor(
unique_id=energy_unique_id,
sensor_config=sensor_config,
rounding_digits=sensor_config.get(CONF_ENERGY_SENSOR_PRECISION) or DEFAULT_ENERGY_SENSOR_PRECISION,
group_type=group_type,
entity_id=entity_id,
device_id=sensor_config.get(CONF_DEVICE),
)
Expand All @@ -388,6 +400,7 @@ def __init__(
entity_id: str,
sensor_config: dict[str, Any],
rounding_digits: int,
group_type: GroupType,
unique_id: str | None = None,
device_id: str | None = None,
) -> None:
Expand All @@ -404,7 +417,7 @@ def __init__(
self._prev_state_store: PreviousStateStore = PreviousStateStore(hass)
self._native_value_exact = Decimal(0)
self._states: dict[str, Decimal] = {}
self.group_type: GroupType = GroupType(sensor_config.get(CONF_GROUP_TYPE, GroupType.CUSTOM))
self.group_type = group_type

async def async_added_to_hass(self) -> None:
"""Register state listeners."""
Expand Down Expand Up @@ -600,6 +613,7 @@ def __init__(
entity_id: str,
sensor_config: dict[str, Any],
rounding_digits: int,
group_type: GroupType,
unique_id: str | None = None,
device_id: str | None = None,
) -> None:
Expand All @@ -610,6 +624,7 @@ def __init__(
entity_id,
sensor_config,
rounding_digits,
group_type,
unique_id,
device_id,
)
Expand Down
2 changes: 2 additions & 0 deletions custom_components/powercalc/sensors/group/subtract.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
CONF_CREATE_ENERGY_SENSORS,
CONF_POWER_SENSOR_PRECISION,
CONF_SUBTRACT_ENTITIES,
GroupType,
)
from custom_components.powercalc.errors import SensorConfigurationError
from custom_components.powercalc.sensors.abstract import generate_power_sensor_entity_id, generate_power_sensor_name
Expand Down Expand Up @@ -104,6 +105,7 @@ def __init__(
entity_id=entity_id,
sensor_config=sensor_config,
rounding_digits=int(sensor_config.get(CONF_POWER_SENSOR_PRECISION, 2)),
group_type=GroupType.SUBTRACT,
unique_id=unique_id,
device_id=None, # todo: add device_id
)
Expand Down

0 comments on commit 326ef57

Please sign in to comment.