Skip to content

Commit

Permalink
When using include also include non powercalc entities in group
Browse files Browse the repository at this point in the history
  • Loading branch information
bramstroker committed Jul 15, 2023
1 parent 63b59b8 commit 20dcff0
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 3 deletions.
18 changes: 15 additions & 3 deletions custom_components/powercalc/group_include/include.py
Expand Up @@ -3,6 +3,7 @@

from homeassistant.components.group import DOMAIN as GROUP_DOMAIN
from homeassistant.components.light import DOMAIN as LIGHT_DOMAIN
from homeassistant.components.sensor import SensorDeviceClass
from homeassistant.const import ATTR_ENTITY_ID, CONF_DOMAIN
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers import area_registry, device_registry, entity_registry
Expand All @@ -19,22 +20,33 @@
DOMAIN,
)
from custom_components.powercalc.errors import SensorConfigurationError
from custom_components.powercalc.sensors.energy import RealEnergySensor
from custom_components.powercalc.sensors.power import RealPowerSensor

from .filter import create_filter

_LOGGER = logging.getLogger(__name__)


def resolve_include_entities(hass: HomeAssistant, include_config: dict) -> list:
powercalc_entities = []
resolved_entities = []
source_entities = resolve_include_source_entities(hass, include_config)
_LOGGER.debug("Found include entities: %s", source_entities)
for source_entity in source_entities:
if source_entity.entity_id in hass.data[DOMAIN][DATA_CONFIGURED_ENTITIES]:
powercalc_entities.extend(
resolved_entities.extend(
hass.data[DOMAIN][DATA_CONFIGURED_ENTITIES][source_entity.entity_id],
)
return powercalc_entities
continue

if source_entity.domain is not DOMAIN and source_entity.device_class in [SensorDeviceClass.POWER, SensorDeviceClass.ENERGY]:
if source_entity.device_class == SensorDeviceClass.POWER:
entity = RealPowerSensor(source_entity.entity_id, source_entity.device_id, source_entity.unique_id)
else:
entity = RealEnergySensor(source_entity)
resolved_entities.append(entity)

return resolved_entities


@callback
Expand Down
52 changes: 52 additions & 0 deletions tests/group_include/test_include.py
Expand Up @@ -3,6 +3,7 @@

import pytest
from homeassistant.components import light
from homeassistant.components.sensor import SensorDeviceClass
from homeassistant.const import (
CONF_DOMAIN,
CONF_ENTITIES,
Expand Down Expand Up @@ -402,6 +403,57 @@ async def test_include_yaml_configured_entity(
}


async def test_include_non_powercalc_entities_in_group(hass: HomeAssistant, area_reg: AreaRegistry) -> None:
area = area_reg.async_get_or_create("bedroom")
await hass.async_block_till_done()

_create_powercalc_config_entry(hass, "light.test")

shelly_power_sensor = "sensor.shelly_power"
shelly_energy_sensor = "sensor.shelly_energy"
mock_registry(
hass,
{
shelly_power_sensor: RegistryEntry(
entity_id=shelly_power_sensor,
unique_id="1111",
platform="sensor",
device_class=SensorDeviceClass.POWER,
area_id=area.id,
),
shelly_energy_sensor: RegistryEntry(
entity_id=shelly_energy_sensor,
unique_id="2222",
platform="sensor",
device_class=SensorDeviceClass.ENERGY,
area_id=area.id,
),
"light.test": RegistryEntry(
entity_id="light.test",
unique_id="3333",
platform="light",
area_id=area.id,
),
},
)

await run_powercalc_setup(
hass,
{
CONF_CREATE_GROUP: "Test include",
CONF_INCLUDE: {
CONF_AREA: "bedroom",
},
},
)

power_state = hass.states.get("sensor.test_include_power")
assert power_state.attributes.get(ATTR_ENTITIES) == {"sensor.test_power", shelly_power_sensor}

energy_state = hass.states.get("sensor.test_include_energy")
assert energy_state.attributes.get(ATTR_ENTITIES) == {"sensor.test_energy", shelly_energy_sensor}


def _create_powercalc_config_entry(
hass: HomeAssistant,
source_entity_id: str,
Expand Down

0 comments on commit 20dcff0

Please sign in to comment.