diff --git a/custom_components/powercalc/config_flow.py b/custom_components/powercalc/config_flow.py index 8bbc1402f..24765f603 100644 --- a/custom_components/powercalc/config_flow.py +++ b/custom_components/powercalc/config_flow.py @@ -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), ), @@ -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(), @@ -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( @@ -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 diff --git a/tests/config_flow/test_real_power.py b/tests/config_flow/test_real_power.py index 0e84158b6..ba84ea027 100644 --- a/tests/config_flow/test_real_power.py +++ b/tests/config_flow/test_real_power.py @@ -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: @@ -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"