Skip to content

Commit

Permalink
Merge 599c9ca into 2677467
Browse files Browse the repository at this point in the history
  • Loading branch information
bramstroker committed Dec 2, 2022
2 parents 2677467 + 599c9ca commit 4fdd1c4
Showing 1 changed file with 55 additions and 20 deletions.
75 changes: 55 additions & 20 deletions custom_components/powercalc/config_flow.py
Expand Up @@ -81,10 +81,13 @@

CONF_CONFIRM_AUTODISCOVERED_MODEL = "confirm_autodisovered_model"

MENU_OPTION_LIBRARY = "menu_library"

SENSOR_TYPE_MENU = {
SensorType.DAILY_ENERGY: "Daily energy",
SensorType.VIRTUAL_POWER: "Virtual power",
SensorType.GROUP: "Group",
SensorType.VIRTUAL_POWER: "Virtual power (manual)",
MENU_OPTION_LIBRARY: "Virtual power (library)"
}

SCHEMA_DAILY_ENERGY_OPTIONS = vol.Schema(
Expand Down Expand Up @@ -113,6 +116,14 @@
}
).extend(SCHEMA_DAILY_ENERGY_OPTIONS.schema)

SCHEMA_POWER_LIBRARY = vol.Schema(
{
vol.Required(CONF_ENTITY_ID): selector.EntitySelector(),
vol.Optional(CONF_NAME): selector.TextSelector(),
vol.Optional(CONF_UNIQUE_ID): selector.TextSelector(),
}
)

SCHEMA_POWER_OPTIONS = vol.Schema(
{
vol.Optional(CONF_STANDBY_POWER): vol.Coerce(float),
Expand All @@ -125,27 +136,37 @@
}
)

SCHEMA_POWER_OPTIONS_LIBRARY = vol.Schema(
{
vol.Optional(
CONF_CREATE_ENERGY_SENSOR, default=True
): selector.BooleanSelector(),
vol.Optional(
CONF_CREATE_UTILITY_METERS, default=False
): selector.BooleanSelector(),
}
)

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(),
vol.Optional(
CONF_MODE, default=CalculationStrategy.FIXED
): selector.SelectSelector(
selector.SelectSelectorConfig(
options=[
CalculationStrategy.FIXED,
CalculationStrategy.LINEAR,
CalculationStrategy.WLED,
CalculationStrategy.LUT,
],
mode=selector.SelectSelectorMode.DROPDOWN,
)
),
}
)

STRATEGY_SELECTOR = selector.SelectSelector(
selector.SelectSelectorConfig(
options=[
CalculationStrategy.FIXED,
CalculationStrategy.LINEAR,
CalculationStrategy.WLED,
CalculationStrategy.LUT,
],
mode=selector.SelectSelectorMode.DROPDOWN,
)
)

SCHEMA_POWER_FIXED = vol.Schema(
{
vol.Optional(CONF_POWER): vol.Coerce(float),
Expand Down Expand Up @@ -205,6 +226,7 @@ def __init__(self):
self.source_entity_id: str | None = None
self.power_profile: PowerProfile | None = None
self.skip_advanced_step: bool = False
self.is_library_flow: bool = False

@staticmethod
@callback
Expand Down Expand Up @@ -246,6 +268,7 @@ async def async_step_integration_discovery(
"manufacturer": self.sensor_config.get(CONF_MANUFACTURER),
"model": self.sensor_config.get(CONF_MODEL),
}
self.is_library_flow = True

if discovery_info.get(CONF_MODE) == CalculationStrategy.WLED:
return await self.async_step_wled()
Expand All @@ -257,8 +280,16 @@ async def async_step_user(self, user_input=None) -> FlowResult:

return self.async_show_menu(step_id="user", menu_options=SENSOR_TYPE_MENU)

async def async_step_menu_library(self) -> FlowResult:
"""
Handle the Virtual power (library) step.
We forward to the virtual_power step, but without the strategy selector displayed
"""
self.is_library_flow = True
return await self.async_step_virtual_power()

async def async_step_virtual_power(
self, user_input: dict[str, str] = None
self, user_input: dict[str, str] = None, strategy_selection: bool = True
) -> FlowResult:
if user_input is not None:
self.source_entity_id = user_input[CONF_ENTITY_ID]
Expand All @@ -278,6 +309,9 @@ async def async_step_virtual_power(
self.selected_sensor_type = SensorType.VIRTUAL_POWER
self.sensor_config.update(user_input)

if user_input.get(CONF_MODE) == CalculationStrategy.LUT or not strategy_selection:
return await self.async_step_library()

if user_input.get(CONF_MODE) == CalculationStrategy.FIXED:
return await self.async_step_fixed()

Expand All @@ -287,12 +321,9 @@ async def async_step_virtual_power(
if user_input.get(CONF_MODE) == CalculationStrategy.WLED:
return await self.async_step_wled()

if user_input.get(CONF_MODE) == CalculationStrategy.LUT:
return await self.async_step_library()

return self.async_show_form(
step_id="virtual_power",
data_schema=_create_virtual_power_schema(self.hass),
data_schema=_create_virtual_power_schema(self.hass, not self.is_library_flow),
errors={},
)

Expand Down Expand Up @@ -671,10 +702,14 @@ def _get_strategy_schema(strategy: str, source_entity_id: str) -> vol.Schema:
return vol.Schema({})


def _create_virtual_power_schema(hass: HomeAssistant) -> vol.Schema:
def _create_virtual_power_schema(hass: HomeAssistant, strategy_selection: bool = True) -> vol.Schema:
base_schema: vol.Schema = SCHEMA_POWER_BASE.extend(
{vol.Optional(CONF_GROUP): _create_group_selector(hass)}
)
if strategy_selection:
base_schema = base_schema.extend({vol.Optional(CONF_MODE, default=CalculationStrategy.FIXED): STRATEGY_SELECTOR})
return base_schema.extend(SCHEMA_POWER_OPTIONS_LIBRARY.schema)

return base_schema.extend(SCHEMA_POWER_OPTIONS.schema)


Expand Down

0 comments on commit 4fdd1c4

Please sign in to comment.