|
33 | 33 | energidataservice_config_option_initial_schema, |
34 | 34 | energidataservice_config_option_tariff_settings, |
35 | 35 | ) |
| 36 | +from .utils.tariffhandler import TariffHandler |
36 | 37 | from .utils.regionhandler import RegionHandler |
| 38 | +from .utils.forecasthandler import ForecastHandler |
37 | 39 |
|
38 | 40 | _LOGGER = logging.getLogger(__name__) |
39 | 41 |
|
40 | 42 |
|
| 43 | +def get_options(area) -> list: |
| 44 | + """Get available options for a given region.""" |
| 45 | + |
| 46 | + options = [] |
| 47 | + |
| 48 | + region = RegionHandler.description_to_region(area) |
| 49 | + tariff_connectors = TariffHandler.get_chargeowners(region) |
| 50 | + forecast_connectors = ForecastHandler.get_forecasts_connectors(region) |
| 51 | + |
| 52 | + if len(tariff_connectors) > 0: |
| 53 | + options.append("tariff") |
| 54 | + |
| 55 | + if len(forecast_connectors) > 0: |
| 56 | + options.append("forecast") |
| 57 | + |
| 58 | + return options |
| 59 | + |
| 60 | + |
41 | 61 | class EnergidataserviceOptionsFlowHandler(config_entries.OptionsFlow): |
42 | 62 | """Energidataservice config flow options handler.""" |
43 | 63 |
|
@@ -138,8 +158,9 @@ async def async_step_enable_extras( |
138 | 158 | data=self.options, |
139 | 159 | ) |
140 | 160 |
|
| 161 | + options = get_options(self.config_entry.options.get(CONF_AREA)) |
141 | 162 | enable_extra_schema = energidataservice_config_option_extras( |
142 | | - self.config_entry.options |
| 163 | + self.config_entry.options, options |
143 | 164 | ) |
144 | 165 | return self.async_show_form( |
145 | 166 | step_id="enable_extras", |
@@ -247,18 +268,26 @@ async def async_step_region(self, user_input: Any | None = None) -> FlowResult: |
247 | 268 |
|
248 | 269 | template_ok = await _validate_template(self.hass, user_input[CONF_TEMPLATE]) |
249 | 270 | if template_ok: |
250 | | - enable_extra_schema = energidataservice_config_option_extras( |
251 | | - self.options |
252 | | - ) |
253 | | - return self.async_show_form( |
254 | | - step_id="enable_extras", |
255 | | - data_schema=vol.Schema(enable_extra_schema), |
256 | | - errors=self._errors, |
257 | | - description_placeholders={ |
258 | | - "name": self.config_entry.data[CONF_NAME], |
259 | | - "country": self.get_country(), |
260 | | - }, |
261 | | - ) |
| 271 | + options = get_options(self.config_entry.options.get(CONF_AREA)) |
| 272 | + if len(options) > 0: |
| 273 | + enable_extra_schema = energidataservice_config_option_extras( |
| 274 | + self.options, options |
| 275 | + ) |
| 276 | + return self.async_show_form( |
| 277 | + step_id="enable_extras", |
| 278 | + data_schema=vol.Schema(enable_extra_schema), |
| 279 | + errors=self._errors, |
| 280 | + description_placeholders={ |
| 281 | + "name": self.config_entry.data[CONF_NAME], |
| 282 | + "country": self.get_country(), |
| 283 | + }, |
| 284 | + ) |
| 285 | + else: |
| 286 | + async_call_later(self.hass, 2, self._do_update) |
| 287 | + return self.async_create_entry( |
| 288 | + title=self.options.get(CONF_NAME), |
| 289 | + data=self.options, |
| 290 | + ) |
262 | 291 | else: |
263 | 292 | self._errors["base"] = "invalid_template" |
264 | 293 | schema = energidataservice_config_option_info_schema(self.config_entry.options) |
@@ -335,18 +364,26 @@ async def async_step_region(self, user_input: Any | None = None) -> FlowResult: |
335 | 364 | template_ok = await _validate_template(self.hass, user_input[CONF_TEMPLATE]) |
336 | 365 | self._async_abort_entries_match({CONF_NAME: user_input[CONF_NAME]}) |
337 | 366 | if template_ok: |
338 | | - enable_extra_schema = energidataservice_config_option_extras( |
339 | | - self.user_input |
340 | | - ) |
341 | | - return self.async_show_form( |
342 | | - step_id="enable_extras", |
343 | | - data_schema=vol.Schema(enable_extra_schema), |
344 | | - errors=self._errors, |
345 | | - description_placeholders={ |
346 | | - "name": self.user_input[CONF_NAME], |
347 | | - "country": self.user_input[CONF_COUNTRY], |
348 | | - }, |
349 | | - ) |
| 367 | + options = get_options(self.user_input.get(CONF_AREA)) |
| 368 | + if len(options) > 0: |
| 369 | + enable_extra_schema = energidataservice_config_option_extras( |
| 370 | + self.user_input, options |
| 371 | + ) |
| 372 | + return self.async_show_form( |
| 373 | + step_id="enable_extras", |
| 374 | + data_schema=vol.Schema(enable_extra_schema), |
| 375 | + errors=self._errors, |
| 376 | + description_placeholders={ |
| 377 | + "name": self.user_input[CONF_NAME], |
| 378 | + "country": self.user_input[CONF_COUNTRY], |
| 379 | + }, |
| 380 | + ) |
| 381 | + else: |
| 382 | + return self.async_create_entry( |
| 383 | + title=user_input[CONF_NAME], |
| 384 | + data={"name": user_input[CONF_NAME]}, |
| 385 | + options=user_input, |
| 386 | + ) |
350 | 387 | else: |
351 | 388 | self._errors["base"] = "invalid_template" |
352 | 389 |
|
@@ -399,7 +436,10 @@ async def async_step_enable_extras( |
399 | 436 | options=user_input, |
400 | 437 | ) |
401 | 438 |
|
402 | | - enable_extra_schema = energidataservice_config_option_extras(self.user_input) |
| 439 | + options = get_options(self.user_input.get(CONF_AREA)) |
| 440 | + enable_extra_schema = energidataservice_config_option_extras( |
| 441 | + self.user_input, options |
| 442 | + ) |
403 | 443 | return self.async_show_form( |
404 | 444 | step_id="enable_extras", |
405 | 445 | data_schema=vol.Schema(enable_extra_schema), |
|
0 commit comments