2626
2727_LOGGER = logging .getLogger (__name__ )
2828
29-
3029async 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 ("\t name: %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