Skip to content

Commit

Permalink
Merge 620d57a into 4260346
Browse files Browse the repository at this point in the history
  • Loading branch information
bramstroker committed Oct 29, 2022
2 parents 4260346 + 620d57a commit fed92c6
Show file tree
Hide file tree
Showing 10 changed files with 127 additions and 126 deletions.
109 changes: 55 additions & 54 deletions custom_components/powercalc/config_flow.py
Expand Up @@ -61,8 +61,8 @@
CONF_VALUE_TEMPLATE,
CONF_WLED,
DOMAIN,
ENERGY_INTEGRATION_METHOD_LEFT,
ENERGY_INTEGRATION_METHODS,
ENERGY_INTEGRATION_METHOD_LEFT,
CalculationStrategy,
SensorType,
)
Expand Down Expand Up @@ -161,7 +161,7 @@
}
)

SCHEMA_POWER_LUT_AUTODISCOVERED = vol.Schema(
SCHEMA_POWER_AUTODISCOVERED = vol.Schema(
{vol.Optional(CONF_CONFIRM_AUTODISCOVERED_MODEL, default=True): bool}
)

Expand All @@ -170,9 +170,7 @@
vol.Optional(CONF_CALCULATION_ENABLED_CONDITION): selector.TemplateSelector(),
vol.Optional(CONF_IGNORE_UNAVAILABLE_STATE): selector.BooleanSelector(),
vol.Optional(CONF_MULTIPLY_FACTOR): vol.Coerce(float),
vol.Optional(
CONF_ENERGY_INTEGRATION_METHOD, default=ENERGY_INTEGRATION_METHOD_LEFT
): selector.SelectSelector(
vol.Optional(CONF_ENERGY_INTEGRATION_METHOD, default=ENERGY_INTEGRATION_METHOD_LEFT): selector.SelectSelector(
selector.SelectSelectorConfig(
options=ENERGY_INTEGRATION_METHODS,
mode=selector.SelectSelectorMode.DROPDOWN,
Expand Down Expand Up @@ -211,7 +209,7 @@ def async_get_options_flow(config_entry: ConfigEntry) -> OptionsFlow:
return OptionsFlowHandler(config_entry)

async def async_step_integration_discovery(
self, discovery_info: DiscoveryInfoType
self, discovery_info: DiscoveryInfoType
) -> FlowResult:
"""Handle integration discovery."""

Expand All @@ -235,25 +233,25 @@ async def async_step_integration_discovery(
"manufacturer": self.sensor_config.get(CONF_MANUFACTURER),
"model": self.sensor_config.get(CONF_MODEL),
}
return await self.async_step_lut()
return await self.async_step_library()

async def async_step_user(self, user_input=None) -> FlowResult:
"""Handle the initial step."""

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

async def async_step_virtual_power(
self, user_input: dict[str, str] = None
self, user_input: dict[str, str] = None
) -> FlowResult:
if user_input is not None:
self.source_entity_id = user_input[CONF_ENTITY_ID]
self.source_entity = await create_source_entity(
self.source_entity_id, self.hass
)
unique_id = (
user_input.get(CONF_UNIQUE_ID)
or self.source_entity.unique_id
or self.source_entity_id
user_input.get(CONF_UNIQUE_ID)
or self.source_entity.unique_id
or self.source_entity_id
)

await self.async_set_unique_id(unique_id)
Expand All @@ -273,7 +271,7 @@ async def async_step_virtual_power(
return await self.async_step_wled()

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

return self.async_show_form(
step_id="virtual_power",
Expand All @@ -282,7 +280,7 @@ async def async_step_virtual_power(
)

async def async_step_daily_energy(
self, user_input: dict[str, str] = None
self, user_input: dict[str, str] = None
) -> FlowResult:
errors = _validate_daily_energy_input(user_input)

Expand Down Expand Up @@ -371,8 +369,11 @@ async def async_step_wled(self, user_input: dict[str, str] = None) -> FlowResult
errors=errors,
)

async def async_step_lut(self, user_input: dict[str, str] = None) -> FlowResult:
"""Try to autodiscover manufacturer/model first. Ask the user to confirm this or forward to manual configuration"""
async def async_step_library(self, user_input: dict[str, str] = None) -> FlowResult:
"""
Try to autodiscover manufacturer/model first.
Ask the user to confirm this or forward to manual library selection
"""
if user_input is not None:
if user_input.get(CONF_CONFIRM_AUTODISCOVERED_MODEL) and self.power_profile:
self.sensor_config.update(
Expand All @@ -383,7 +384,7 @@ async def async_step_lut(self, user_input: dict[str, str] = None) -> FlowResult:
)
return await self.async_step_power_advanced()

return await self.async_step_lut_manufacturer()
return await self.async_step_manufacturer()

if self.source_entity.entity_entry:
try:
Expand All @@ -394,36 +395,36 @@ async def async_step_lut(self, user_input: dict[str, str] = None) -> FlowResult:
self.power_profile = None
if self.power_profile:
return self.async_show_form(
step_id="lut",
step_id="library",
description_placeholders={
"manufacturer": self.power_profile.manufacturer,
"model": self.power_profile.model,
},
data_schema=SCHEMA_POWER_LUT_AUTODISCOVERED,
data_schema=SCHEMA_POWER_AUTODISCOVERED,
errors={},
)

return await self.async_step_lut_manufacturer()
return await self.async_step_manufacturer()

async def async_step_lut_manufacturer(
self, user_input: dict[str, str] = None
async def async_step_manufacturer(
self, user_input: dict[str, str] = None
) -> FlowResult:
"""Ask the user to select the manufacturer"""
if user_input is not None:
self.sensor_config.update(
{CONF_MANUFACTURER: user_input.get(CONF_MANUFACTURER)}
)
return await self.async_step_lut_model()
return await self.async_step_model()

schema = _create_lut_schema_manufacturer(self.hass)
schema = _create_schema_manufacturer(self.hass)
return self.async_show_form(
step_id="lut_manufacturer",
step_id="manufacturer",
data_schema=schema,
errors={},
)

async def async_step_lut_model(
self, user_input: dict[str, str] = None
async def async_step_model(
self, user_input: dict[str, str] = None
) -> FlowResult:
errors: dict[str, str] = {}
if user_input is not None:
Expand All @@ -437,14 +438,14 @@ async def async_step_lut_model(
)
sub_profiles = profile.get_sub_profiles()
if sub_profiles:
return await self.async_step_lut_subprofile()
return await self.async_step_sub_profile()
errors = await self.validate_strategy_config()
if not errors:
return await self.async_step_power_advanced()

return self.async_show_form(
step_id="lut_model",
data_schema=_create_lut_schema_model(
step_id="model",
data_schema=_create_schema_model(
self.hass, self.sensor_config.get(CONF_MANUFACTURER)
),
description_placeholders={
Expand All @@ -453,8 +454,8 @@ async def async_step_lut_model(
errors=errors,
)

async def async_step_lut_subprofile(
self, user_input: dict[str, str] = None
async def async_step_sub_profile(
self, user_input: dict[str, str] = None
) -> FlowResult:
errors: dict[str, str] = {}
if user_input is not None:
Expand All @@ -470,13 +471,13 @@ async def async_step_lut_subprofile(
self.sensor_config.get(CONF_MODEL),
)
return self.async_show_form(
step_id="lut_subprofile",
data_schema=await _create_lut_schema_subprofile(self.hass, model_info),
step_id="sub_profile",
data_schema=await _create_schema_sub_profile(self.hass, model_info),
errors=errors,
)

async def async_step_power_advanced(
self, user_input: dict[str, str] = None
self, user_input: dict[str, str] = None
) -> FlowResult:
errors: dict[str, str] = {}
if user_input is not None or self.skip_advanced_step:
Expand Down Expand Up @@ -526,13 +527,13 @@ def __init__(self, config_entry: ConfigEntry) -> None:
self.config_entry = config_entry
self.current_config: dict = dict(config_entry.data)
self.sensor_type: SensorType = (
self.current_config.get(CONF_SENSOR_TYPE) or SensorType.VIRTUAL_POWER
self.current_config.get(CONF_SENSOR_TYPE) or SensorType.VIRTUAL_POWER
)
self.source_entity_id: str | None = self.current_config.get(CONF_ENTITY_ID)
self.source_entity: SourceEntity | None = None

async def async_step_init(
self, user_input: dict[str, Any] | None = None
self, user_input: dict[str, Any] | None = None
) -> FlowResult:
"""Handle options flow."""

Expand Down Expand Up @@ -628,7 +629,7 @@ def build_options_schema(self) -> vol.Schema:


async def _create_strategy_object(
hass: HomeAssistant, strategy: str, config: dict, source_entity: SourceEntity
hass: HomeAssistant, strategy: str, config: dict, source_entity: SourceEntity
) -> PowerCalculationStrategyInterface:
"""Create the calculation strategy object"""
factory = PowerCalculatorStrategyFactory(hass)
Expand Down Expand Up @@ -667,7 +668,7 @@ def _create_group_options_schema(hass: HomeAssistant) -> vol.Schema:
)
for config_entry in hass.config_entries.async_entries(DOMAIN)
if config_entry.data.get(CONF_SENSOR_TYPE) == SensorType.VIRTUAL_POWER
and config_entry.unique_id is not None
and config_entry.unique_id is not None
]
member_sensor_selector = selector.SelectSelector(
selector.SelectSelectorConfig(
Expand Down Expand Up @@ -704,7 +705,7 @@ def _create_group_options_schema(hass: HomeAssistant) -> vol.Schema:


def _create_group_selector(
hass: HomeAssistant, multiple: bool = False
hass: HomeAssistant, multiple: bool = False
) -> selector.SelectSelector:
options = [
selector.SelectOptionDict(
Expand All @@ -730,10 +731,10 @@ def _validate_group_input(user_input: dict[str, str] = None) -> dict:
errors: dict[str, str] = {}

if (
CONF_SUB_GROUPS not in user_input
and CONF_GROUP_POWER_ENTITIES not in user_input
and CONF_GROUP_ENERGY_ENTITIES not in user_input
and CONF_GROUP_MEMBER_SENSORS not in user_input
CONF_SUB_GROUPS not in user_input
and CONF_GROUP_POWER_ENTITIES not in user_input
and CONF_GROUP_ENERGY_ENTITIES not in user_input
and CONF_GROUP_MEMBER_SENSORS not in user_input
):
errors["base"] = "group_mandatory"

Expand All @@ -751,8 +752,8 @@ def _create_linear_schema(source_entity_id: str) -> vol.Schema:
)


def _create_lut_schema_manufacturer(hass: HomeAssistant) -> vol.Schema:
"""Create LUT schema"""
def _create_schema_manufacturer(hass: HomeAssistant) -> vol.Schema:
"""Create manufacturer schema"""
library = ProfileLibrary(hass)
manufacturers = [
selector.SelectOptionDict(value=manufacturer, label=manufacturer)
Expand All @@ -769,8 +770,8 @@ def _create_lut_schema_manufacturer(hass: HomeAssistant) -> vol.Schema:
)


def _create_lut_schema_model(hass: HomeAssistant, manufacturer: str) -> vol.Schema:
"""Create LUT schema"""
def _create_schema_model(hass: HomeAssistant, manufacturer: str) -> vol.Schema:
"""Create model schema"""
library = ProfileLibrary(hass)
models = [
selector.SelectOptionDict(value=model, label=model)
Expand All @@ -787,10 +788,10 @@ def _create_lut_schema_model(hass: HomeAssistant, manufacturer: str) -> vol.Sche
)


async def _create_lut_schema_subprofile(
hass: HomeAssistant, model_info: ModelInfo
async def _create_schema_sub_profile(
hass: HomeAssistant, model_info: ModelInfo
) -> vol.Schema:
"""Create LUT schema"""
"""Create sub profile schema"""
library = ProfileLibrary(hass)
profile = await library.get_profile(model_info)
sub_profiles = [
Expand All @@ -809,7 +810,7 @@ async def _create_lut_schema_subprofile(


def _build_strategy_config(
strategy: str, source_entity_id: str, user_input: dict[str, str] = None
strategy: str, source_entity_id: str, user_input: dict[str, str] = None
) -> dict[str, Any]:
"""Build the config dict needed for the configured strategy"""
strategy_schema = _get_strategy_schema(strategy, source_entity_id)
Expand Down Expand Up @@ -851,9 +852,9 @@ def _fill_schema_defaults(data_schema: vol.Schema, options: dict[str, str]):
new_key = key
if key in options and isinstance(key, vol.Marker):
if (
isinstance(key, vol.Optional)
and callable(key.default)
and key.default()
isinstance(key, vol.Optional)
and callable(key.default)
and key.default()
):
new_key = vol.Optional(key.schema, default=options.get(key))
else:
Expand Down
18 changes: 9 additions & 9 deletions custom_components/powercalc/strings.json
Expand Up @@ -104,32 +104,32 @@
"power_factor": "Power factor"
}
},
"lut": {
"title": "LUT config",
"description": "Manufacturer '({manufacturer})' and model '({model})' were automatically detected for your light",
"library": {
"title": "Library",
"description": "Manufacturer '{manufacturer}' and model '{model}' were automatically detected for your device",
"data": {
"confirm_autodisovered_model": "Confirm model"
},
"data_description": {
"confirm_autodisovered_model": "If you choose not to confirm, you can input the manufacturer and model yourself"
}
},
"lut_manufacturer": {
"title": "LUT config",
"manufacturer": {
"title": "Manufacturer",
"description": "Select the device manufacturer",
"data": {
"manufacturer": "Manufacturer"
}
},
"lut_model": {
"title": "LUT config",
"model": {
"title": "Model",
"description": "Select the device model. See the [list]({supported_models_link}) of supported models for more information",
"data": {
"model": "Model ID"
}
},
"lut_subprofile": {
"title": "LUT config",
"sub_profile": {
"title": "Sub profile",
"description": "This model has multiple sub profiles. Select one that suites your device",
"data": {
"sub_profile": "Sub profile"
Expand Down

0 comments on commit fed92c6

Please sign in to comment.