Skip to content

Commit

Permalink
Merge pull request #1686 from bramstroker/fix/daily-fixed-utility-meter
Browse files Browse the repository at this point in the history
fix issue where daily fixed utility meters were not created anymore since 1.6.2
  • Loading branch information
bramstroker committed May 30, 2023
2 parents 2e8de7b + 88eacc2 commit f22deaa
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 28 deletions.
19 changes: 14 additions & 5 deletions custom_components/powercalc/sensor.py
Expand Up @@ -120,7 +120,8 @@
from .sensors.abstract import BaseEntity
from .sensors.daily_energy import (
DAILY_FIXED_ENERGY_SCHEMA,
create_daily_fixed_energy_sensors,
create_daily_fixed_energy_power_sensor,
create_daily_fixed_energy_sensor,
)
from .sensors.energy import EnergySensor, create_energy_sensor
from .sensors.group import (
Expand Down Expand Up @@ -595,13 +596,21 @@ async def create_individual_sensors(
entities_to_add: list[Entity] = []
energy_sensor: EnergySensor | None = None
if CONF_DAILY_FIXED_ENERGY in sensor_config:
entities_to_add.extend(
await create_daily_fixed_energy_sensors(
energy_sensor = await create_daily_fixed_energy_sensor(
hass,
sensor_config,
source_entity,
)
entities_to_add.append(energy_sensor)

if source_entity:
daily_fixed_power_sensor = await create_daily_fixed_energy_power_sensor(
hass,
sensor_config,
source_entity,
),
)
)
if daily_fixed_power_sensor:
entities_to_add.append(daily_fixed_power_sensor)
else:
try:
power_sensor = await create_power_sensor(
Expand Down
23 changes: 0 additions & 23 deletions custom_components/powercalc/sensors/daily_energy.py
Expand Up @@ -72,29 +72,6 @@
_LOGGER = logging.getLogger(__name__)


async def create_daily_fixed_energy_sensors(
hass: HomeAssistant,
sensor_config: ConfigType,
source_entity: SourceEntity | None = None,
) -> list[SensorEntity]:
entities: list[SensorEntity] = []
energy_sensor = await create_daily_fixed_energy_sensor(
hass,
sensor_config,
source_entity,
)
entities.append(energy_sensor)
if source_entity:
daily_fixed_sensor = await create_daily_fixed_energy_power_sensor(
hass,
sensor_config,
source_entity,
)
if daily_fixed_sensor:
entities.append(daily_fixed_sensor)
return entities


async def create_daily_fixed_energy_sensor(
hass: HomeAssistant,
sensor_config: ConfigType,
Expand Down
17 changes: 17 additions & 0 deletions tests/sensors/test_daily_energy.py
Expand Up @@ -25,6 +25,7 @@
)

from custom_components.powercalc.const import (
CONF_CREATE_UTILITY_METERS,
CONF_DAILY_FIXED_ENERGY,
CONF_ENERGY_SENSOR_NAMING,
CONF_ENERGY_SENSOR_UNIT_PREFIX,
Expand Down Expand Up @@ -128,6 +129,22 @@ async def test_daily_energy_sensor_from_kwh_value(hass: HomeAssistant) -> None:
assert_entity_state(hass, sensor_entity_id, "12.0000")


async def test_utility_meters_are_created(hass: HomeAssistant) -> None:
await run_powercalc_setup(
hass,
{
CONF_PLATFORM: DOMAIN,
CONF_NAME: "IP camera upstairs",
CONF_DAILY_FIXED_ENERGY: {
CONF_VALUE: 12,
},
CONF_CREATE_UTILITY_METERS: True,
},
)

assert hass.states.get("sensor.ip_camera_upstairs_energy_daily")


async def test_daily_energy_sensor_also_creates_power_sensor(
hass: HomeAssistant,
) -> None:
Expand Down

0 comments on commit f22deaa

Please sign in to comment.