Skip to content

Commit d87fdff

Browse files
committed
support for multiple nodes at same bridge
1 parent 3d2767f commit d87fdff

File tree

2 files changed

+28
-15
lines changed

2 files changed

+28
-15
lines changed

custom_components/tibber_local/config_flow.py

+27-14
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@
33

44
import voluptuous as vol
55
from aiohttp import ClientResponseError
6+
from requests.exceptions import HTTPError, Timeout
7+
8+
from custom_components.tibber_local import TibberLocalBridge
69
from homeassistant import config_entries
710
from homeassistant.const import CONF_ID, CONF_HOST, CONF_NAME, CONF_SCAN_INTERVAL, CONF_PASSWORD, CONF_MODE
811
from homeassistant.core import HomeAssistant, callback
912
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
1314
from .const import (
1415
DOMAIN,
1516
DEFAULT_NAME,
@@ -29,19 +30,27 @@
2930
def tibber_local_entries(hass: HomeAssistant):
3031
conf_hosts = []
3132
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}")
3643
return conf_hosts
3744

3845

3946
@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):
4249
return True
4350
return False
4451

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)]
4554

4655
class TibberLocalConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
4756
VERSION = 1
@@ -113,8 +122,11 @@ async def async_step_user(self, user_input=None):
113122
scan = user_input.get(CONF_SCAN_INTERVAL, DEFAULT_SCAN_INTERVAL)
114123
node_num = user_input.get(CONF_NODE_NUMBER, DEFAULT_NODE_NUMBER)
115124

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):
117128
self._errors[CONF_HOST] = "already_configured"
129+
self._errors[CONF_NODE_NUMBER] = "already_configured"
118130
else:
119131
if await self._test_connection_tibber_local(host, pwd, node_num):
120132

@@ -195,18 +207,19 @@ async def async_step_user(self, user_input=None):
195207
host_entry = host_entry.replace("http://", "")
196208
if host_entry.startswith('https://'):
197209
host_entry = host_entry.replace("https://", "")
198-
199210
user_input[CONF_HOST] = host_entry
200211

212+
node_num = user_input[CONF_NODE_NUMBER]
201213
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):
203215
# 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):
205217
self._errors[CONF_HOST] = "already_configured"
218+
self._errors[CONF_NODE_NUMBER] = "already_configured"
206219
else:
207220
return self._update_options()
208221
else:
209-
# host did not change...
222+
# host/node-number did not change...
210223
return self._update_options()
211224

212225
return self.async_show_form(

custom_components/tibber_local/manifest.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@
1010
"iot_class": "local_polling",
1111
"issue_tracker": "https://github.com/marq24/ha-tibber-pulse-local/issues",
1212
"requirements": ["smllib==1.5"],
13-
"version": "2024.11.0"
13+
"version": "2025.3.0"
1414
}

0 commit comments

Comments
 (0)