11"""Binary Sensor platform for battery_notes."""
2+
23from __future__ import annotations
34
45from collections .abc import Callable
@@ -88,6 +89,7 @@ class BatteryNotesBinarySensorEntityDescription(
8889
8990 unique_id_suffix : str
9091
92+
9193PLATFORM_SCHEMA = PLATFORM_SCHEMA .extend (
9294 {
9395 vol .Optional (CONF_NAME ): cv .string ,
@@ -96,6 +98,7 @@ class BatteryNotesBinarySensorEntityDescription(
9698 }
9799)
98100
101+
99102@callback
100103def 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+
112118async 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+
211220class _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 :
0 commit comments