|
3 | 3 |
|
4 | 4 | import voluptuous as vol
|
5 | 5 | from aiohttp import ClientResponseError
|
| 6 | +from requests.exceptions import HTTPError, Timeout |
| 7 | + |
| 8 | +from custom_components.tibber_local import TibberLocalBridge |
6 | 9 | from homeassistant import config_entries
|
7 | 10 | from homeassistant.const import CONF_ID, CONF_HOST, CONF_NAME, CONF_SCAN_INTERVAL, CONF_PASSWORD, CONF_MODE
|
8 | 11 | from homeassistant.core import HomeAssistant, callback
|
9 | 12 | from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
10 |
| -from requests.exceptions import HTTPError, Timeout |
11 |
| - |
12 |
| -from custom_components.tibber_local import TibberLocalBridge |
| 13 | +from homeassistant.util import slugify |
13 | 14 | from .const import (
|
14 | 15 | DOMAIN,
|
15 | 16 | DEFAULT_NAME,
|
|
29 | 30 | def tibber_local_entries(hass: HomeAssistant):
|
30 | 31 | conf_hosts = []
|
31 | 32 | for entry in hass.config_entries.async_entries(DOMAIN):
|
32 |
| - if hasattr(entry, 'options') and CONF_HOST in entry.options: |
33 |
| - conf_hosts.append(entry.options[CONF_HOST]) |
34 |
| - else: |
35 |
| - conf_hosts.append(entry.data[CONF_HOST]) |
| 33 | + a_host = entry.data[CONF_HOST] |
| 34 | + a_node = entry.data[CONF_NODE_NUMBER] |
| 35 | + |
| 36 | + if hasattr(entry, 'options'): |
| 37 | + if CONF_HOST in entry.options: |
| 38 | + a_host = entry.options[CONF_HOST] |
| 39 | + if CONF_NODE_NUMBER in entry.options: |
| 40 | + a_node = entry.options[CONF_NODE_NUMBER] |
| 41 | + |
| 42 | + conf_hosts.append(f"{a_node}@@{a_host}") |
36 | 43 | return conf_hosts
|
37 | 44 |
|
38 | 45 |
|
39 | 46 | @staticmethod
|
40 |
| -def _host_in_configuration_exists(host: str, hass: HomeAssistant) -> bool: |
41 |
| - if host in tibber_local_entries(hass): |
| 47 | +def _host_in_configuration_exists(a_host: str, a_node, hass: HomeAssistant) -> bool: |
| 48 | + if f"{a_node}@@{a_host}" in tibber_local_entries(hass): |
42 | 49 | return True
|
43 | 50 | return False
|
44 | 51 |
|
| 52 | +def _config_title_exists(a_title: str, hass: HomeAssistant) -> bool: |
| 53 | + return slugify(a_title) in [slugify(a_entry.title) for a_entry in hass.config_entries.async_entries(DOMAIN)] |
45 | 54 |
|
46 | 55 | class TibberLocalConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
47 | 56 | VERSION = 1
|
@@ -113,8 +122,11 @@ async def async_step_user(self, user_input=None):
|
113 | 122 | scan = user_input.get(CONF_SCAN_INTERVAL, DEFAULT_SCAN_INTERVAL)
|
114 | 123 | node_num = user_input.get(CONF_NODE_NUMBER, DEFAULT_NODE_NUMBER)
|
115 | 124 |
|
116 |
| - if _host_in_configuration_exists(host, self.hass): |
| 125 | + if _config_title_exists(name, self.hass): |
| 126 | + self._errors[CONF_NAME] = "already_configured" |
| 127 | + elif _host_in_configuration_exists(host, node_num, self.hass): |
117 | 128 | self._errors[CONF_HOST] = "already_configured"
|
| 129 | + self._errors[CONF_NODE_NUMBER] = "already_configured" |
118 | 130 | else:
|
119 | 131 | if await self._test_connection_tibber_local(host, pwd, node_num):
|
120 | 132 |
|
@@ -195,18 +207,19 @@ async def async_step_user(self, user_input=None):
|
195 | 207 | host_entry = host_entry.replace("http://", "")
|
196 | 208 | if host_entry.startswith('https://'):
|
197 | 209 | host_entry = host_entry.replace("https://", "")
|
198 |
| - |
199 | 210 | user_input[CONF_HOST] = host_entry
|
200 | 211 |
|
| 212 | + node_num = user_input[CONF_NODE_NUMBER] |
201 | 213 | self.options.update(user_input)
|
202 |
| - if self.data.get(CONF_HOST) != self.options.get(CONF_HOST): |
| 214 | + if self.data.get(CONF_HOST) != self.options.get(CONF_HOST) or self.data.get(CONF_NODE_NUMBER) != self.options.get(CONF_NODE_NUMBER): |
203 | 215 | # ok looks like the host has been changed... we need to do some things...
|
204 |
| - if _host_in_configuration_exists(host_entry, self.hass): |
| 216 | + if _host_in_configuration_exists(host_entry, node_num, self.hass): |
205 | 217 | self._errors[CONF_HOST] = "already_configured"
|
| 218 | + self._errors[CONF_NODE_NUMBER] = "already_configured" |
206 | 219 | else:
|
207 | 220 | return self._update_options()
|
208 | 221 | else:
|
209 |
| - # host did not change... |
| 222 | + # host/node-number did not change... |
210 | 223 | return self._update_options()
|
211 | 224 |
|
212 | 225 | return self.async_show_form(
|
|
0 commit comments