Skip to content

Commit

Permalink
Cleanup old logic from group creation code (#1729)
Browse files Browse the repository at this point in the history
  • Loading branch information
bramstroker committed Jun 25, 2023
1 parent f12d35e commit 1293cba
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 31 deletions.
36 changes: 5 additions & 31 deletions custom_components/powercalc/sensors/group.py
Expand Up @@ -7,14 +7,13 @@
from decimal import Decimal, DecimalException
from typing import Any

from homeassistant.components.sensor import DOMAIN as SENSOR_DOMAIN
from homeassistant.components.sensor import (
ATTR_STATE_CLASS,
RestoreSensor,
SensorDeviceClass,
SensorEntity,
SensorStateClass,
)
from homeassistant.components.sensor import DOMAIN as SENSOR_DOMAIN
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import (
ATTR_ENTITY_ID,
Expand Down Expand Up @@ -327,35 +326,10 @@ def resolve_entity_ids_recursively(
if device_class == SensorDeviceClass.POWER
else ENTRY_DATA_ENERGY_ENTITY
)
if key in member_entry.data:
resolved_ids.extend([member_entry.data.get(key)])
else:
# Below is the old logic for entity resolving.
# May be removed in the future when all config entries of users have been migrated
# In the new situation we save the power and energy entity id's on the config entry
# So we don't have to use a hacky way to get the entities from the entity registry anymore.
if device_class == SensorDeviceClass.POWER:
_LOGGER.warning("Using legacy resolve_entity_ids_recursively method")
entity_reg = er.async_get(hass)
state_class = (
SensorStateClass.MEASUREMENT
if device_class == SensorDeviceClass.POWER
else SensorStateClass.TOTAL_INCREASING
)
entities = [
entity_entry.entity_id
for entity_entry in entity_reg.entities.values()
if entity_entry.config_entry_id == member_entry_id
and entity_entry.capabilities
and entity_entry.capabilities.get(ATTR_STATE_CLASS) in state_class # type: ignore
]
if not entities:
_LOGGER.error(
f"No power or energy sensor found for config entry: {member_entry.title}, skipping these from the group",
)
continue
sorted_entities = sorted(entities)
resolved_ids.extend([sorted_entities[0]])
if key not in member_entry.data: # pragma: no cover
continue

resolved_ids.extend([member_entry.data.get(key)])

# Include the additional power/energy sensors the user specified
conf_key = (
Expand Down
23 changes: 23 additions & 0 deletions tests/sensors/test_group.py
Expand Up @@ -1161,6 +1161,29 @@ async def test_storage_version_1(hass: HomeAssistant) -> None:
assert store_state is None


async def test_unknown_member_config_entry_is_skipped_from_group(hass: HomeAssistant) -> None:
member_entry = await setup_config_entry(
hass,
{
CONF_SENSOR_TYPE: SensorType.VIRTUAL_POWER,
CONF_ENTITY_ID: "light.test",
CONF_MODE: CalculationStrategy.FIXED,
CONF_FIXED: {CONF_POWER: 50},
},
)

await setup_config_entry(
hass,
{
CONF_SENSOR_TYPE: SensorType.GROUP,
CONF_NAME: "group",
CONF_GROUP_MEMBER_SENSORS: [member_entry.entry_id, "foobar"],
},
)

assert hass.states.get("sensor.group_power").attributes.get("entities") == {"sensor.test_power"}


async def _create_energy_group(
hass: HomeAssistant,
name: str,
Expand Down

0 comments on commit 1293cba

Please sign in to comment.