Skip to content

Commit 2c0eab6

Browse files
committed
Fix new devices on restart, if serial not yet available.
1 parent 99bc50d commit 2c0eab6

File tree

2 files changed

+17
-17
lines changed

2 files changed

+17
-17
lines changed

custom_components/kaco/__init__.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,16 @@ async def async_get_datas() -> Dict:
229229
values[MEAS_ENERGY_TODAY.valueKey] = float(cols[4])
230230
node[MEAS_ENERGY_TODAY.valueKey] = values[MEAS_ENERGY_TODAY.valueKey]
231231
values["extra"]["serialno"] = cols[1]
232+
serial = cols[1]
233+
values["extra"]["serialno"] = serial
234+
# In config_entry persistieren (nur wenn noch nicht gesetzt)
235+
for entry in hass.config_entries.async_entries(DOMAIN):
236+
if entry.data.get(CONF_KACO_URL) == ip:
237+
if entry.data.get("serialno") != serial:
238+
new_data = dict(entry.data)
239+
new_data["serialno"] = serial
240+
hass.config_entries.async_update_entry(entry, data=new_data)
241+
break
232242
node["serialno"] = values["extra"]["serialno"]
233243
values["extra"]["model"] = cols[0]
234244
values["extra"]["last_kWh_Update"] = now

custom_components/kaco/sensor.py

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626

2727
_LOGGER = logging.getLogger(__name__)
2828

29-
3029
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
3130
"""Run setup via YAML."""
3231
_LOGGER.debug("Config via YAML")
@@ -77,15 +76,13 @@ def __init__(
7776
self._value_key = sensor_obj.valueKey
7877
self._unit = sensor_obj.unit
7978
self._description = sensor_obj.description
80-
self._url: str = config.get(CONF_KACO_URL) or ""
79+
self._url: str = (config.get(CONF_KACO_URL) or "").strip()
80+
self._serial: str | None = config.get("serialno") or None
8181
self._name: str = config.get(CONF_NAME) or DEFAULT_NAME
8282
self._icon = DEFAULT_ICON
8383

84-
# Fallback-ID aus Host/IP (letztes Label) oder komplette URL
85-
try:
86-
self._id = (self._url.split(".")[-1] or self._url).strip()
87-
except Exception:
88-
self._id = (self._url or "unknown").strip()
84+
# unique_id must stay constant across restarts, even before serialno is known.
85+
self._id = self._url or "unknown"
8986

9087
_LOGGER.debug("KACO config:")
9188
_LOGGER.debug("\tname: %s", self._name)
@@ -96,15 +93,8 @@ def __init__(
9693

9794
@property
9895
def unique_id(self) -> str:
99-
"""Stable unique_id even if coordinator has no data yet."""
100-
serial = None
101-
try:
102-
if self.coordinator and self.coordinator.data:
103-
serial = self.coordinator.data.get("extra", {}).get("serialno")
104-
except Exception:
105-
serial = None
106-
base = serial or self._id or self._url or "unknown"
107-
return f"{DOMAIN}_{base}_{self._value_key}"
96+
"""Return a restart-stable unique_id for this sensor."""
97+
return f"{DOMAIN}_{self._id}_{self._value_key}"
10898

10999
@property
110100
def name(self) -> str:
@@ -121,7 +111,7 @@ def device_info(self):
121111
"""Device info without hard dependency on live data."""
122112
info = {
123113
"identifiers": {(DOMAIN, self._id)},
124-
"name": self.name,
114+
"name": self._name,
125115
"configuration_url": f"http://{self._url}" if self._url else None,
126116
"manufacturer": "Kaco",
127117
}

0 commit comments

Comments
 (0)