Skip to content

Commit 0e3546b

Browse files
authored
Merge pull request #226 from MTrab/Hide-tariffs-selection-for-regions-without-tariff-connector
Only show available options for the region
2 parents 6636940 + a0180db commit 0e3546b

File tree

3 files changed

+117
-35
lines changed

3 files changed

+117
-35
lines changed

custom_components/energidataservice/config_flow.py

Lines changed: 66 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,31 @@
3333
energidataservice_config_option_initial_schema,
3434
energidataservice_config_option_tariff_settings,
3535
)
36+
from .utils.tariffhandler import TariffHandler
3637
from .utils.regionhandler import RegionHandler
38+
from .utils.forecasthandler import ForecastHandler
3739

3840
_LOGGER = logging.getLogger(__name__)
3941

4042

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+
4161
class EnergidataserviceOptionsFlowHandler(config_entries.OptionsFlow):
4262
"""Energidataservice config flow options handler."""
4363

@@ -138,8 +158,9 @@ async def async_step_enable_extras(
138158
data=self.options,
139159
)
140160

161+
options = get_options(self.config_entry.options.get(CONF_AREA))
141162
enable_extra_schema = energidataservice_config_option_extras(
142-
self.config_entry.options
163+
self.config_entry.options, options
143164
)
144165
return self.async_show_form(
145166
step_id="enable_extras",
@@ -247,18 +268,26 @@ async def async_step_region(self, user_input: Any | None = None) -> FlowResult:
247268

248269
template_ok = await _validate_template(self.hass, user_input[CONF_TEMPLATE])
249270
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+
)
262291
else:
263292
self._errors["base"] = "invalid_template"
264293
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:
335364
template_ok = await _validate_template(self.hass, user_input[CONF_TEMPLATE])
336365
self._async_abort_entries_match({CONF_NAME: user_input[CONF_NAME]})
337366
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+
)
350387
else:
351388
self._errors["base"] = "invalid_template"
352389

@@ -399,7 +436,10 @@ async def async_step_enable_extras(
399436
options=user_input,
400437
)
401438

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+
)
403443
return self.async_show_form(
404444
step_id="enable_extras",
405445
data_schema=vol.Schema(enable_extra_schema),

custom_components/energidataservice/utils/configuration_schema.py

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -150,23 +150,42 @@ def energidataservice_config_option_info_schema(options: ConfigEntry = {}) -> di
150150
return schema
151151

152152

153-
def energidataservice_config_option_extras(options: ConfigEntry = {}) -> dict:
153+
def energidataservice_config_option_extras(
154+
options: ConfigEntry = {}, selections: list = ["ALL"]
155+
) -> dict:
154156
"""Return a schema for enabling forecasts."""
155157
_LOGGER.debug(options)
158+
159+
if "ALL" in selections:
160+
selections = ["forecast", "tariff"]
161+
156162
if not options:
157163
options = {
158164
CONF_ENABLE_FORECAST: False,
159165
CONF_ENABLE_TARIFFS: False,
160166
}
161167

162-
schema = {
163-
vol.Required(
164-
CONF_ENABLE_FORECAST, default=options.get(CONF_ENABLE_FORECAST) or False
165-
): bool,
166-
vol.Required(
167-
CONF_ENABLE_TARIFFS, default=options.get(CONF_ENABLE_TARIFFS) or False
168-
): bool,
169-
}
168+
schema = {}
169+
170+
if "forecast" in selections:
171+
schema.update(
172+
{
173+
vol.Required(
174+
CONF_ENABLE_FORECAST,
175+
default=options.get(CONF_ENABLE_FORECAST) or False,
176+
): bool
177+
}
178+
)
179+
180+
if "tariff" in selections:
181+
schema.update(
182+
{
183+
vol.Required(
184+
CONF_ENABLE_TARIFFS,
185+
default=options.get(CONF_ENABLE_TARIFFS) or False,
186+
): bool
187+
}
188+
)
170189

171190
_LOGGER.debug("Schema: %s", schema)
172191
return schema
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
"""For handling forecasts."""
2+
from __future__ import annotations
3+
4+
from importlib import import_module
5+
import logging
6+
7+
from ..forecasts import Forecast
8+
9+
_LOGGER = logging.getLogger(__name__)
10+
11+
12+
class ForecastHandler:
13+
"""Forecast handler."""
14+
15+
@staticmethod
16+
def get_forecasts_connectors(
17+
region: str, sort: bool = False, descending: bool = False
18+
) -> list:
19+
"""Get a list of forecast connectors for this region."""
20+
connectors = Forecast().get_endpoint(region)
21+
_LOGGER.debug("Forecast connectors: %s", connectors)
22+
23+
return connectors

0 commit comments

Comments
 (0)