186 files
This commit is contained in:
@@ -264,15 +264,28 @@ async def resolve_entity_ids_recursively(
|
||||
entry: ConfigEntry,
|
||||
device_class: SensorDeviceClass,
|
||||
resolved_ids: set[str] | None = None,
|
||||
seen_entry_ids: set[str] | None = None,
|
||||
) -> set[str]:
|
||||
"""Get all the entity IDs for the current group and all the subgroups."""
|
||||
"""Get all the entity IDs for the current group and all the subgroups.
|
||||
|
||||
Groups can reference each other as a subgroup, either in a cycle or as two groups sharing
|
||||
the same subgroup. `seen_entry_ids` makes sure every group is only visited once, so a cycle
|
||||
does not recurse until the stack runs out.
|
||||
"""
|
||||
if resolved_ids is None:
|
||||
resolved_ids = set()
|
||||
if seen_entry_ids is None:
|
||||
seen_entry_ids = set()
|
||||
|
||||
if entry.entry_id in seen_entry_ids:
|
||||
_LOGGER.debug("Group config entry %s already resolved, skipping", entry.entry_id)
|
||||
return resolved_ids
|
||||
seen_entry_ids.add(entry.entry_id)
|
||||
|
||||
_add_member_entry_ids(hass, entry, device_class, resolved_ids)
|
||||
_add_specified_sensors(entry, device_class, resolved_ids)
|
||||
await _add_include_based_sensors(hass, entry, device_class, resolved_ids)
|
||||
await _add_subgroup_entities(hass, entry, device_class, resolved_ids)
|
||||
await _add_subgroup_entities(hass, entry, device_class, resolved_ids, seen_entry_ids)
|
||||
|
||||
return resolved_ids
|
||||
|
||||
@@ -331,6 +344,7 @@ async def _add_subgroup_entities(
|
||||
entry: ConfigEntry,
|
||||
device_class: SensorDeviceClass,
|
||||
resolved_ids: set[str],
|
||||
seen_entry_ids: set[str],
|
||||
) -> None:
|
||||
"""Recursively add entities from subgroups."""
|
||||
subgroups = entry.data.get(CONF_SUB_GROUPS)
|
||||
@@ -343,7 +357,7 @@ async def _add_subgroup_entities(
|
||||
_LOGGER.error("Subgroup config entry not found: %s", subgroup_entry_id)
|
||||
continue
|
||||
|
||||
await resolve_entity_ids_recursively(hass, subgroup_entry, device_class, resolved_ids)
|
||||
await resolve_entity_ids_recursively(hass, subgroup_entry, device_class, resolved_ids, seen_entry_ids)
|
||||
|
||||
|
||||
@callback
|
||||
|
||||
@@ -1,12 +1,13 @@
|
||||
from decimal import Decimal
|
||||
import logging
|
||||
from typing import Any
|
||||
|
||||
from homeassistant.components.sensor import (
|
||||
SensorDeviceClass,
|
||||
SensorEntity,
|
||||
SensorStateClass,
|
||||
)
|
||||
from homeassistant.const import CONF_NAME, UnitOfPower
|
||||
from homeassistant.const import ATTR_UNIT_OF_MEASUREMENT, CONF_NAME, UnitOfPower
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
||||
from homeassistant.helpers.entity import Entity
|
||||
@@ -14,6 +15,8 @@ from homeassistant.helpers.typing import ConfigType
|
||||
|
||||
from custom_components.powercalc.common import create_source_entity
|
||||
from custom_components.powercalc.const import (
|
||||
ATTR_MEMBERS,
|
||||
ATTR_STATE,
|
||||
CONF_CREATE_ENERGY_SENSORS,
|
||||
CONF_POWER_SENSOR_PRECISION,
|
||||
DATA_STANDBY_POWER_SENSORS,
|
||||
@@ -77,6 +80,7 @@ class StandbyPowerSensor(PowerSensor, SensorEntity):
|
||||
self._recalculate,
|
||||
),
|
||||
)
|
||||
await self._recalculate()
|
||||
|
||||
async def _recalculate(self) -> None:
|
||||
"""Calculate sum of all power sensors in standby, and update the state of the sensor."""
|
||||
@@ -90,3 +94,18 @@ class StandbyPowerSensor(PowerSensor, SensorEntity):
|
||||
else:
|
||||
self._attr_native_value = None
|
||||
self.async_schedule_update_ha_state(True)
|
||||
|
||||
def debug_group(self) -> dict[str, Any]:
|
||||
"""Return the current standby contribution of each tracked power sensor."""
|
||||
members = {
|
||||
entity_id: {
|
||||
ATTR_STATE: str(round(power, self._rounding_digits)),
|
||||
ATTR_UNIT_OF_MEASUREMENT: self.native_unit_of_measurement,
|
||||
}
|
||||
for entity_id, power in sorted(self.standby_sensors.items())
|
||||
}
|
||||
return {
|
||||
ATTR_STATE: str(self.state),
|
||||
ATTR_UNIT_OF_MEASUREMENT: self.native_unit_of_measurement,
|
||||
ATTR_MEMBERS: members,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user