Skip to content

Commit

Permalink
Fix referencing GUI entities from YAML include works correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
bramstroker committed Jul 16, 2023
1 parent 371b393 commit f126d65
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 17 deletions.
32 changes: 23 additions & 9 deletions custom_components/powercalc/group_include/include.py
Expand Up @@ -4,7 +4,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.const import ATTR_ENTITY_ID, CONF_DOMAIN, CONF_ENTITY_ID
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers import area_registry, device_registry, entity_registry
from homeassistant.helpers.entity import Entity
Expand All @@ -19,6 +19,8 @@
CONF_TEMPLATE,
DATA_CONFIGURED_ENTITIES,
DOMAIN,
ENTRY_DATA_ENERGY_ENTITY,
ENTRY_DATA_POWER_ENTITY,
)
from custom_components.powercalc.errors import SensorConfigurationError
from custom_components.powercalc.sensors.energy import RealEnergySensor
Expand All @@ -37,14 +39,9 @@ def resolve_include_entities(hass: HomeAssistant, include_config: dict) -> list[
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]:
resolved_entities.extend(
hass.data[DOMAIN][DATA_CONFIGURED_ENTITIES][source_entity.entity_id],
)
continue
resolved_entities.extend(find_powercalc_entities_by_source_entity(hass, source_entity.entity_id))

# When we are dealing with a non powercalc sensor and it's an power or energy sensor,
# When we are dealing with a non powercalc sensor, and it's a 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:
Expand All @@ -56,11 +53,28 @@ def resolve_include_entities(hass: HomeAssistant, include_config: dict) -> list[
),
)
elif source_entity.device_class == SensorDeviceClass.ENERGY:
resolved_entities.append(RealEnergySensor(source_entity))
resolved_entities.append(RealEnergySensor(source_entity.entity_id))

return resolved_entities


def find_powercalc_entities_by_source_entity(hass: HomeAssistant, source_entity_id: str) -> list[Entity]:
# Check if we have powercalc sensors setup with YAML
if source_entity_id in hass.data[DOMAIN][DATA_CONFIGURED_ENTITIES]:
return hass.data[DOMAIN][DATA_CONFIGURED_ENTITIES][source_entity_id]

# Check if we have powercalc sensors setup with GUI
entities = []
for entry in hass.config_entries.async_entries(DOMAIN):
if entry.data.get(CONF_ENTITY_ID) != source_entity_id:
continue
if entry.data.get(ENTRY_DATA_POWER_ENTITY):
entities.append(RealPowerSensor(entry.data.get(ENTRY_DATA_POWER_ENTITY)))
if entry.data.get(ENTRY_DATA_ENERGY_ENTITY):
entities.append(RealEnergySensor(entry.data.get(ENTRY_DATA_ENERGY_ENTITY)))
return entities


@callback
def resolve_include_source_entities(
hass: HomeAssistant,
Expand Down
24 changes: 17 additions & 7 deletions custom_components/powercalc/sensors/energy.py
Expand Up @@ -66,7 +66,11 @@ async def create_energy_sensor(
f"No energy sensor with id {energy_sensor_id} found in your HA instance. "
"Double check `energy_sensor_id` setting",
)
return RealEnergySensor(entity_entry)
return RealEnergySensor(
entity_entry.entity_id,
entity_entry.name or entity_entry.original_name,
entity_entry.unique_id,
)

# User specified an existing power sensor with "power_sensor_id" option. Try to find a corresponding energy sensor
if CONF_POWER_SENSOR_ID in sensor_config and isinstance(
Expand Down Expand Up @@ -176,7 +180,12 @@ def find_related_real_energy_sensor(
if not energy_sensors:
return None

return RealEnergySensor(energy_sensors[0])
entity_entry = energy_sensors[0]
return RealEnergySensor(
entity_entry.entity_id,
entity_entry.name or entity_entry.original_name,
entity_entry.unique_id,
)


class EnergySensor(BaseEntity):
Expand Down Expand Up @@ -254,16 +263,17 @@ async def async_calibrate(self, value: str) -> None:
class RealEnergySensor(EnergySensor):
"""Contains a reference to an existing energy sensor entity."""

def __init__(self, entity_entry: er.RegistryEntry) -> None:
self._entity_entry = entity_entry
self.entity_id = self._entity_entry.entity_id
def __init__(self, entity_id: str, name: str | None = None, unique_id: str | None = None) -> None:
self.entity_id = entity_id
self._name = name
self._unique_id = unique_id

@property
def name(self) -> str | None:
"""Return the name of the sensor."""
return self._entity_entry.name or self._entity_entry.original_name
return self._name

@property
def unique_id(self) -> str | None:
"""Return the unique_id of the sensor."""
return self._entity_entry.unique_id
return self._unique_id
6 changes: 5 additions & 1 deletion tests/group_include/test_include.py
Expand Up @@ -404,7 +404,7 @@ async def test_include_yaml_configured_entity(


async def test_include_non_powercalc_entities_in_group(
hass: HomeAssistant, area_reg: AreaRegistry
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")
Expand Down Expand Up @@ -463,6 +463,10 @@ async def test_include_non_powercalc_entities_in_group(
}


#async def test_include_powercalc_gui_entry_in_yaml_include(hass: HomeAssistant):



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

0 comments on commit f126d65

Please sign in to comment.