1
1
"""Binary Sensor platform for battery_notes."""
2
+
2
3
from __future__ import annotations
3
4
4
5
from collections .abc import Callable
@@ -88,6 +89,7 @@ class BatteryNotesBinarySensorEntityDescription(
88
89
89
90
unique_id_suffix : str
90
91
92
+
91
93
PLATFORM_SCHEMA = PLATFORM_SCHEMA .extend (
92
94
{
93
95
vol .Optional (CONF_NAME ): cv .string ,
@@ -96,6 +98,7 @@ class BatteryNotesBinarySensorEntityDescription(
96
98
}
97
99
)
98
100
101
+
99
102
@callback
100
103
def async_add_to_device (hass : HomeAssistant , entry : ConfigEntry ) -> str | None :
101
104
"""Add our config entry to the device."""
@@ -105,10 +108,13 @@ def async_add_to_device(hass: HomeAssistant, entry: ConfigEntry) -> str | None:
105
108
106
109
if device_id :
107
110
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
+ )
109
114
return device_id
110
115
return None
111
116
117
+
112
118
async def async_setup_entry (
113
119
hass : HomeAssistant ,
114
120
config_entry : ConfigEntry ,
@@ -148,7 +154,9 @@ async def async_registry_updated(event: Event) -> None:
148
154
device_id , remove_config_entry_id = config_entry .entry_id
149
155
)
150
156
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
+ )
152
160
153
161
config_entry .async_on_unload (
154
162
async_track_entity_registry_updated_event (
@@ -208,6 +216,7 @@ async def async_setup_platform(
208
216
209
217
await async_setup_reload_service (hass , DOMAIN , PLATFORMS )
210
218
219
+
211
220
class _TemplateAttribute :
212
221
"""Attribute value linked to template result."""
213
222
@@ -302,7 +311,10 @@ def handle_result(
302
311
self .on_update (validated )
303
312
return
304
313
305
- class BatteryNotesBatteryLowTemplateSensor (BinarySensorEntity , CoordinatorEntity [BatteryNotesCoordinator ]):
314
+
315
+ class BatteryNotesBatteryLowTemplateSensor (
316
+ BinarySensorEntity , CoordinatorEntity [BatteryNotesCoordinator ]
317
+ ):
306
318
"""Represents a low battery threshold binary sensor."""
307
319
308
320
_attr_should_poll = False
@@ -337,15 +349,25 @@ def __init__(
337
349
self ._attr_has_entity_name = True
338
350
339
351
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
+ )
342
358
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
+ }
345
365
self .entity_id = f"binary_sensor.{ source_object_id } _{ description .key } "
346
366
else :
347
367
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
+ )
349
371
350
372
self ._template = template
351
373
self ._state : bool | None = None
@@ -397,7 +419,9 @@ def add_template_attribute(
397
419
@callback
398
420
def _async_setup_templates (self ) -> None :
399
421
"""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
+ )
401
425
402
426
@callback
403
427
def _async_template_startup (
@@ -474,7 +498,6 @@ def _handle_results(
474
498
self .async_write_ha_state ()
475
499
return
476
500
477
-
478
501
@callback
479
502
def _update_state (self , result ):
480
503
@@ -489,18 +512,25 @@ def _update_state(self, result):
489
512
490
513
self ._state = state
491
514
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
+ )
494
520
495
521
@property
496
522
def is_on (self ) -> bool | None :
497
523
"""Return true if sensor is on."""
498
524
return self ._state
499
525
500
- class BatteryNotesBatteryLowSensor (BinarySensorEntity , CoordinatorEntity [BatteryNotesCoordinator ]):
526
+
527
+ class BatteryNotesBatteryLowSensor (
528
+ BinarySensorEntity , CoordinatorEntity [BatteryNotesCoordinator ]
529
+ ):
501
530
"""Represents a low battery threshold binary sensor."""
502
531
503
532
_attr_should_poll = False
533
+ _unrecorded_attributes = frozenset ({ATTR_BATTERY_LOW_THRESHOLD })
504
534
505
535
def __init__ (
506
536
self ,
@@ -517,15 +547,25 @@ def __init__(
517
547
self ._attr_has_entity_name = True
518
548
519
549
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
+ )
522
556
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
+ }
525
563
self .entity_id = f"binary_sensor.{ source_object_id } _{ description .key } "
526
564
else :
527
565
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
+ )
529
569
530
570
self .entity_description = description
531
571
self ._attr_unique_id = unique_id
@@ -574,7 +614,11 @@ def _handle_coordinator_update(self) -> None:
574
614
575
615
self .async_write_ha_state ()
576
616
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
+ )
578
622
579
623
@property
580
624
def extra_state_attributes (self ) -> dict [str , str ] | None :
0 commit comments