|
47 | 47 | PowercalcFormStep, |
48 | 48 | Step, |
49 | 49 | fill_schema_defaults, |
50 | | - unwrap_choose_selector, |
51 | | - wrap_choose_selector, |
52 | 50 | ) |
53 | 51 | from custom_components.powercalc.flow_helper.flows.global_configuration import get_global_powercalc_config |
54 | 52 | from custom_components.powercalc.flow_helper.flows.library import ( |
|
61 | 59 | SCHEMA_SENSOR_ENERGY_OPTIONS, |
62 | 60 | SCHEMA_UTILITY_METER_TOGGLE, |
63 | 61 | ) |
| 62 | +from custom_components.powercalc.flow_helper.strategy_form import ( |
| 63 | + FIXED_CHOICES, |
| 64 | + find_present_choice, |
| 65 | + order_choices_for_default, |
| 66 | + unwrap_strategy_user_input, |
| 67 | + wrap_strategy_form_data, |
| 68 | +) |
64 | 69 | from custom_components.powercalc.power_profile.power_profile import DeviceType |
65 | 70 | from custom_components.powercalc.strategy.wled import CONFIG_SCHEMA as SCHEMA_POWER_WLED |
66 | 71 |
|
|
110 | 115 | } |
111 | 116 |
|
112 | 117 |
|
113 | | -def order_choices_for_default[T]( |
114 | | - choices: dict[str, T], |
115 | | - default_choice: str | None, |
116 | | -) -> dict[str, T]: |
117 | | - """Put the default choice first because HA initializes choose selectors from the first choice.""" |
118 | | - if default_choice not in choices: |
119 | | - return choices |
120 | | - return { |
121 | | - default_choice: choices[default_choice], |
122 | | - **{choice: config for choice, config in choices.items() if choice != default_choice}, |
123 | | - } |
124 | | - |
125 | | - |
126 | | -def has_saved_choice_value(value: object) -> bool: |
127 | | - """Return whether a saved strategy value should drive the selected form choice.""" |
128 | | - if value is None: |
129 | | - return False |
130 | | - if isinstance(value, (str, list, dict)): |
131 | | - return bool(value) |
132 | | - return True |
133 | | - |
134 | | - |
135 | | -def find_present_choice(form_data: dict[str, Any], choices: dict[str, list[str] | str]) -> str | None: |
136 | | - """Find the first choice that has matching config data.""" |
137 | | - for choice_id, mapping in choices.items(): |
138 | | - keys = [mapping] if isinstance(mapping, str) else mapping |
139 | | - if any(has_saved_choice_value(form_data.get(key)) for key in keys): |
140 | | - return choice_id |
141 | | - return None |
142 | | - |
143 | | - |
144 | 118 | SCHEMA_POWER_FIXED = vol.Schema( |
145 | 119 | { |
146 | 120 | vol.Required(CONF_FIXED_VALUE): selector.ChooseSelector( |
@@ -177,49 +151,6 @@ def find_present_choice(form_data: dict[str, Any], choices: dict[str, list[str] |
177 | 151 | }, |
178 | 152 | ) |
179 | 153 |
|
180 | | -FIXED_CHOICES: dict[str, list[str] | str] = { |
181 | | - CONF_STATES_POWER: CONF_STATES_POWER, |
182 | | - CONF_POWER_TEMPLATE: CONF_POWER_TEMPLATE, |
183 | | - CONF_POWER: CONF_POWER, |
184 | | -} |
185 | | - |
186 | | - |
187 | | -def fixed_choice_key_from_validated_value(value: object) -> str: |
188 | | - """Infer the fixed strategy config key from a validated ChooseSelector value.""" |
189 | | - if isinstance(value, list): |
190 | | - return CONF_STATES_POWER |
191 | | - if isinstance(value, str): |
192 | | - return CONF_POWER_TEMPLATE |
193 | | - return CONF_POWER |
194 | | - |
195 | | - |
196 | | -def unwrap_strategy_user_input(strategy: CalculationStrategy, user_input: dict[str, Any]) -> dict[str, Any]: |
197 | | - """Unwrap ChooseSelector wrappers and normalize list/dict shapes for strategy user input.""" |
198 | | - if strategy == CalculationStrategy.FIXED: |
199 | | - unwrap_choose_selector(user_input, CONF_FIXED_VALUE, fixed_choice_key_from_validated_value) |
200 | | - if CONF_STATE_TRIGGER in user_input and isinstance(user_input[CONF_STATE_TRIGGER], list): |
201 | | - user_input[CONF_STATE_TRIGGER] = { |
202 | | - item[CONF_STATE]: item[CONF_PLAYBOOK_ID] for item in user_input[CONF_STATE_TRIGGER] |
203 | | - } |
204 | | - return user_input |
205 | | - |
206 | | - |
207 | | -def wrap_strategy_form_data(strategy: CalculationStrategy, form_data: dict[str, Any]) -> dict[str, Any]: |
208 | | - """Wrap flat strategy config back into ChooseSelector form structure for display.""" |
209 | | - if strategy == CalculationStrategy.FIXED: |
210 | | - choices = order_choices_for_default(FIXED_CHOICES, find_present_choice(form_data, FIXED_CHOICES)) |
211 | | - form_data = wrap_choose_selector(form_data, CONF_FIXED_VALUE, choices, raw_value=True) |
212 | | - if CONF_STATE_TRIGGER in form_data and isinstance(form_data[CONF_STATE_TRIGGER], dict): |
213 | | - form_data = { |
214 | | - **form_data, |
215 | | - CONF_STATE_TRIGGER: [ |
216 | | - {CONF_STATE: state, CONF_PLAYBOOK_ID: playbook_id} |
217 | | - for state, playbook_id in form_data[CONF_STATE_TRIGGER].items() |
218 | | - ], |
219 | | - } |
220 | | - return form_data |
221 | | - |
222 | | - |
223 | 154 | SCHEMA_POWER_MULTI_SWITCH_MANUAL = vol.Schema( |
224 | 155 | { |
225 | 156 | vol.Required(CONF_POWER): vol.Coerce(float), |
|
0 commit comments