Skip to content

Commit

Permalink
Merge 2e02102 into 95b060b
Browse files Browse the repository at this point in the history
  • Loading branch information
bramstroker committed Mar 30, 2024
2 parents 95b060b + 2e02102 commit 2397b5e
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 1 deletion.
6 changes: 5 additions & 1 deletion custom_components/powercalc/sensor.py
Expand Up @@ -770,7 +770,11 @@ async def create_individual_sensors(
return EntitiesBucket()

# Create energy sensor which integrates the power sensor
if sensor_config.get(CONF_CREATE_ENERGY_SENSOR) or CONF_ENERGY_SENSOR_ID in sensor_config:
if (
sensor_config.get(CONF_CREATE_ENERGY_SENSOR) or
sensor_config.get(CONF_FORCE_ENERGY_SENSOR_CREATION) or
CONF_ENERGY_SENSOR_ID in sensor_config
):
energy_sensor = await create_energy_sensor(
hass,
sensor_config,
Expand Down
3 changes: 3 additions & 0 deletions docs/source/sensor-types/real-power-sensor.rst
Expand Up @@ -35,5 +35,8 @@ to use an existing power sensor and let powercalc create the energy sensors and
This also enables you to combine virtual power sensors (created with powercalc) and existing power sensors in your HA installation into
a YAML group. Without this configuration option power_sensor_id that would not be possible.

When using `force_energy_sensor_creation` you need to provide either a source entity (``entity_id``) or a ``name`` for the energy sensor.
If you don't provide this Powercalc has no way to determine how the energy sensor should be named.

.. note::
If you don't define `force_energy_sensor_creation` or you set it to `false` an energy sensor will not be created if the device already has an energy sensor. This can be a problem if you want to create an energy sensor for an MQTT device with multiple energy and power sensors already in it.
36 changes: 36 additions & 0 deletions tests/sensors/test_energy.py
Expand Up @@ -31,6 +31,7 @@
ATTR_ENTITIES,
ATTR_SOURCE_DOMAIN,
ATTR_SOURCE_ENTITY,
CONF_CREATE_ENERGY_SENSORS,
CONF_CREATE_GROUP,
CONF_DISABLE_EXTENDED_ATTRIBUTES,
CONF_ENERGY_SENSOR_ID,
Expand Down Expand Up @@ -191,6 +192,41 @@ async def test_force_create_energy_sensor_for_existing_power_sensor(
}


async def test_force_create_energy_sensor_overrides_create_energy_sensors_option(hass: HomeAssistant) -> None:
"""
When you use force_energy_sensor_creation, it should override create_energy_sensors option,
and create an energy sensor
"""
mock_registry(
hass,
{
"sensor.existing_power": RegistryEntry(
entity_id="sensor.bedroom_airco_power",
unique_id="1234",
platform="sensor",
device_class=SensorDeviceClass.POWER,
),
},
)

await run_powercalc_setup(
hass,
{
CONF_POWER_SENSOR_ID: "sensor.bedroom_airco_power",
CONF_NAME: "Bedroom airco",
CONF_FORCE_ENERGY_SENSOR_CREATION: True,
CONF_IGNORE_UNAVAILABLE_STATE: True,
},
{
CONF_CREATE_ENERGY_SENSORS: False,
},
)
await hass.async_block_till_done()

energy_state = hass.states.get("sensor.bedroom_airco_energy")
assert energy_state


async def test_disable_extended_attributes(hass: HomeAssistant) -> None:
await create_input_boolean(hass)

Expand Down

0 comments on commit 2397b5e

Please sign in to comment.