Skip to content

Commit 1b4735b

Browse files
Stop recording attributes (#1729)
1 parent 46cb5e3 commit 1b4735b

File tree

2 files changed

+127
-44
lines changed

2 files changed

+127
-44
lines changed

custom_components/battery_notes/binary_sensor.py

Lines changed: 63 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
"""Binary Sensor platform for battery_notes."""
2+
23
from __future__ import annotations
34

45
from collections.abc import Callable
@@ -88,6 +89,7 @@ class BatteryNotesBinarySensorEntityDescription(
8889

8990
unique_id_suffix: str
9091

92+
9193
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
9294
{
9395
vol.Optional(CONF_NAME): cv.string,
@@ -96,6 +98,7 @@ class BatteryNotesBinarySensorEntityDescription(
9698
}
9799
)
98100

101+
99102
@callback
100103
def async_add_to_device(hass: HomeAssistant, entry: ConfigEntry) -> str | None:
101104
"""Add our config entry to the device."""
@@ -105,10 +108,13 @@ def async_add_to_device(hass: HomeAssistant, entry: ConfigEntry) -> str | None:
105108

106109
if device_id:
107110
if device_registry.async_get(device_id):
108-
device_registry.async_update_device(device_id, add_config_entry_id=entry.entry_id)
111+
device_registry.async_update_device(
112+
device_id, add_config_entry_id=entry.entry_id
113+
)
109114
return device_id
110115
return None
111116

117+
112118
async def async_setup_entry(
113119
hass: HomeAssistant,
114120
config_entry: ConfigEntry,
@@ -148,7 +154,9 @@ async def async_registry_updated(event: Event) -> None:
148154
device_id, remove_config_entry_id=config_entry.entry_id
149155
)
150156

151-
coordinator: BatteryNotesCoordinator = hass.data[DOMAIN][DATA].devices[config_entry.entry_id].coordinator
157+
coordinator: BatteryNotesCoordinator = (
158+
hass.data[DOMAIN][DATA].devices[config_entry.entry_id].coordinator
159+
)
152160

153161
config_entry.async_on_unload(
154162
async_track_entity_registry_updated_event(
@@ -208,6 +216,7 @@ async def async_setup_platform(
208216

209217
await async_setup_reload_service(hass, DOMAIN, PLATFORMS)
210218

219+
211220
class _TemplateAttribute:
212221
"""Attribute value linked to template result."""
213222

@@ -302,7 +311,10 @@ def handle_result(
302311
self.on_update(validated)
303312
return
304313

305-
class BatteryNotesBatteryLowTemplateSensor(BinarySensorEntity, CoordinatorEntity[BatteryNotesCoordinator]):
314+
315+
class BatteryNotesBatteryLowTemplateSensor(
316+
BinarySensorEntity, CoordinatorEntity[BatteryNotesCoordinator]
317+
):
306318
"""Represents a low battery threshold binary sensor."""
307319

308320
_attr_should_poll = False
@@ -337,15 +349,25 @@ def __init__(
337349
self._attr_has_entity_name = True
338350

339351
if coordinator.source_entity_id and not coordinator.device_id:
340-
self._attr_translation_placeholders = {"device_name": coordinator.device_name + " "}
341-
self.entity_id = f"binary_sensor.{coordinator.device_name.lower()}_{description.key}"
352+
self._attr_translation_placeholders = {
353+
"device_name": coordinator.device_name + " "
354+
}
355+
self.entity_id = (
356+
f"binary_sensor.{coordinator.device_name.lower()}_{description.key}"
357+
)
342358
elif coordinator.source_entity_id and coordinator.device_id:
343-
source_entity_domain, source_object_id = split_entity_id(coordinator.source_entity_id)
344-
self._attr_translation_placeholders = {"device_name": coordinator.source_entity_name + " "}
359+
source_entity_domain, source_object_id = split_entity_id(
360+
coordinator.source_entity_id
361+
)
362+
self._attr_translation_placeholders = {
363+
"device_name": coordinator.source_entity_name + " "
364+
}
345365
self.entity_id = f"binary_sensor.{source_object_id}_{description.key}"
346366
else:
347367
self._attr_translation_placeholders = {"device_name": ""}
348-
self.entity_id = f"binary_sensor.{coordinator.device_name.lower()}_{description.key}"
368+
self.entity_id = (
369+
f"binary_sensor.{coordinator.device_name.lower()}_{description.key}"
370+
)
349371

350372
self._template = template
351373
self._state: bool | None = None
@@ -397,7 +419,9 @@ def add_template_attribute(
397419
@callback
398420
def _async_setup_templates(self) -> None:
399421
"""Set up templates."""
400-
self.add_template_attribute("_state", Template(self._template), None, self._update_state)
422+
self.add_template_attribute(
423+
"_state", Template(self._template), None, self._update_state
424+
)
401425

402426
@callback
403427
def _async_template_startup(
@@ -474,7 +498,6 @@ def _handle_results(
474498
self.async_write_ha_state()
475499
return
476500

477-
478501
@callback
479502
def _update_state(self, result):
480503

@@ -489,18 +512,25 @@ def _update_state(self, result):
489512

490513
self._state = state
491514
self.coordinator.battery_low_template_state = state
492-
_LOGGER.debug("%s binary sensor battery_low set to: %s via template", self.entity_id, state)
493-
515+
_LOGGER.debug(
516+
"%s binary sensor battery_low set to: %s via template",
517+
self.entity_id,
518+
state,
519+
)
494520

495521
@property
496522
def is_on(self) -> bool | None:
497523
"""Return true if sensor is on."""
498524
return self._state
499525

500-
class BatteryNotesBatteryLowSensor(BinarySensorEntity, CoordinatorEntity[BatteryNotesCoordinator]):
526+
527+
class BatteryNotesBatteryLowSensor(
528+
BinarySensorEntity, CoordinatorEntity[BatteryNotesCoordinator]
529+
):
501530
"""Represents a low battery threshold binary sensor."""
502531

503532
_attr_should_poll = False
533+
_unrecorded_attributes = frozenset({ATTR_BATTERY_LOW_THRESHOLD})
504534

505535
def __init__(
506536
self,
@@ -517,15 +547,25 @@ def __init__(
517547
self._attr_has_entity_name = True
518548

519549
if coordinator.source_entity_id and not coordinator.device_id:
520-
self._attr_translation_placeholders = {"device_name": coordinator.device_name + " "}
521-
self.entity_id = f"binary_sensor.{coordinator.device_name.lower()}_{description.key}"
550+
self._attr_translation_placeholders = {
551+
"device_name": coordinator.device_name + " "
552+
}
553+
self.entity_id = (
554+
f"binary_sensor.{coordinator.device_name.lower()}_{description.key}"
555+
)
522556
elif coordinator.source_entity_id and coordinator.device_id:
523-
source_entity_domain, source_object_id = split_entity_id(coordinator.source_entity_id)
524-
self._attr_translation_placeholders = {"device_name": coordinator.source_entity_name + " "}
557+
source_entity_domain, source_object_id = split_entity_id(
558+
coordinator.source_entity_id
559+
)
560+
self._attr_translation_placeholders = {
561+
"device_name": coordinator.source_entity_name + " "
562+
}
525563
self.entity_id = f"binary_sensor.{source_object_id}_{description.key}"
526564
else:
527565
self._attr_translation_placeholders = {"device_name": ""}
528-
self.entity_id = f"binary_sensor.{coordinator.device_name.lower()}_{description.key}"
566+
self.entity_id = (
567+
f"binary_sensor.{coordinator.device_name.lower()}_{description.key}"
568+
)
529569

530570
self.entity_description = description
531571
self._attr_unique_id = unique_id
@@ -574,7 +614,11 @@ def _handle_coordinator_update(self) -> None:
574614

575615
self.async_write_ha_state()
576616

577-
_LOGGER.debug("%s binary sensor battery_low set to: %s", self.coordinator.wrapped_battery.entity_id, self.coordinator.battery_low)
617+
_LOGGER.debug(
618+
"%s binary sensor battery_low set to: %s",
619+
self.coordinator.wrapped_battery.entity_id,
620+
self.coordinator.battery_low,
621+
)
578622

579623
@property
580624
def extra_state_attributes(self) -> dict[str, str] | None:

custom_components/battery_notes/sensor.py

Lines changed: 64 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,21 @@ class BatteryNotesBatteryPlusSensor(
265265

266266
_attr_should_poll = False
267267
_wrapped_attributes = None
268+
_unrecorded_attributes = frozenset(
269+
{
270+
ATTR_BATTERY_QUANTITY,
271+
ATTR_BATTERY_TYPE,
272+
ATTR_BATTERY_TYPE_AND_QUANTITY,
273+
ATTR_BATTERY_LOW,
274+
ATTR_BATTERY_LOW_THRESHOLD,
275+
ATTR_BATTERY_LAST_REPORTED,
276+
ATTR_BATTERY_LAST_REPORTED_LEVEL,
277+
ATTR_BATTERY_LAST_REPLACED,
278+
ATTR_DEVICE_ID,
279+
ATTR_SOURCE_ENTITY_ID,
280+
ATTR_DEVICE_NAME,
281+
}
282+
)
268283

269284
def __init__(
270285
self,
@@ -288,20 +303,30 @@ def __init__(
288303
self._attr_has_entity_name = True
289304

290305
if coordinator.source_entity_id and not coordinator.device_id:
291-
self._attr_translation_placeholders = {"device_name": coordinator.device_name + " "}
292-
self.entity_id = f"sensor.{coordinator.device_name.lower()}_{description.key}"
306+
self._attr_translation_placeholders = {
307+
"device_name": coordinator.device_name + " "
308+
}
309+
self.entity_id = (
310+
f"sensor.{coordinator.device_name.lower()}_{description.key}"
311+
)
293312
elif coordinator.source_entity_id and coordinator.device_id:
294-
source_entity_domain, source_object_id = split_entity_id(coordinator.source_entity_id)
295-
self._attr_translation_placeholders = {"device_name": coordinator.source_entity_name + " "}
313+
source_entity_domain, source_object_id = split_entity_id(
314+
coordinator.source_entity_id
315+
)
316+
self._attr_translation_placeholders = {
317+
"device_name": coordinator.source_entity_name + " "
318+
}
296319
self.entity_id = f"sensor.{source_object_id}_{description.key}"
297320
else:
298321
self._attr_translation_placeholders = {"device_name": ""}
299-
self.entity_id = f"sensor.{coordinator.device_name.lower()}_{description.key}"
322+
self.entity_id = (
323+
f"sensor.{coordinator.device_name.lower()}_{description.key}"
324+
)
300325

301326
_LOGGER.debug(
302327
"Setting up %s with wrapped battery %s",
303328
self.entity_id,
304-
self.coordinator.wrapped_battery.entity_id
329+
self.coordinator.wrapped_battery.entity_id,
305330
)
306331

307332
self.entity_description = description
@@ -548,6 +573,7 @@ class BatteryNotesTypeSensor(RestoreSensor, SensorEntity):
548573

549574
_attr_should_poll = False
550575
entity_description: BatteryNotesSensorEntityDescription
576+
_unrecorded_attributes = frozenset({ATTR_BATTERY_QUANTITY, ATTR_BATTERY_TYPE})
551577

552578
def __init__(
553579
self,
@@ -568,15 +594,25 @@ def __init__(
568594
self._attr_has_entity_name = True
569595

570596
if coordinator.source_entity_id and not coordinator.device_id:
571-
self._attr_translation_placeholders = {"device_name": coordinator.device_name + " "}
572-
self.entity_id = f"sensor.{coordinator.device_name.lower()}_{description.key}"
597+
self._attr_translation_placeholders = {
598+
"device_name": coordinator.device_name + " "
599+
}
600+
self.entity_id = (
601+
f"sensor.{coordinator.device_name.lower()}_{description.key}"
602+
)
573603
elif coordinator.source_entity_id and coordinator.device_id:
574-
source_entity_domain, source_object_id = split_entity_id(coordinator.source_entity_id)
575-
self._attr_translation_placeholders = {"device_name": coordinator.source_entity_name + " "}
604+
source_entity_domain, source_object_id = split_entity_id(
605+
coordinator.source_entity_id
606+
)
607+
self._attr_translation_placeholders = {
608+
"device_name": coordinator.source_entity_name + " "
609+
}
576610
self.entity_id = f"sensor.{source_object_id}_{description.key}"
577611
else:
578612
self._attr_translation_placeholders = {"device_name": ""}
579-
self.entity_id = f"sensor.{coordinator.device_name.lower()}_{description.key}"
613+
self.entity_id = (
614+
f"sensor.{coordinator.device_name.lower()}_{description.key}"
615+
)
580616

581617
self.entity_description = description
582618
self._attr_unique_id = unique_id
@@ -657,15 +693,25 @@ def __init__(
657693
self._attr_has_entity_name = True
658694

659695
if coordinator.source_entity_id and not coordinator.device_id:
660-
self._attr_translation_placeholders = {"device_name": coordinator.device_name + " "}
661-
self.entity_id = f"sensor.{coordinator.device_name.lower()}_{description.key}"
696+
self._attr_translation_placeholders = {
697+
"device_name": coordinator.device_name + " "
698+
}
699+
self.entity_id = (
700+
f"sensor.{coordinator.device_name.lower()}_{description.key}"
701+
)
662702
elif coordinator.source_entity_id and coordinator.device_id:
663-
source_entity_domain, source_object_id = split_entity_id(coordinator.source_entity_id)
664-
self._attr_translation_placeholders = {"device_name": coordinator.source_entity_name + " "}
703+
source_entity_domain, source_object_id = split_entity_id(
704+
coordinator.source_entity_id
705+
)
706+
self._attr_translation_placeholders = {
707+
"device_name": coordinator.source_entity_name + " "
708+
}
665709
self.entity_id = f"sensor.{source_object_id}_{description.key}"
666710
else:
667711
self._attr_translation_placeholders = {"device_name": ""}
668-
self.entity_id = f"sensor.{coordinator.device_name.lower()}_{description.key}"
712+
self.entity_id = (
713+
f"sensor.{coordinator.device_name.lower()}_{description.key}"
714+
)
669715

670716
self._attr_device_class = description.device_class
671717
self._attr_unique_id = unique_id
@@ -674,7 +720,6 @@ def __init__(
674720
self.entity_description = description
675721
self._native_value = None
676722

677-
678723
self._set_native_value(log_on_error=False)
679724

680725
device_registry = dr.async_get(hass)
@@ -699,10 +744,7 @@ def _set_native_value(self, log_on_error=True):
699744
entry = self.coordinator.store.async_get_device(self._device_id)
700745

701746
if entry:
702-
if (
703-
LAST_REPLACED in entry
704-
and entry[LAST_REPLACED] is not None
705-
):
747+
if LAST_REPLACED in entry and entry[LAST_REPLACED] is not None:
706748
last_replaced_date = datetime.fromisoformat(
707749
str(entry[LAST_REPLACED]) + "+00:00"
708750
)
@@ -721,10 +763,7 @@ def _handle_coordinator_update(self) -> None:
721763
entry = self.coordinator.store.async_get_device(self._device_id)
722764

723765
if entry:
724-
if (
725-
LAST_REPLACED in entry
726-
and entry[LAST_REPLACED] is not None
727-
):
766+
if LAST_REPLACED in entry and entry[LAST_REPLACED] is not None:
728767
last_replaced_date = datetime.fromisoformat(
729768
str(entry[LAST_REPLACED]) + "+00:00"
730769
)

0 commit comments

Comments
 (0)