Skip to content

Commit fd2a26b

Browse files
authored
Merge pull request #380 from Olen/fix/entity-auto-rename
fix: allow entity auto-rename when device is renamed
2 parents b92b966 + 85b8b60 commit fd2a26b

File tree

2 files changed

+60
-30
lines changed

2 files changed

+60
-30
lines changed

custom_components/plant/number.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
async_generate_entity_id,
2525
)
2626
from homeassistant.helpers.entity_platform import AddEntitiesCallback
27+
from homeassistant.helpers.entity_registry import async_get as er_async_get
2728
from homeassistant.helpers.event import async_track_state_change_event
2829
from homeassistant.util.unit_conversion import TemperatureConverter
2930

@@ -203,11 +204,16 @@ def __init__(
203204
self._config = config
204205
self.hass = hass
205206
self._plant = plantdevice
206-
self.entity_id = async_generate_entity_id(
207-
f"{DOMAIN}.{{}}",
208-
f"{self._plant.name} {self._entity_id_key}",
209-
current_ids={},
210-
)
207+
# Only force entity_id for existing entities (backwards compat).
208+
# New entities let has_entity_name derive the entity_id automatically,
209+
# which enables auto-rename when the device is renamed.
210+
ent_reg = er_async_get(hass)
211+
if ent_reg.async_get_entity_id("number", DOMAIN, self._attr_unique_id):
212+
self.entity_id = async_generate_entity_id(
213+
f"{DOMAIN}.{{}}",
214+
f"{self._plant.name} {self._entity_id_key}",
215+
current_ids={},
216+
)
211217
# pylint: disable=no-member
212218
if (
213219
not hasattr(self, "_attr_native_value")
@@ -221,6 +227,7 @@ def device_info(self) -> DeviceInfo:
221227
"""Device info for devices"""
222228
return DeviceInfo(
223229
identifiers={(DOMAIN, self._plant.unique_id)},
230+
name=self._plant.name,
224231
)
225232

226233
async def async_set_native_value(self, value: float) -> None:

custom_components/plant/sensor.py

Lines changed: 48 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@
4444
EVENT_ENTITY_REGISTRY_UPDATED,
4545
EventEntityRegistryUpdatedData,
4646
)
47+
from homeassistant.helpers.entity_registry import (
48+
async_get as er_async_get,
49+
)
4750
from homeassistant.helpers.event import (
4851
async_track_state_change_event,
4952
)
@@ -198,11 +201,15 @@ def __init__(
198201
self._plant = plantdevice
199202
self._tracker = []
200203
self._follow_external = True
201-
self.entity_id = async_generate_entity_id(
202-
f"{DOMAIN}.{{}}",
203-
f"{self._plant.name} {self._entity_id_key}",
204-
current_ids={},
205-
)
204+
# Only force entity_id for existing entities (backwards compat).
205+
# New entities let has_entity_name derive the entity_id automatically.
206+
ent_reg = er_async_get(hass)
207+
if ent_reg.async_get_entity_id(DOMAIN_SENSOR, DOMAIN, self._attr_unique_id):
208+
self.entity_id = async_generate_entity_id(
209+
f"{DOMAIN}.{{}}",
210+
f"{self._plant.name} {self._entity_id_key}",
211+
current_ids={},
212+
)
206213
if (
207214
not self._attr_native_value
208215
or self._attr_native_value == STATE_UNKNOWN
@@ -220,6 +227,7 @@ def device_info(self) -> DeviceInfo:
220227
"""Device info for devices"""
221228
return DeviceInfo(
222229
identifiers={(DOMAIN, self._plant.unique_id)},
230+
name=self._plant.name,
223231
)
224232

225233
@property
@@ -622,11 +630,13 @@ def __init__(
622630
self._source_is_ppfd = False # Track if source already provides PPFD
623631
super().__init__(hass, config, plantdevice)
624632
self._follow_unit = False
625-
self.entity_id = async_generate_entity_id(
626-
f"{DOMAIN_SENSOR}.{{}}",
627-
f"{self._plant.name} {self._entity_id_key}",
628-
current_ids={},
629-
)
633+
ent_reg = er_async_get(hass)
634+
if ent_reg.async_get_entity_id(DOMAIN_SENSOR, DOMAIN, self._attr_unique_id):
635+
self.entity_id = async_generate_entity_id(
636+
f"{DOMAIN_SENSOR}.{{}}",
637+
f"{self._plant.name} {self._entity_id_key}",
638+
current_ids={},
639+
)
630640

631641
def _is_ppfd_source(self) -> bool:
632642
"""Check if the external sensor provides PPFD directly (not lux).
@@ -765,17 +775,22 @@ def __init__(
765775
unit_time=UnitOfTime.SECONDS,
766776
max_sub_interval=None,
767777
)
768-
self.entity_id = async_generate_entity_id(
769-
f"{DOMAIN_SENSOR}.{{}}",
770-
f"{self._plant.name} Total {READING_PPFD} Integral",
771-
current_ids={},
772-
)
778+
ent_reg = er_async_get(hass)
779+
if ent_reg.async_get_entity_id(
780+
DOMAIN_SENSOR, DOMAIN, f"{config.entry_id}-ppfd-integral"
781+
):
782+
self.entity_id = async_generate_entity_id(
783+
f"{DOMAIN_SENSOR}.{{}}",
784+
f"{self._plant.name} Total {READING_PPFD} Integral",
785+
current_ids={},
786+
)
773787

774788
@property
775789
def device_info(self) -> DeviceInfo:
776790
"""Device info for devices"""
777791
return DeviceInfo(
778792
identifiers={(DOMAIN, self._plant.unique_id)},
793+
name=self._plant.name,
779794
)
780795

781796
def _calculate_unit(self, source_unit: str) -> str:
@@ -874,11 +889,13 @@ def __init__(
874889
suggested_entity_id=None,
875890
periodically_resetting=True,
876891
)
877-
self.entity_id = async_generate_entity_id(
878-
f"{DOMAIN_SENSOR}.{{}}",
879-
f"{self._plant.name} {READING_DLI}",
880-
current_ids={},
881-
)
892+
ent_reg = er_async_get(hass)
893+
if ent_reg.async_get_entity_id(DOMAIN_SENSOR, DOMAIN, f"{config.entry_id}-dli"):
894+
self.entity_id = async_generate_entity_id(
895+
f"{DOMAIN_SENSOR}.{{}}",
896+
f"{self._plant.name} {READING_DLI}",
897+
current_ids={},
898+
)
882899

883900
@property
884901
def native_unit_of_measurement(self) -> str:
@@ -896,6 +913,7 @@ def device_info(self) -> DeviceInfo:
896913
"""Device info for devices"""
897914
return DeviceInfo(
898915
identifiers={(DOMAIN, self._plant.unique_id)},
916+
name=self._plant.name,
899917
)
900918

901919
async def async_added_to_hass(self) -> None:
@@ -983,11 +1001,15 @@ def __init__(
9831001
precision=2,
9841002
percentile=50, # Not used for "change" characteristic
9851003
)
986-
self.entity_id = async_generate_entity_id(
987-
f"{DOMAIN_SENSOR}.{{}}",
988-
f"{self._plant.name} {READING_DLI}_24h",
989-
current_ids={},
990-
)
1004+
ent_reg = er_async_get(hass)
1005+
if ent_reg.async_get_entity_id(
1006+
DOMAIN_SENSOR, DOMAIN, f"{config.entry_id}-dli-24h"
1007+
):
1008+
self.entity_id = async_generate_entity_id(
1009+
f"{DOMAIN_SENSOR}.{{}}",
1010+
f"{self._plant.name} {READING_DLI}_24h",
1011+
current_ids={},
1012+
)
9911013

9921014
@property
9931015
def native_unit_of_measurement(self) -> str:
@@ -1002,6 +1024,7 @@ def device_info(self) -> DeviceInfo:
10021024
"""Device info for devices."""
10031025
return DeviceInfo(
10041026
identifiers={(DOMAIN, self._plant.unique_id)},
1027+
name=self._plant.name,
10051028
)
10061029

10071030

0 commit comments

Comments
 (0)