Skip to content

Commit

Permalink
When using include also include non powercalc entities in group (#1752)
Browse files Browse the repository at this point in the history
* When using include also include non powercalc entities in group

* Add docs

* Add docs

* Fix mypy typing
  • Loading branch information
bramstroker authored Jul 16, 2023
1 parent 63b59b8 commit 9c37b40
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 4 deletions.
28 changes: 24 additions & 4 deletions custom_components/powercalc/group_include/include.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@

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
from homeassistant.helpers.entity import Entity
from homeassistant.helpers.entity_component import EntityComponent
from homeassistant.helpers.entity_platform import split_entity_id
from homeassistant.helpers.template import Template
Expand All @@ -19,22 +21,40 @@
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 = []
def resolve_include_entities(hass: HomeAssistant, include_config: dict) -> list[Entity]:
""""
For a given include configuration fetch all power and energy sensors from the HA instance
"""
resolved_entities: list[Entity] = []
source_entities = resolve_include_source_entities(hass, include_config)
_LOGGER.debug("Found include entities: %s", source_entities)
for source_entity in source_entities:
# Check if we have powercalc sensors for giving source entity
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

# When we are dealing with a non powercalc sensor and it's an power or energy sensor,
# we can include that in the group
if source_entity.domain is not DOMAIN:
if source_entity.device_class == SensorDeviceClass.POWER:
resolved_entities.append(
RealPowerSensor(source_entity.entity_id, source_entity.device_id, source_entity.unique_id),
)
elif source_entity.device_class == SensorDeviceClass.ENERGY:
resolved_entities.append(RealEnergySensor(source_entity))

return resolved_entities


@callback
Expand Down
53 changes: 53 additions & 0 deletions tests/group_include/test_include.py
Original file line number Diff line number Diff line change
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,58 @@ async def test_include_yaml_configured_entity(
}


async def test_include_non_powercalc_entities_in_group(hass: HomeAssistant, area_reg: AreaRegistry) -> None:
"""Test that both powercalc and non powercalc entities can be included"""
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 9c37b40

Please sign in to comment.