Skip to content

Commit

Permalink
fix: tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bramstroker committed Nov 23, 2024
1 parent 3d9a03c commit 852f0dc
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 3 deletions.
4 changes: 4 additions & 0 deletions custom_components/powercalc/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import copy
import logging
import uuid
from abc import ABC, abstractmethod
from datetime import timedelta
from enum import StrEnum
Expand Down Expand Up @@ -1151,6 +1152,7 @@ async def async_step_integration_discovery(

self.skip_advanced_step = True # We don't want to ask advanced options when discovered

await self.async_set_unique_id(str(uuid.uuid4()))
self.selected_sensor_type = SensorType.VIRTUAL_POWER
self.source_entity = discovery_info[DISCOVERY_SOURCE_ENTITY]
del discovery_info[DISCOVERY_SOURCE_ENTITY]
Expand Down Expand Up @@ -1200,6 +1202,8 @@ async def async_step_user(
if not global_config_entry:
menu = {Steps.GLOBAL_CONFIGURATION: "Global configuration", **menu}

await self.async_set_unique_id(str(uuid.uuid4()))

return self.async_show_menu(step_id=Steps.USER, menu_options=menu)

async def async_step_menu_library(
Expand Down
3 changes: 2 additions & 1 deletion tests/config_flow/test_group_subtract.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ async def test_subtract_group_flow(hass: HomeAssistant) -> None:
user_input,
)
assert result["type"] == data_entry_flow.FlowResultType.CREATE_ENTRY
assert result["unique_id"] == "pc_subtract_sensor.outlet_power"
config_entry: ConfigEntry = result["result"]
assert config_entry.unique_id == "pc_subtract_sensor.outlet_power"
assert result["data"] == {
CONF_SENSOR_TYPE: SensorType.GROUP,
CONF_GROUP_TYPE: GroupType.SUBTRACT,
Expand Down
5 changes: 4 additions & 1 deletion tests/config_flow/test_real_power.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from homeassistant import data_entry_flow
from homeassistant.components.utility_meter.const import DAILY, WEEKLY
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_DEVICE, CONF_ENTITY_ID, CONF_NAME, CONF_SENSOR_TYPE
from homeassistant.core import HomeAssistant
from homeassistant.helpers.device_registry import DeviceEntry
Expand Down Expand Up @@ -85,11 +86,13 @@ async def test_energy_sensor_is_bound_to_power_device(hass: HomeAssistant) -> No
)
assert result["type"] == data_entry_flow.FlowResultType.CREATE_ENTRY

config_entry: ConfigEntry = result["result"]

await hass.async_block_till_done()

energy_sensor_entry = entity_registry.async_get("sensor.test_energy")
assert energy_sensor_entry
assert energy_sensor_entry.unique_id == "123_energy"
assert energy_sensor_entry.unique_id == f"{config_entry.unique_id}_energy"
assert energy_sensor_entry.device_id == device_id


Expand Down
6 changes: 5 additions & 1 deletion tests/config_flow/test_virtual_power_fixed.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,11 @@ async def test_sensor_is_created_without_providing_source_entity(hass: HomeAssis
assert hass.states.get("sensor.my_nice_sensor_energy")


async def test_with_unique_id(hass: HomeAssistant) -> None:
async def test_setup_twice_for_same_entity(hass: HomeAssistant) -> None:
"""
Test that we can set up two virtual power sensors for the same entity.
See: https://github.com/bramstroker/homeassistant-powercalc/issues/2684
"""
result = await goto_virtual_power_strategy_step(
hass,
CalculationStrategy.FIXED,
Expand Down

0 comments on commit 852f0dc

Please sign in to comment.