1 """Configflow for the emoncms integration."""
3 from __future__
import annotations
7 from pyemoncms
import EmoncmsClient
8 import voluptuous
as vol
24 CONF_ONLY_INCLUDE_FEEDID,
34 def get_options(feeds: list[dict[str, Any]]) -> list[dict[str, Any]]:
35 """Build the selector options with the feed list."""
38 "value": feed[FEED_ID],
39 "label": f
"{feed[FEED_ID]}|{feed[FEED_TAG]}|{feed[FEED_NAME]}",
46 """Return sensor name."""
47 sensorip = url.rsplit(
"//", maxsplit=1)[-1]
48 return f
"emoncms@{sensorip}"
52 emoncms_client: EmoncmsClient,
54 """Check connection to emoncms and return feed list if successful."""
55 return await emoncms_client.async_request(
"/feed/list.json")
59 """emoncms integration UI config flow."""
63 include_only_feeds: list |
None =
None
69 config_entry: ConfigEntry,
70 ) -> EmoncmsOptionsFlow:
71 """Get the options flow for this handler."""
75 self, user_input: dict[str, Any] |
None =
None
76 ) -> ConfigFlowResult:
77 """Initiate a flow via the UI."""
78 errors: dict[str, str] = {}
79 description_placeholders = {}
81 if user_input
is not None:
82 self.
urlurl = user_input[CONF_URL]
83 self.
api_keyapi_key = user_input[CONF_API_KEY]
86 CONF_API_KEY: self.
api_keyapi_key,
87 CONF_URL: self.
urlurl,
90 emoncms_client = EmoncmsClient(
94 if not result[CONF_SUCCESS]:
95 errors[
"base"] =
"api_error"
96 description_placeholders = {
"details": result[CONF_MESSAGE]}
113 vol.Required(CONF_URL): str,
114 vol.Required(CONF_API_KEY): str,
120 description_placeholders=description_placeholders,
125 user_input: dict[str, Any] |
None =
None,
126 ) -> ConfigFlowResult:
127 """Choose feeds to import."""
128 errors: dict[str, str] = {}
129 include_only_feeds: list = []
134 include_only_feeds = user_input[CONF_ONLY_INCLUDE_FEEDID]
138 CONF_URL: self.
urlurl,
139 CONF_API_KEY: self.
api_keyapi_key,
140 CONF_ONLY_INCLUDE_FEEDID: include_only_feeds,
144 step_id=
"choose_feeds",
145 data_schema=vol.Schema(
148 CONF_ONLY_INCLUDE_FEEDID,
149 default=include_only_feeds,
157 """Import config from yaml."""
158 url = import_info[CONF_URL]
159 api_key = import_info[CONF_API_KEY]
160 include_only_feeds =
None
161 if import_info.get(CONF_ONLY_INCLUDE_FEEDID)
is not None:
162 include_only_feeds =
list(map(str, import_info[CONF_ONLY_INCLUDE_FEEDID]))
164 CONF_API_KEY: api_key,
165 CONF_ONLY_INCLUDE_FEEDID: include_only_feeds,
170 if errors := result.get(
"errors"):
176 """Emoncms Options flow handler."""
178 def __init__(self, config_entry: ConfigEntry) ->
None:
179 """Initialize emoncms options flow."""
180 self.
_url_url = config_entry.data[CONF_URL]
181 self.
_api_key_api_key = config_entry.data[CONF_API_KEY]
184 self, user_input: dict[str, Any] |
None =
None
185 ) -> ConfigFlowResult:
186 """Manage the options."""
187 errors: dict[str, str] = {}
188 description_placeholders = {}
190 CONF_ONLY_INCLUDE_FEEDID,
193 options: list = include_only_feeds
194 emoncms_client = EmoncmsClient(
200 if not result[CONF_SUCCESS]:
201 errors[
"base"] =
"api_error"
202 description_placeholders = {
"details": result[CONF_MESSAGE]}
205 dropdown = {
"options": options,
"mode":
"dropdown",
"multiple":
True}
207 include_only_feeds = user_input[CONF_ONLY_INCLUDE_FEEDID]
210 CONF_ONLY_INCLUDE_FEEDID: include_only_feeds,
216 data_schema=vol.Schema(
219 CONF_ONLY_INCLUDE_FEEDID, default=include_only_feeds
224 description_placeholders=description_placeholders,
ConfigFlowResult async_step_choose_feeds(self, dict[str, Any]|None user_input=None)
ConfigFlowResult async_step_import(self, ConfigType import_info)
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
EmoncmsOptionsFlow async_get_options_flow(ConfigEntry config_entry)
ConfigFlowResult async_step_init(self, dict[str, Any]|None user_input=None)
None __init__(self, ConfigEntry config_entry)
None _abort_if_unique_id_configured(self, dict[str, Any]|None updates=None, bool reload_on_update=True, *str error="already_configured")
ConfigEntry|None async_set_unique_id(self, str|None unique_id=None, *bool raise_on_progress=True)
ConfigFlowResult async_create_entry(self, *str title, Mapping[str, Any] data, str|None description=None, Mapping[str, str]|None description_placeholders=None, Mapping[str, Any]|None options=None)
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
ConfigFlowResult async_abort(self, *str reason, Mapping[str, str]|None description_placeholders=None)
None _async_abort_entries_match(self, dict[str, Any]|None match_dict=None)
ConfigFlowResult async_show_form(self, *str|None step_id=None, vol.Schema|None data_schema=None, dict[str, str]|None errors=None, Mapping[str, str]|None description_placeholders=None, bool|None last_step=None, str|None preview=None)
ConfigEntry config_entry(self)
None config_entry(self, ConfigEntry value)
vol.Schema add_suggested_values_to_schema(self, vol.Schema data_schema, Mapping[str, Any]|None suggested_values)
_FlowResultT async_show_form(self, *str|None step_id=None, vol.Schema|None data_schema=None, dict[str, str]|None errors=None, Mapping[str, str]|None description_placeholders=None, bool|None last_step=None, str|None preview=None)
_FlowResultT async_create_entry(self, *str|None title=None, Mapping[str, Any] data, str|None description=None, Mapping[str, str]|None description_placeholders=None)
_FlowResultT async_abort(self, *str reason, Mapping[str, str]|None description_placeholders=None)
dict[str, Any] get_feed_list(EmoncmsClient emoncms_client)
list[dict[str, Any]] get_options(list[dict[str, Any]] feeds)
aiohttp.ClientSession async_get_clientsession(HomeAssistant hass, bool verify_ssl=True, socket.AddressFamily family=socket.AF_UNSPEC, ssl_util.SSLCipherList ssl_cipher=ssl_util.SSLCipherList.PYTHON_DEFAULT)
Selector selector(Any config)