Repository: bramstroker/homeassistant-powercalc

Issue:
Improvements config flow virtual power from library
- Limit entity selection to only entities having domain switch, light or media_player
- Remove standby power from power options (as it is provided by the power profile already)
- Change submit buttons to next so it's clear for the user there are next steps.


Discussion:

Resolving patch:
diff --git a/custom_components/powercalc/config_flow.py b/custom_components/powercalc/config_flow.py
--- a/custom_components/powercalc/config_flow.py
+++ b/custom_components/powercalc/config_flow.py
@@ -73,7 +73,7 @@
 from .errors import ModelNotSupported, StrategyConfigurationError
 from .power_profile.factory import get_power_profile
 from .power_profile.library import ModelInfo, ProfileLibrary
-from .power_profile.power_profile import PowerProfile
+from .power_profile.power_profile import PowerProfile, DEVICE_DOMAINS
 from .sensors.daily_energy import DEFAULT_DAILY_UPDATE_FREQUENCY
 from .strategy.factory import PowerCalculatorStrategyFactory
 from .strategy.strategy_interface import PowerCalculationStrategyInterface
@@ -151,7 +151,6 @@
 
 SCHEMA_POWER_BASE = vol.Schema(
     {
-        vol.Required(CONF_ENTITY_ID): selector.EntitySelector(),
         vol.Optional(CONF_NAME): selector.TextSelector(),
         vol.Optional(CONF_UNIQUE_ID): selector.TextSelector(),
     }
@@ -332,9 +331,10 @@ async def async_step_virtual_power(
         return self.async_show_form(
             step_id="virtual_power",
             data_schema=_create_virtual_power_schema(
-                self.hass, not self.is_library_flow
+                self.hass, self.is_library_flow
             ),
             errors={},
+            last_step=False
         )
 
     async def async_step_daily_energy(
@@ -397,6 +397,7 @@ async def async_step_fixed(self, user_input: dict[str, str] = None) -> FlowResul
             step_id="fixed",
             data_schema=SCHEMA_POWER_FIXED,
             errors=errors,
+            last_step=False,
         )
 
     async def async_step_linear(self, user_input: dict[str, str] = None) -> FlowResult:
@@ -411,6 +412,7 @@ async def async_step_linear(self, user_input: dict[str, str] = None) -> FlowResu
             step_id="linear",
             data_schema=_create_linear_schema(self.source_entity_id),
             errors=errors,
+            last_step=False,
         )
 
     async def async_step_wled(self, user_input: dict[str, str] = None) -> FlowResult:
@@ -425,6 +427,7 @@ async def async_step_wled(self, user_input: dict[str, str] = None) -> FlowResult
             step_id="wled",
             data_schema=SCHEMA_POWER_WLED,
             errors=errors,
+            last_step=False,
         )
 
     async def async_step_library(self, user_input: dict[str, str] = None) -> FlowResult:
@@ -468,6 +471,7 @@ async def async_step_library(self, user_input: dict[str, str] = None) -> FlowRes
                 },
                 data_schema=SCHEMA_POWER_AUTODISCOVERED,
                 errors={},
+                last_step=False,
             )
 
         return await self.async_step_manufacturer()
@@ -487,6 +491,7 @@ async def async_step_manufacturer(
             step_id="manufacturer",
             data_schema=schema,
             errors={},
+            last_step=False,
         )
 
     async def async_step_model(self, user_input: dict[str, str] = None) -> FlowResult:
@@ -516,6 +521,7 @@ async def async_step_model(self, user_input: dict[str, str] = None) -> FlowResul
                 "supported_models_link": "https://github.com/bramstroker/homeassistant-powercalc/blob/master/docs/supported_models.md"
             },
             errors=errors,
+            last_step=False,
         )
 
     async def async_step_post_library(self, user_input: dict[str, str] = None):
@@ -549,6 +555,7 @@ async def async_step_sub_profile(
             step_id="sub_profile",
             data_schema=await _create_schema_sub_profile(self.hass, model_info),
             errors=errors,
+            last_step=False,
         )
 
     async def async_step_power_advanced(
@@ -755,22 +762,34 @@ def _get_strategy_schema(strategy: str, source_entity_id: str) -> vol.Schema:
 
 
 def _create_virtual_power_schema(
-    hass: HomeAssistant, strategy_selection: bool = True
+    hass: HomeAssistant, is_library_flow: bool = True
 ) -> vol.Schema:
-    base_schema: vol.Schema = SCHEMA_POWER_BASE.extend(
+    if is_library_flow:
+        entity_selector = selector.EntitySelector(
+            selector.EntitySelectorConfig(domain=list(DEVICE_DOMAINS.values()))
+        )
+    else:
+        entity_selector = selector.EntitySelector()
+
+    schema = vol.Schema(
+        {
+            vol.Required(CONF_ENTITY_ID): entity_selector,
+        }
+    ).extend(SCHEMA_POWER_BASE.schema)
+    schema = schema.extend(
         {vol.Optional(CONF_GROUP): _create_group_selector(hass)}
     )
-    if strategy_selection:
-        base_schema = base_schema.extend(
+    if not is_library_flow:
+        schema = schema.extend(
             {
                 vol.Optional(
                     CONF_MODE, default=CalculationStrategy.FIXED
                 ): STRATEGY_SELECTOR
             }
         )
-        return base_schema.extend(SCHEMA_POWER_OPTIONS_LIBRARY.schema)
+        return schema.extend(SCHEMA_POWER_OPTIONS.schema)
 
-    return base_schema.extend(SCHEMA_POWER_OPTIONS.schema)
+    return schema.extend(SCHEMA_POWER_OPTIONS_LIBRARY.schema)
 
 
 def _create_group_options_schema(hass: HomeAssistant) -> vol.Schema:


Test patch:
diff --git a/tests/test_config_flow.py b/tests/test_config_flow.py
--- a/tests/test_config_flow.py
+++ b/tests/test_config_flow.py
@@ -504,6 +504,25 @@ async def test_manufacturer_listing_is_filtered_by_entity_domain(
     assert {"value": "signify", "label": "signify"} in manufacturer_options
 
 
+async def test_manufacturer_listing_is_filtered_by_entity_domain2(
+    hass: HomeAssistant,
+) -> None:
+    result = await _goto_virtual_power_strategy_step(hass, CalculationStrategy.LUT,
+                                                     {
+                                                         CONF_ENTITY_ID: "switch.test",
+                                                         CONF_MODE: CalculationStrategy.LUT,
+                                                         CONF_UNIQUE_ID: DEFAULT_UNIQUE_ID,
+                                                     }
+                                                     )
+
+    assert result["type"] == data_entry_flow.FlowResultType.FORM
+    assert result["step_id"] == "manufacturer"
+    data_schema: vol.Schema = result["data_schema"]
+    manufacturer_select: SelectSelector = data_schema.schema["manufacturer"]
+    manufacturer_options = manufacturer_select.config["options"]
+    assert {"value": "sonos", "label": "sonos"} not in manufacturer_options
+    assert {"value": "shelly", "label": "shelly"} in manufacturer_options
+
 async def test_advanced_power_configuration_can_be_set(hass: HomeAssistant):
     result = await _goto_virtual_power_strategy_step(hass, CalculationStrategy.FIXED)
     advanced_options = {