56
56
SERVICE_CHECK_BATTERY_LAST_REPORTED ,
57
57
SERVICE_DATA_DAYS_LAST_REPORTED ,
58
58
SERVICE_CHECK_BATTERY_LAST_REPORTED_SCHEMA ,
59
+ SERVICE_CHECK_BATTERY_LOW ,
59
60
EVENT_BATTERY_NOT_REPORTED ,
61
+ EVENT_BATTERY_THRESHOLD ,
60
62
DATA_STORE ,
61
63
ATTR_REMOVE ,
62
64
ATTR_DEVICE_ID ,
67
69
ATTR_BATTERY_LAST_REPORTED ,
68
70
ATTR_BATTERY_LAST_REPORTED_DAYS ,
69
71
ATTR_BATTERY_LAST_REPORTED_LEVEL ,
72
+ ATTR_BATTERY_LEVEL ,
73
+ ATTR_PREVIOUS_BATTERY_LEVEL ,
74
+ ATTR_BATTERY_THRESHOLD_REMINDER ,
75
+ ATTR_BATTERY_LOW ,
70
76
CONF_BATTERY_TYPE ,
71
77
CONF_BATTERY_QUANTITY ,
72
78
MIN_HA_VERSION ,
@@ -267,7 +273,7 @@ async def async_update_options(hass: HomeAssistant, entry: ConfigEntry) -> None:
267
273
268
274
269
275
@callback
270
- def register_services (hass ):
276
+ def register_services (hass : HomeAssistant ):
271
277
"""Register services used by battery notes component."""
272
278
273
279
async def handle_battery_replaced (call ):
@@ -356,6 +362,33 @@ async def handle_battery_last_reported(call):
356
362
str (device .coordinator .last_reported ),
357
363
)
358
364
365
+ async def handle_battery_low (call ):
366
+ """Handle the service call."""
367
+
368
+ device : BatteryNotesDevice
369
+ for device in hass .data [DOMAIN ][DATA ].devices .values ():
370
+ if device .coordinator .battery_low is True :
371
+
372
+ hass .bus .async_fire (
373
+ EVENT_BATTERY_THRESHOLD ,
374
+ {
375
+ ATTR_DEVICE_ID : device .coordinator .device_id ,
376
+ ATTR_DEVICE_NAME : device .coordinator .device_name ,
377
+ ATTR_BATTERY_LOW : device .coordinator .battery_low ,
378
+ ATTR_BATTERY_TYPE_AND_QUANTITY : device .coordinator .battery_type_and_quantity ,
379
+ ATTR_BATTERY_TYPE : device .coordinator .battery_type ,
380
+ ATTR_BATTERY_QUANTITY : device .coordinator .battery_quantity ,
381
+ ATTR_BATTERY_LEVEL : device .coordinator .rounded_battery_level ,
382
+ ATTR_PREVIOUS_BATTERY_LEVEL : device .coordinator ._previous_battery_level ,
383
+ ATTR_BATTERY_THRESHOLD_REMINDER : True ,
384
+ },
385
+ )
386
+
387
+ _LOGGER .debug (
388
+ "Raised event device %s battery low" ,
389
+ device .coordinator .device_id ,
390
+ )
391
+
359
392
hass .services .async_register (
360
393
DOMAIN ,
361
394
SERVICE_BATTERY_REPLACED ,
@@ -369,3 +402,9 @@ async def handle_battery_last_reported(call):
369
402
handle_battery_last_reported ,
370
403
schema = SERVICE_CHECK_BATTERY_LAST_REPORTED_SCHEMA ,
371
404
)
405
+
406
+ hass .services .async_register (
407
+ DOMAIN ,
408
+ SERVICE_CHECK_BATTERY_LOW ,
409
+ handle_battery_low ,
410
+ )
0 commit comments