Skip to content

Commit

Permalink
fix: don't create group energy sensors when explicitely disabled by user
Browse files Browse the repository at this point in the history
  • Loading branch information
bramstroker committed Mar 22, 2024
1 parent 636f425 commit c312081
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 49 deletions.
8 changes: 4 additions & 4 deletions custom_components/powercalc/sensor.py
Expand Up @@ -139,8 +139,8 @@
from .sensors.group import (
add_to_associated_group,
create_domain_group_sensor,
create_group_sensors,
create_group_sensors_from_config_entry,
create_group_sensors_gui,
create_group_sensors_yaml,
)
from .sensors.group_standby import create_general_standby_sensors
from .sensors.power import VirtualPowerSensor, create_power_sensor
Expand Down Expand Up @@ -332,7 +332,7 @@ async def async_setup_entry(
global_config,
sensor_config,
)
entities = await create_group_sensors_from_config_entry(
entities = await create_group_sensors_gui(
hass=hass,
entry=entry,
sensor_config=merged_sensor_config,
Expand Down Expand Up @@ -697,7 +697,7 @@ async def create_sensors( # noqa: C901
# Create group sensors (power, energy, utility)
if CONF_CREATE_GROUP in config:
entities_to_add.new.extend(
await create_group_sensors(
await create_group_sensors_yaml(
str(config.get(CONF_CREATE_GROUP)),
get_merged_sensor_configuration(global_config, config, validate=False),
entities_to_add.all(),
Expand Down
86 changes: 41 additions & 45 deletions custom_components/powercalc/sensors/group.py
Expand Up @@ -55,6 +55,7 @@
ATTR_ENTITIES,
ATTR_IS_GROUP,
CONF_AREA,
CONF_CREATE_ENERGY_SENSOR,
CONF_DISABLE_EXTENDED_ATTRIBUTES,
CONF_ENERGY_SENSOR_PRECISION,
CONF_ENERGY_SENSOR_UNIT_PREFIX,
Expand Down Expand Up @@ -99,7 +100,7 @@
STATE_DUMP_INTERVAL = timedelta(minutes=10)


async def create_group_sensors(
async def create_group_sensors_yaml(
group_name: str,
sensor_config: dict[str, Any],
entities: list[Entity],
Expand All @@ -126,72 +127,67 @@ def _get_filtered_entity_ids_by_class(
)
]

group_sensors: list[Entity] = []

power_sensor_ids = _get_filtered_entity_ids_by_class(entities, filters, PowerSensor)
power_sensor = create_grouped_power_sensor(
hass,
group_name,
sensor_config,
set(power_sensor_ids),
)
group_sensors.append(power_sensor)

energy_sensor_ids = _get_filtered_entity_ids_by_class(
entities,
filters,
EnergySensor,
)
energy_sensor = create_grouped_energy_sensor(
hass,
group_name,
sensor_config,
set(energy_sensor_ids),
)
group_sensors.append(energy_sensor)

group_sensors.extend(
await create_utility_meters(
hass,
energy_sensor,
sensor_config,
net_consumption=True,
),
)
create_energy_sensor: bool = sensor_config.get(CONF_CREATE_ENERGY_SENSOR, True)
energy_sensor_ids = []
if create_energy_sensor:
energy_sensor_ids = _get_filtered_entity_ids_by_class(
entities,
filters,
EnergySensor,
)

return group_sensors
return await create_group_sensors(hass, group_name, sensor_config, set(power_sensor_ids), set(energy_sensor_ids))


async def create_group_sensors_from_config_entry(
async def create_group_sensors_gui(
hass: HomeAssistant,
entry: ConfigEntry,
sensor_config: dict,
) -> list[SensorEntity]:
) -> list[Entity]:
"""Create group sensors based on a config_entry."""
group_sensors: list[SensorEntity] = []

group_name = str(entry.data.get(CONF_NAME))

if CONF_UNIQUE_ID not in sensor_config:
sensor_config[CONF_UNIQUE_ID] = entry.entry_id

power_sensor_ids = await resolve_entity_ids_recursively(hass, entry, SensorDeviceClass.POWER)

energy_sensor_ids = await resolve_entity_ids_recursively(hass, entry, SensorDeviceClass.ENERGY)

return await create_group_sensors(hass, group_name, sensor_config, power_sensor_ids, energy_sensor_ids)


async def create_group_sensors(
hass: HomeAssistant,
group_name: str,
sensor_config: dict[str, Any],
power_sensor_ids: set[str],
energy_sensor_ids: set[str],
) -> list[Entity]:
"""Create grouped power and energy sensors."""

group_sensors: list[Entity] = []

if power_sensor_ids:
power_sensor = create_grouped_power_sensor(
hass,
group_name,
sensor_config,
power_sensor_ids,
group_sensors.append(
create_grouped_power_sensor(
hass,
group_name,
sensor_config,
set(power_sensor_ids),
),
)
group_sensors.append(power_sensor)

energy_sensor_ids = await resolve_entity_ids_recursively(hass, entry, SensorDeviceClass.ENERGY)
if energy_sensor_ids:
create_energy_sensor: bool = sensor_config.get(CONF_CREATE_ENERGY_SENSOR, True)
if energy_sensor_ids and create_energy_sensor:
energy_sensor = create_grouped_energy_sensor(
hass,
group_name,
sensor_config,
energy_sensor_ids,
set(energy_sensor_ids),
)
group_sensors.append(energy_sensor)

Expand All @@ -217,7 +213,7 @@ async def create_domain_group_sensor(
sensor_config[
CONF_UNIQUE_ID
] = f"powercalc_domaingroup_{discovery_info[CONF_DOMAIN]}"
return await create_group_sensors(
return await create_group_sensors_yaml(
f"All {domain}",
sensor_config,
discovery_info[CONF_ENTITIES],
Expand Down
33 changes: 33 additions & 0 deletions tests/sensors/test_group.py
Expand Up @@ -37,6 +37,7 @@
from custom_components.powercalc.const import (
ATTR_ENTITIES,
ATTR_IS_GROUP,
CONF_CREATE_ENERGY_SENSOR,
CONF_CREATE_GROUP,
CONF_CREATE_UTILITY_METERS,
CONF_DISABLE_EXTENDED_ATTRIBUTES,
Expand Down Expand Up @@ -1371,6 +1372,38 @@ async def test_bind_to_configured_device(
assert group_entity.device_id == device_entry.id


async def test_disable_energy_sensor_creation(hass: HomeAssistant) -> None:
"""See https://github.com/bramstroker/homeassistant-powercalc/issues/2143"""
await run_powercalc_setup(
hass,
{
CONF_CREATE_GROUP: "TestGroup",
CONF_ENTITIES: [
get_simple_fixed_config("input_boolean.test1"),
get_simple_fixed_config("input_boolean.test2"),
],
CONF_CREATE_ENERGY_SENSOR: False,
},
)

assert hass.states.get("sensor.testgroup_energy") is None


async def test_disable_energy_sensor_creation_gui(hass: HomeAssistant) -> None:
"""See https://github.com/bramstroker/homeassistant-powercalc/issues/2143"""
await setup_config_entry(
hass,
{
CONF_SENSOR_TYPE: SensorType.GROUP,
CONF_NAME: "TestGroup",
CONF_CREATE_ENERGY_SENSOR: False,
CONF_GROUP_ENERGY_ENTITIES: ["sensor.a_energy", "sensor.b_energy"],
},
)

assert hass.states.get("sensor.testgroup_energy") is None


async def test_inital_group_sum_calculated(hass: HomeAssistant) -> None:
"""See https://github.com/bramstroker/homeassistant-powercalc/issues/1922"""
hass.states.async_set("sensor.my_power", STATE_UNAVAILABLE)
Expand Down

0 comments on commit c312081

Please sign in to comment.