Skip to content

Commit

Permalink
Merge pull request #1679 from bramstroker/fix/daily-fixed-energy-sensor
Browse files Browse the repository at this point in the history
fix issue where daily fixed energy sensor was not created correctly
  • Loading branch information
bramstroker committed May 27, 2023
2 parents ffa4c8d + 4d59bcd commit 03802ec
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 12 deletions.
7 changes: 2 additions & 5 deletions custom_components/powercalc/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -343,11 +343,8 @@ def save_entity_ids_on_config_entry(
e.entity_id for e in entities.all() if isinstance(e, VirtualPowerSensor)
]
new_data = config_entry.data.copy()
if not power_entities:
raise SensorConfigurationError(
f"No power sensor created for config_entry {config_entry.entry_id}",
)
new_data.update({ENTRY_DATA_POWER_ENTITY: power_entities[0]})
if power_entities:
new_data.update({ENTRY_DATA_POWER_ENTITY: power_entities[0]})

if CONF_CREATE_ENERGY_SENSOR not in config_entry.data or config_entry.data.get(
CONF_CREATE_ENERGY_SENSOR,
Expand Down
2 changes: 1 addition & 1 deletion custom_components/powercalc/sensors/group.py
Original file line number Diff line number Diff line change
Expand Up @@ -660,7 +660,7 @@ def calculate_new_state(
)
continue
prev_state = self._prev_state_store.get_entity_state(
self.entity_id, entity_state.entity_id
self.entity_id, entity_state.entity_id,
)
cur_state_value = self._get_state_value_in_native_unit(entity_state)

Expand Down
20 changes: 20 additions & 0 deletions tests/sensors/test_daily_energy.py
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,26 @@ async def test_name_and_entity_id_can_be_inherited_from_source_entity(
assert state


async def test_create_daily_energy_sensor_using_config_entry(hass: HomeAssistant) -> None:
config_entry_group = MockConfigEntry(
domain=DOMAIN,
data={
CONF_SENSOR_TYPE: SensorType.DAILY_ENERGY,
CONF_NAME: "Test",
CONF_DAILY_FIXED_ENERGY: {
CONF_UNIT_OF_MEASUREMENT: ENERGY_KILO_WATT_HOUR,
CONF_VALUE: 200,
CONF_UPDATE_FREQUENCY: 1800.0,
},
},
)
config_entry_group.add_to_hass(hass)
assert await hass.config_entries.async_setup(config_entry_group.entry_id)
await hass.async_block_till_done()

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


async def _trigger_periodic_update(
hass: HomeAssistant,
number_of_updates: int = 1,
Expand Down
12 changes: 6 additions & 6 deletions tests/sensors/test_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -1022,7 +1022,7 @@ async def test_ignore_unavailable_state(hass: HomeAssistant) -> None:

async def test_energy_sensor_delta_updates_new_sensor(hass: HomeAssistant) -> None:
await _create_energy_group(
hass, "TestGroup", ["sensor.a_energy", "sensor.b_energy"]
hass, "TestGroup", ["sensor.a_energy", "sensor.b_energy"],
)

hass.states.async_set("sensor.a_energy", "2.00")
Expand Down Expand Up @@ -1050,7 +1050,7 @@ async def test_energy_sensor_delta_updates_new_sensor(hass: HomeAssistant) -> No

async def test_energy_sensor_delta_updates_existing_sensor(hass: HomeAssistant) -> None:
await _create_energy_group(
hass, "TestGroup", ["sensor.a_energy", "sensor.b_energy"]
hass, "TestGroup", ["sensor.a_energy", "sensor.b_energy"],
)

hass.states.async_set("sensor.testgroup_energy", "5.00")
Expand All @@ -1076,13 +1076,13 @@ async def test_energy_sensor_in_multiple_groups_calculates_correctly(
Fixes https://github.com/bramstroker/homeassistant-powercalc/issues/1673
"""
await _create_energy_group(
hass, "TestGroupA", ["sensor.a_energy", "sensor.b_energy"]
hass, "TestGroupA", ["sensor.a_energy", "sensor.b_energy"],
)
await _create_energy_group(
hass, "TestGroupB", ["sensor.a_energy", "sensor.c_energy"]
hass, "TestGroupB", ["sensor.a_energy", "sensor.c_energy"],
)
await _create_energy_group(
hass, "TestGroupC", ["sensor.a_energy", "sensor.d_energy"]
hass, "TestGroupC", ["sensor.a_energy", "sensor.d_energy"],
)

hass.states.async_set("sensor.a_energy", "2.00")
Expand Down Expand Up @@ -1140,7 +1140,7 @@ async def test_storage_version_1(hass: HomeAssistant) -> None:


async def _create_energy_group(
hass: HomeAssistant, name: str, member_entities: list[str]
hass: HomeAssistant, name: str, member_entities: list[str],
) -> None:
"""Create a group energy sensor for testing purposes"""
config_entry_group = MockConfigEntry(
Expand Down

0 comments on commit 03802ec

Please sign in to comment.