Skip to content

Commit 322d8ac

Browse files
Merge pull request #14 from andrew-codechimp/friendly-names
Friendly names
2 parents fa64069 + d123f90 commit 322d8ac

File tree

3 files changed

+37
-2
lines changed

3 files changed

+37
-2
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ Label State Helpers for Home Assistant
1010

1111
You can create state, not state and numeric state helpers which provide a binary sensor that turns on if any entity with an assigned label matches the criteria you specify.
1212

13-
An `entities` attribute is available which lists all entities that match the criteria.
13+
An `entities` attribute is available which lists all entities that match the criteria.
14+
An `entity_names` attribute is available which lists either the device (entity) names or just the entity name if the entity is not part of a device, for all entities that match the criteria.
1415

1516
## Example use cases
1617

@@ -82,7 +83,7 @@ actions:
8283
data:
8384
message: >-
8485
Critical sensors are unavailable {{
85-
state_attr('binary_sensor.critical_sensor_unavailable', 'entities') |
86+
state_attr('binary_sensor.critical_sensor_unavailable', 'entity_names') |
8687
join(', ') }}
8788
mode: single
8889
```

custom_components/label_state/binary_sensor.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
STATE_UNKNOWN,
1212
)
1313
from homeassistant.core import Event, EventStateChangedData, HomeAssistant, callback
14+
from homeassistant.helpers import device_registry as dr
1415
from homeassistant.helpers import entity_registry as er
1516
from homeassistant.helpers.entity_platform import (
1617
AddConfigEntryEntitiesCallback,
@@ -22,6 +23,7 @@
2223

2324
from .const import (
2425
ATTR_ENTITIES,
26+
ATTR_ENTITY_NAMES,
2527
CONF_LABEL,
2628
CONF_STATE_LOWER_LIMIT,
2729
CONF_STATE_NOT,
@@ -264,6 +266,7 @@ def _calc_state(self) -> None:
264266

265267
state_is_on: bool | None = False
266268
entities_on: list[str] = []
269+
entity_names: list[str] = []
267270

268271
entity_registry = er.async_get(self.hass)
269272

@@ -281,6 +284,8 @@ def _calc_state(self) -> None:
281284
):
282285
state_is_on = True
283286
entities_on.append(entity_id)
287+
name = self._get_device_or_entity_name(entity_id)
288+
entity_names.append(name)
284289

285290
if self._state_type == StateTypes.NOT_STATE:
286291
for entity_id in self._state_dict.keys():
@@ -296,6 +301,8 @@ def _calc_state(self) -> None:
296301
):
297302
state_is_on = True
298303
entities_on.append(entity_id)
304+
name = self._get_device_or_entity_name(entity_id)
305+
entity_names.append(name)
299306

300307
if self._state_type == StateTypes.NUMERIC_STATE:
301308
for entity_id in self._state_dict.keys():
@@ -320,6 +327,9 @@ def _calc_state(self) -> None:
320327
):
321328
state_is_on = True
322329
entities_on.append(entity_id)
330+
name = self._get_device_or_entity_name(entity_id)
331+
entity_names.append(name)
332+
323333
LOGGER.debug(
324334
"State %s is below lower limit %s and above upper limit %s for %s",
325335
entity_state,
@@ -334,6 +344,9 @@ def _calc_state(self) -> None:
334344
):
335345
state_is_on = True
336346
entities_on.append(entity_id)
347+
name = self._get_device_or_entity_name(entity_id)
348+
entity_names.append(name)
349+
337350
LOGGER.debug(
338351
"State %s is below lower limit %s for %s",
339352
entity_state,
@@ -347,6 +360,9 @@ def _calc_state(self) -> None:
347360
):
348361
state_is_on = True
349362
entities_on.append(entity_id)
363+
name = self._get_device_or_entity_name(entity_id)
364+
entity_names.append(name)
365+
350366
LOGGER.debug(
351367
"State %s is above upper limit %s for %s",
352368
entity_state,
@@ -368,3 +384,20 @@ def _calc_state(self) -> None:
368384

369385
self._attr_is_on = state_is_on
370386
self._attr_extra_state_attributes[ATTR_ENTITIES] = entities_on
387+
self._attr_extra_state_attributes[ATTR_ENTITY_NAMES] = entity_names
388+
389+
def _get_device_or_entity_name(
390+
self,
391+
entity_id: str,
392+
) -> str:
393+
"""Get the device or entity name."""
394+
entity_registry = er.async_get(self.hass)
395+
entity_entry = entity_registry.async_get(entity_id)
396+
if not entity_entry:
397+
return entity_id
398+
if entity_entry.device_id:
399+
device_registry = dr.async_get(self.hass)
400+
device_entry = device_registry.async_get(device_id=entity_entry.device_id)
401+
if device_entry is not None:
402+
return f"{device_entry.name_by_user or device_entry.name} ({entity_entry.name or entity_entry.original_name or entity_id})"
403+
return entity_entry.name or entity_entry.original_name or entity_id

custom_components/label_state/const.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
CONF_STATE_UPPER_LIMIT = "state_upper_limit"
3232

3333
ATTR_ENTITIES = "entities"
34+
ATTR_ENTITY_NAMES = "entity_names"
3435

3536

3637
class StateTypes(StrEnum):

0 commit comments

Comments
 (0)