Skip to content

Commit ebe9d9c

Browse files
Add area_name attribute to battery notes services and coordinator (#4924)
1 parent 67c3c5f commit ebe9d9c

3 files changed

Lines changed: 30 additions & 0 deletions

File tree

custom_components/battery_notes/const.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@
7676
EVENT_BATTERY_NOT_REPORTED = "battery_notes_battery_not_reported"
7777
EVENT_BATTERY_REPLACED = "battery_notes_battery_replaced"
7878

79+
ATTR_AREA_NAME = "area_name"
7980
ATTR_DEVICE_ID = "device_id"
8081
ATTR_SOURCE_ENTITY_ID = "source_entity_id"
8182
ATTR_REMOVE = "remove"

custom_components/battery_notes/coordinator.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
)
2222
from homeassistant.core import CALLBACK_TYPE, HomeAssistant, callback
2323
from homeassistant.helpers import (
24+
area_registry as ar,
2425
device_registry as dr,
2526
entity_registry as er,
2627
issue_registry as ir,
@@ -431,6 +432,27 @@ def source_entity_name(self):
431432

432433
return self._source_entity_name
433434

435+
@property
436+
def area_name(self):
437+
"""Get the area name of the source_entity_id or device_id."""
438+
if self.source_entity_id:
439+
entity_registry = er.async_get(self.hass)
440+
registry_entry = entity_registry.async_get(self.source_entity_id)
441+
if registry_entry and registry_entry.area_id:
442+
area_registry = ar.async_get(self.hass)
443+
area_entry = area_registry.async_get_area(registry_entry.area_id)
444+
if area_entry:
445+
return area_entry.name
446+
elif self.device_id:
447+
device_registry = dr.async_get(self.hass)
448+
device_entry = device_registry.async_get(self.device_id)
449+
if device_entry and device_entry.area_id:
450+
area_registry = ar.async_get(self.hass)
451+
area_entry = area_registry.async_get_area(device_entry.area_id)
452+
if area_entry:
453+
return area_entry.name
454+
return None
455+
434456
@property
435457
def battery_low_template_state(self):
436458
"""Get the current battery low status from a templated device."""

custom_components/battery_notes/services.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from homeassistant.util import dt as dt_util
1616

1717
from .const import (
18+
ATTR_AREA_NAME,
1819
ATTR_BATTERY_LAST_REPLACED,
1920
ATTR_BATTERY_LAST_REPLACED_DAYS,
2021
ATTR_BATTERY_LAST_REPORTED,
@@ -272,6 +273,7 @@ async def _async_battery_last_replaced(call: ServiceCall) -> ServiceResponse:
272273
ATTR_SOURCE_ENTITY_ID: coordinator.source_entity_id
273274
or "",
274275
ATTR_DEVICE_NAME: coordinator.device_name,
276+
ATTR_AREA_NAME: coordinator.area_name,
275277
ATTR_BATTERY_TYPE_AND_QUANTITY: coordinator.battery_type_and_quantity,
276278
ATTR_BATTERY_TYPE: coordinator.battery_type,
277279
ATTR_BATTERY_QUANTITY: coordinator.battery_quantity,
@@ -292,6 +294,7 @@ async def _async_battery_last_replaced(call: ServiceCall) -> ServiceResponse:
292294
ATTR_DEVICE_ID: coordinator.device_id or "",
293295
ATTR_SOURCE_ENTITY_ID: coordinator.source_entity_id or "",
294296
ATTR_DEVICE_NAME: coordinator.device_name,
297+
ATTR_AREA_NAME: coordinator.area_name,
295298
ATTR_BATTERY_TYPE_AND_QUANTITY: coordinator.battery_type_and_quantity,
296299
ATTR_BATTERY_TYPE: coordinator.battery_type,
297300
ATTR_BATTERY_QUANTITY: coordinator.battery_quantity,
@@ -352,6 +355,7 @@ async def _async_battery_last_reported(call: ServiceCall) -> ServiceResponse:
352355
ATTR_SOURCE_ENTITY_ID: coordinator.source_entity_id
353356
or "",
354357
ATTR_DEVICE_NAME: coordinator.device_name,
358+
ATTR_AREA_NAME: coordinator.area_name,
355359
ATTR_BATTERY_TYPE_AND_QUANTITY: coordinator.battery_type_and_quantity,
356360
ATTR_BATTERY_TYPE: coordinator.battery_type,
357361
ATTR_BATTERY_QUANTITY: coordinator.battery_quantity,
@@ -372,6 +376,7 @@ async def _async_battery_last_reported(call: ServiceCall) -> ServiceResponse:
372376
ATTR_DEVICE_ID: coordinator.device_id or "",
373377
ATTR_SOURCE_ENTITY_ID: coordinator.source_entity_id or "",
374378
ATTR_DEVICE_NAME: coordinator.device_name,
379+
ATTR_AREA_NAME: coordinator.area_name,
375380
ATTR_BATTERY_TYPE_AND_QUANTITY: coordinator.battery_type_and_quantity,
376381
ATTR_BATTERY_TYPE: coordinator.battery_type,
377382
ATTR_BATTERY_QUANTITY: coordinator.battery_quantity,
@@ -411,6 +416,7 @@ async def _async_battery_low(call: ServiceCall) -> ServiceResponse:
411416
ATTR_DEVICE_ID: coordinator.device_id or "",
412417
ATTR_DEVICE_NAME: coordinator.device_name,
413418
ATTR_SOURCE_ENTITY_ID: coordinator.source_entity_id or "",
419+
ATTR_AREA_NAME: coordinator.area_name,
414420
ATTR_BATTERY_LOW: coordinator.battery_low,
415421
ATTR_BATTERY_LOW_THRESHOLD: coordinator.battery_low_threshold,
416422
ATTR_BATTERY_TYPE_AND_QUANTITY: coordinator.battery_type_and_quantity,
@@ -431,6 +437,7 @@ async def _async_battery_low(call: ServiceCall) -> ServiceResponse:
431437
ATTR_DEVICE_ID: coordinator.device_id or "",
432438
ATTR_DEVICE_NAME: coordinator.device_name,
433439
ATTR_SOURCE_ENTITY_ID: coordinator.source_entity_id or "",
440+
ATTR_AREA_NAME: coordinator.area_name,
434441
ATTR_BATTERY_LOW: coordinator.battery_low,
435442
ATTR_BATTERY_LOW_THRESHOLD: coordinator.battery_low_threshold,
436443
ATTR_BATTERY_TYPE_AND_QUANTITY: coordinator.battery_type_and_quantity,

0 commit comments

Comments
 (0)