From ff14d49e404f8c6189d86fc2415afc006d3ce596 Mon Sep 17 00:00:00 2001 From: Phil Bruckner Date: Fri, 8 Dec 2023 10:28:09 -0600 Subject: [PATCH] Simplify unique IDs for UI added additional sensors --- custom_components/sun2/__init__.py | 11 +++++++++++ custom_components/sun2/binary_sensor.py | 7 +++++-- custom_components/sun2/config_flow.py | 4 +--- custom_components/sun2/helpers.py | 6 +----- custom_components/sun2/sensor.py | 9 ++++++--- 5 files changed, 24 insertions(+), 13 deletions(-) diff --git a/custom_components/sun2/__init__.py b/custom_components/sun2/__init__.py index 03d8e2d..0cc60e7 100644 --- a/custom_components/sun2/__init__.py +++ b/custom_components/sun2/__init__.py @@ -3,6 +3,7 @@ import asyncio from collections.abc import Coroutine +import re from typing import Any, cast from astral import SunDirection @@ -17,6 +18,7 @@ Platform, ) from homeassistant.core import Event, HomeAssistant, ServiceCall +from homeassistant.helpers import entity_registry as er from homeassistant.helpers.dispatcher import dispatcher_send from homeassistant.helpers.reload import async_integration_yaml_config from homeassistant.helpers.service import async_register_admin_service @@ -26,6 +28,7 @@ from .helpers import LocData, LocParams, Sun2Data PLATFORMS = [Platform.BINARY_SENSOR, Platform.SENSOR] +_OLD_UNIQUE_ID = re.compile(r"[0-9a-f]{32}-([0-9a-f]{32})") async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: @@ -129,6 +132,14 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: if options != entry.options: hass.config_entries.async_update_entry(entry, options=options) + # From 3.0.0b9 or older: Convert unique_id from entry.entry_id-unique_id -> unique_id + ent_reg = er.async_get(hass) + for entity in ent_reg.entities.values(): + if entity.platform != DOMAIN: + continue + if m := _OLD_UNIQUE_ID.fullmatch(entity.unique_id): + ent_reg.async_update_entity(entity.entity_id, new_unique_id=m.group(1)) + entry.async_on_unload(entry.add_update_listener(entry_updated)) await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS) return True diff --git a/custom_components/sun2/binary_sensor.py b/custom_components/sun2/binary_sensor.py index 5710e79..7e3f014 100644 --- a/custom_components/sun2/binary_sensor.py +++ b/custom_components/sun2/binary_sensor.py @@ -13,7 +13,7 @@ BinarySensorEntity, BinarySensorEntityDescription, ) -from homeassistant.config_entries import ConfigEntry +from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry from homeassistant.const import ( CONF_ABOVE, CONF_BINARY_SENSORS, @@ -337,7 +337,10 @@ def _sensors( sensors: list[Entity] = [] for config in sensors_config: if isinstance(extra, Sun2EntityParams): - extra.unique_id = config[CONF_UNIQUE_ID] + unique_id = config[CONF_UNIQUE_ID] + if extra.entry.source == SOURCE_IMPORT: + unique_id = f"{extra.entry.entry_id}-{unique_id}" + extra.unique_id = unique_id threshold = config[CONF_ELEVATION] name = config.get(CONF_NAME) else: diff --git a/custom_components/sun2/config_flow.py b/custom_components/sun2/config_flow.py index 48cd588..7d57203 100644 --- a/custom_components/sun2/config_flow.py +++ b/custom_components/sun2/config_flow.py @@ -116,9 +116,7 @@ async def async_step_location( CONF_ELEVATION: self.hass.config.elevation, CONF_TIME_ZONE: self.hass.config.time_zone, } - data_schema = self.add_suggested_values_to_schema( - data_schema, suggested_values - ) + data_schema = self.add_suggested_values_to_schema(data_schema, suggested_values) return self.async_show_form( step_id="location", data_schema=data_schema, last_step=False ) diff --git a/custom_components/sun2/helpers.py b/custom_components/sun2/helpers.py index 540672f..5bda08c 100644 --- a/custom_components/sun2/helpers.py +++ b/custom_components/sun2/helpers.py @@ -197,11 +197,7 @@ def __init__( if sun2_entity_params: self._attr_has_entity_name = True self._attr_translation_key = self.entity_description.key - entry = sun2_entity_params.entry - unique_id = sun2_entity_params.unique_id - self._attr_unique_id = ( - f"{entry.entry_id}-{unique_id or self.entity_description.key}" - ) + self._attr_unique_id = sun2_entity_params.unique_id self._attr_device_info = sun2_entity_params.device_info else: self._attr_unique_id = cast(str, self.name) diff --git a/custom_components/sun2/sensor.py b/custom_components/sun2/sensor.py index 1ab421e..ba31a45 100644 --- a/custom_components/sun2/sensor.py +++ b/custom_components/sun2/sensor.py @@ -21,7 +21,7 @@ SensorEntityDescription, SensorStateClass, ) -from homeassistant.config_entries import ConfigEntry +from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry from homeassistant.const import ( ATTR_ICON, CONF_ENTITY_NAMESPACE, @@ -1230,7 +1230,7 @@ def _sensors( for config in sensors_config: if isinstance(config, str): if isinstance(extra, Sun2EntityParams): - extra.unique_id = None + extra.unique_id = f"{extra.entry.entry_id}-{config}" sensors.append( _SENSOR_TYPES[config].cls( loc_params, extra, config, _SENSOR_TYPES[config].icon @@ -1238,7 +1238,10 @@ def _sensors( ) else: if isinstance(extra, Sun2EntityParams): - extra.unique_id = config[CONF_UNIQUE_ID] + unique_id = config[CONF_UNIQUE_ID] + if extra.entry.source == SOURCE_IMPORT: + unique_id = f"{extra.entry.entry_id}-{unique_id}" + extra.unique_id = unique_id if CONF_ELEVATION_AT_TIME in config: # For config entries, JSON serialization turns a time into a string. # Convert back to time in that case.