Skip to content

Commit

Permalink
Add options flow
Browse files Browse the repository at this point in the history
  • Loading branch information
bramstroker committed Aug 6, 2023
1 parent c9fa807 commit a7abc90
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 7 deletions.
13 changes: 9 additions & 4 deletions custom_components/powercalc/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,8 @@
},
).extend(SCHEMA_DAILY_ENERGY_OPTIONS.schema)

SCHEMA_REAL_POWER = vol.Schema(
SCHEMA_REAL_POWER_OPTIONS = vol.Schema(
{
vol.Required(CONF_NAME): selector.TextSelector(),
vol.Required(CONF_ENTITY_ID): selector.EntitySelector(
selector.EntitySelectorConfig(device_class=SensorDeviceClass.POWER),
),
Expand All @@ -139,6 +138,12 @@
},
)

SCHEMA_REAL_POWER = vol.Schema(
{
vol.Required(CONF_NAME): selector.TextSelector(),
},
).extend(SCHEMA_REAL_POWER_OPTIONS.schema)

SCHEMA_POWER_LIBRARY = vol.Schema(
{
vol.Required(CONF_ENTITY_ID): selector.EntitySelector(),
Expand Down Expand Up @@ -775,7 +780,7 @@ async def save_options(
except StrategyConfigurationError as error:
return {"base": error.get_config_flow_translate_key()}

if self.sensor_type == SensorType.GROUP:
if self.sensor_type in [SensorType.GROUP, SensorType.REAL_POWER]:
self._process_user_input(user_input, schema)

self.hass.config_entries.async_update_entry(
Expand Down Expand Up @@ -828,7 +833,7 @@ def build_options_schema(self) -> vol.Schema:
strategy_options = self.current_config.get(self.strategy) or {}

if self.sensor_type == SensorType.REAL_POWER:
data_schema = SCHEMA_REAL_POWER
data_schema = SCHEMA_REAL_POWER_OPTIONS

if self.sensor_type == SensorType.DAILY_ENERGY:
data_schema = SCHEMA_DAILY_ENERGY_OPTIONS
Expand Down
32 changes: 29 additions & 3 deletions tests/config_flow/test_real_power.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from homeassistant import data_entry_flow
from homeassistant.const import CONF_ENTITY_ID, CONF_NAME
from homeassistant.const import CONF_ENTITY_ID, CONF_NAME, CONF_SENSOR_TYPE
from homeassistant.core import HomeAssistant

from custom_components.powercalc import CONF_CREATE_UTILITY_METERS, SensorType
from tests.config_flow.common import select_sensor_type
from tests.config_flow.common import create_mock_entry, initialize_options_flow, select_sensor_type


async def test_real_power(hass: HomeAssistant) -> None:
Expand All @@ -26,4 +26,30 @@ async def test_real_power(hass: HomeAssistant) -> None:


async def test_real_power_options(hass: HomeAssistant) -> None:
pass
entry = create_mock_entry(
hass,
{
CONF_NAME: "Some name",
CONF_ENTITY_ID: "sensor.my_real_power",
CONF_SENSOR_TYPE: SensorType.REAL_POWER,
},
)

result = await initialize_options_flow(hass, entry)

user_input = {
CONF_ENTITY_ID: "sensor.my_new_real_power",
}
result = await hass.config_entries.options.async_configure(
result["flow_id"],
user_input=user_input,
)

await hass.async_block_till_done()

assert result["type"] == data_entry_flow.FlowResultType.CREATE_ENTRY
assert entry.data[CONF_ENTITY_ID] == "sensor.my_new_real_power"

state = hass.states.get("sensor.some_name_energy")
assert state
assert state.attributes.get("source") == "sensor.my_new_real_power"

0 comments on commit a7abc90

Please sign in to comment.