Skip to content

Commit 680345a

Browse files
Fix entity in 2026.2 (#4349)
1 parent 1ad6d93 commit 680345a

3 files changed

Lines changed: 40 additions & 110 deletions

File tree

custom_components/battery_notes/binary_sensor.py

Lines changed: 7 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
Event,
2626
HomeAssistant,
2727
callback,
28-
split_entity_id,
2928
)
3029
from homeassistant.exceptions import TemplateError
3130
from homeassistant.helpers import (
@@ -54,7 +53,10 @@
5453
TemplateStateFromEntityId,
5554
)
5655

57-
from .common import utcnow_no_timezone, validate_is_float
56+
from .common import (
57+
utcnow_no_timezone,
58+
validate_is_float,
59+
)
5860
from .const import (
5961
ATTR_BATTERY_LAST_REPLACED,
6062
ATTR_BATTERY_LOW_THRESHOLD,
@@ -288,23 +290,6 @@ def __init__(
288290
self._template_attrs: dict[Template, list[_TemplateAttribute]] = {}
289291
self._template_result_info: TrackTemplateResultInfo | None = None
290292

291-
if coordinator.source_entity_id and not coordinator.device_id:
292-
self._attr_translation_placeholders = {
293-
"device_name": coordinator.device_name + " "
294-
}
295-
self.entity_id = f"binary_sensor.{coordinator.device_name.lower()}_{entity_description.key}"
296-
elif coordinator.source_entity_id and coordinator.device_id:
297-
_, source_object_id = split_entity_id(coordinator.source_entity_id)
298-
self._attr_translation_placeholders = {
299-
"device_name": coordinator.source_entity_name + " "
300-
}
301-
self.entity_id = (
302-
f"binary_sensor.{source_object_id}_{entity_description.key}"
303-
)
304-
else:
305-
self._attr_translation_placeholders = {"device_name": ""}
306-
self.entity_id = f"binary_sensor.{coordinator.device_name.lower()}_{entity_description.key}"
307-
308293
self._template = battery_low_template
309294
self._state: bool | None = None
310295

@@ -476,23 +461,6 @@ def __init__(
476461
hass=hass, coordinator=coordinator, entity_description=entity_description
477462
)
478463

479-
if coordinator.source_entity_id and not coordinator.device_id:
480-
self._attr_translation_placeholders = {
481-
"device_name": coordinator.device_name + " "
482-
}
483-
self.entity_id = f"binary_sensor.{coordinator.device_name.lower()}_{entity_description.key}"
484-
elif coordinator.source_entity_id and coordinator.device_id:
485-
_, source_object_id = split_entity_id(coordinator.source_entity_id)
486-
self._attr_translation_placeholders = {
487-
"device_name": coordinator.source_entity_name + " "
488-
}
489-
self.entity_id = (
490-
f"binary_sensor.{source_object_id}_{entity_description.key}"
491-
)
492-
else:
493-
self._attr_translation_placeholders = {"device_name": ""}
494-
self.entity_id = f"binary_sensor.{coordinator.device_name.lower()}_{entity_description.key}"
495-
496464
self._attr_unique_id = unique_id
497465

498466
async def async_added_to_hass(self) -> None:
@@ -538,29 +506,13 @@ def __init__(
538506
unique_id: str,
539507
) -> None:
540508
"""Create a low battery binary sensor."""
509+
510+
self._attr_unique_id = unique_id
511+
541512
super().__init__(
542513
hass=hass, coordinator=coordinator, entity_description=entity_description
543514
)
544515

545-
if coordinator.source_entity_id and not coordinator.device_id:
546-
self._attr_translation_placeholders = {
547-
"device_name": coordinator.device_name + " "
548-
}
549-
self.entity_id = f"binary_sensor.{coordinator.device_name.lower()}_{entity_description.key}"
550-
elif coordinator.source_entity_id and coordinator.device_id:
551-
_, source_object_id = split_entity_id(coordinator.source_entity_id)
552-
self._attr_translation_placeholders = {
553-
"device_name": coordinator.source_entity_name + " "
554-
}
555-
self.entity_id = (
556-
f"binary_sensor.{source_object_id}_{entity_description.key}"
557-
)
558-
else:
559-
self._attr_translation_placeholders = {"device_name": ""}
560-
self.entity_id = f"binary_sensor.{coordinator.device_name.lower()}_{entity_description.key}"
561-
562-
self._attr_unique_id = unique_id
563-
564516
async def async_added_to_hass(self) -> None:
565517
"""Handle added to Hass."""
566518

@@ -620,23 +572,6 @@ def __init__(
620572
hass=hass, coordinator=coordinator, entity_description=entity_description
621573
)
622574

623-
if coordinator.source_entity_id and not coordinator.device_id:
624-
self._attr_translation_placeholders = {
625-
"device_name": coordinator.device_name + " "
626-
}
627-
self.entity_id = f"binary_sensor.{coordinator.device_name.lower()}_{entity_description.key}"
628-
elif coordinator.source_entity_id and coordinator.device_id:
629-
_, source_object_id = split_entity_id(coordinator.source_entity_id)
630-
self._attr_translation_placeholders = {
631-
"device_name": coordinator.source_entity_name + " "
632-
}
633-
self.entity_id = (
634-
f"binary_sensor.{source_object_id}_{entity_description.key}"
635-
)
636-
else:
637-
self._attr_translation_placeholders = {"device_name": ""}
638-
self.entity_id = f"binary_sensor.{coordinator.device_name.lower()}_{entity_description.key}"
639-
640575
self._attr_unique_id = unique_id
641576

642577
self._state: bool | None = None

custom_components/battery_notes/button.py

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from homeassistant.const import (
1313
CONF_DEVICE_ID,
1414
)
15-
from homeassistant.core import HomeAssistant, callback, split_entity_id
15+
from homeassistant.core import HomeAssistant, callback
1616
from homeassistant.helpers import (
1717
device_registry as dr,
1818
entity_registry as er,
@@ -131,25 +131,6 @@ def __init__(
131131
hass=hass, entity_description=entity_description, coordinator=coordinator
132132
)
133133

134-
if coordinator.source_entity_id and not coordinator.device_id:
135-
self._attr_translation_placeholders = {
136-
"device_name": coordinator.device_name + " "
137-
}
138-
self.entity_id = (
139-
f"button.{coordinator.device_name.lower()}_{entity_description.key}"
140-
)
141-
elif coordinator.source_entity_id and coordinator.device_id:
142-
_, source_object_id = split_entity_id(coordinator.source_entity_id)
143-
self._attr_translation_placeholders = {
144-
"device_name": coordinator.source_entity_name + " "
145-
}
146-
self.entity_id = f"button.{source_object_id}_{entity_description.key}"
147-
else:
148-
self._attr_translation_placeholders = {"device_name": ""}
149-
self.entity_id = (
150-
f"button.{coordinator.device_name.lower()}_{entity_description.key}"
151-
)
152-
153134
self._attr_unique_id = unique_id
154135
self._source_entity_id = coordinator.source_entity_id
155136

custom_components/battery_notes/entity.py

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@
22

33
from __future__ import annotations
44

5+
from collections.abc import Mapping
56
from dataclasses import dataclass
67

78
from homeassistant.core import HomeAssistant, split_entity_id
89
from homeassistant.helpers import device_registry as dr
910
from homeassistant.helpers.device import async_entity_id_to_device_id
1011
from homeassistant.helpers.entity import EntityDescription
1112
from homeassistant.helpers.update_coordinator import CoordinatorEntity
13+
from homeassistant.util import slugify
1214

1315
from .coordinator import BatteryNotesSubentryCoordinator
1416

@@ -50,28 +52,12 @@ def __init__(
5052
self._attr_has_entity_name = True
5153

5254
# Set up entity naming and translation placeholders
53-
self._set_entity_id(entity_description)
55+
self._attr_translation_placeholders = self._generate_translation_placeholders()
56+
self.entity_id = self._generate_entity_id()
5457

5558
# Set up device association
5659
self._associate_device(hass, device_registry)
5760

58-
def _set_entity_id(self, entity_description: BatteryNotesEntityDescription) -> None:
59-
"""Set up entity naming and translation placeholders."""
60-
if self.coordinator.source_entity_id and not self.coordinator.device_id:
61-
self._attr_translation_placeholders = {
62-
"device_name": self.coordinator.device_name + " "
63-
}
64-
self.entity_id = f"{entity_description.entity_type}.{self.coordinator.device_name.lower()}_{entity_description.key}"
65-
elif self.coordinator.source_entity_id and self.coordinator.device_id:
66-
_, source_object_id = split_entity_id(self.coordinator.source_entity_id)
67-
self._attr_translation_placeholders = {
68-
"device_name": self.coordinator.source_entity_name + " "
69-
}
70-
self.entity_id = f"{entity_description.entity_type}.{source_object_id}_{entity_description.key}"
71-
else:
72-
self._attr_translation_placeholders = {"device_name": ""}
73-
self.entity_id = f"{entity_description.entity_type}.{self.coordinator.device_name.lower()}_{entity_description.key}"
74-
7561
def _associate_device(
7662
self, hass: HomeAssistant, device_registry: dr.DeviceRegistry
7763
) -> None:
@@ -94,3 +80,31 @@ def _associate_device(
9480
else:
9581
# No device, leave hanging
9682
self.device_entry = None
83+
84+
def _generate_translation_placeholders(self) -> Mapping[str, str]:
85+
"""Generate translation placeholders."""
86+
if self.coordinator.source_entity_id and not self.coordinator.device_id:
87+
return {"device_name": self.coordinator.device_name + " "}
88+
elif self.coordinator.source_entity_id and self.coordinator.device_id:
89+
return {"device_name": self.coordinator.source_entity_name + " "}
90+
else:
91+
return {"device_name": ""}
92+
93+
def _generate_entity_id(
94+
self,
95+
) -> str:
96+
"""Generate a consistent entity ID."""
97+
entity_type = self.entity_description.entity_type
98+
entity_key = self.entity_description.key
99+
100+
if self.coordinator.source_entity_id and not self.coordinator.device_id:
101+
return f"{entity_type}.{slugify(self.coordinator.device_name.lower())}_{entity_key}".replace(
102+
"__", "_"
103+
)
104+
elif self.coordinator.source_entity_id and self.coordinator.device_id:
105+
_, source_object_id = split_entity_id(self.coordinator.source_entity_id)
106+
return f"{entity_type}.{source_object_id}_{entity_key}".replace("__", "_")
107+
else:
108+
return f"{entity_type}.{slugify(self.coordinator.device_name.lower())}_{entity_key}".replace(
109+
"__", "_"
110+
)

0 commit comments

Comments
 (0)