Skip to content

Commit 639fbb6

Browse files
Battery low service (#1234)
* Add check_battery_low service * Community example * Typo * Remove @bind_hass * Add battery levels * Fix service icons * Change event data to reminder * Lint issues
1 parent 5291e72 commit 639fbb6

File tree

11 files changed

+83
-6
lines changed

11 files changed

+83
-6
lines changed

custom_components/battery_notes/__init__.py

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,9 @@
5656
SERVICE_CHECK_BATTERY_LAST_REPORTED,
5757
SERVICE_DATA_DAYS_LAST_REPORTED,
5858
SERVICE_CHECK_BATTERY_LAST_REPORTED_SCHEMA,
59+
SERVICE_CHECK_BATTERY_LOW,
5960
EVENT_BATTERY_NOT_REPORTED,
61+
EVENT_BATTERY_THRESHOLD,
6062
DATA_STORE,
6163
ATTR_REMOVE,
6264
ATTR_DEVICE_ID,
@@ -67,6 +69,10 @@
6769
ATTR_BATTERY_LAST_REPORTED,
6870
ATTR_BATTERY_LAST_REPORTED_DAYS,
6971
ATTR_BATTERY_LAST_REPORTED_LEVEL,
72+
ATTR_BATTERY_LEVEL,
73+
ATTR_PREVIOUS_BATTERY_LEVEL,
74+
ATTR_BATTERY_THRESHOLD_REMINDER,
75+
ATTR_BATTERY_LOW,
7076
CONF_BATTERY_TYPE,
7177
CONF_BATTERY_QUANTITY,
7278
MIN_HA_VERSION,
@@ -267,7 +273,7 @@ async def async_update_options(hass: HomeAssistant, entry: ConfigEntry) -> None:
267273

268274

269275
@callback
270-
def register_services(hass):
276+
def register_services(hass: HomeAssistant):
271277
"""Register services used by battery notes component."""
272278

273279
async def handle_battery_replaced(call):
@@ -356,6 +362,33 @@ async def handle_battery_last_reported(call):
356362
str(device.coordinator.last_reported),
357363
)
358364

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+
359392
hass.services.async_register(
360393
DOMAIN,
361394
SERVICE_BATTERY_REPLACED,
@@ -369,3 +402,9 @@ async def handle_battery_last_reported(call):
369402
handle_battery_last_reported,
370403
schema=SERVICE_CHECK_BATTERY_LAST_REPORTED_SCHEMA,
371404
)
405+
406+
hass.services.async_register(
407+
DOMAIN,
408+
SERVICE_CHECK_BATTERY_LOW,
409+
handle_battery_low,
410+
)

custom_components/battery_notes/const.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565

6666
SERVICE_CHECK_BATTERY_LAST_REPORTED = "check_battery_last_reported"
6767
SERVICE_DATA_DAYS_LAST_REPORTED = "days_last_reported"
68+
SERVICE_CHECK_BATTERY_LOW = "check_battery_low"
6869

6970
EVENT_BATTERY_THRESHOLD = "battery_notes_battery_threshold"
7071
EVENT_BATTERY_INCREASED = "battery_notes_battery_increased"
@@ -84,6 +85,7 @@
8485
ATTR_BATTERY_LAST_REPORTED_DAYS = "battery_last_reported_days"
8586
ATTR_BATTERY_LAST_REPORTED_LEVEL = "battery_last_reported_level"
8687
ATTR_PREVIOUS_BATTERY_LEVEL = "previous_battery_level"
88+
ATTR_BATTERY_THRESHOLD_REMINDER = "reminder"
8789

8890
SERVICE_BATTERY_REPLACED_SCHEMA = vol.Schema(
8991
{

custom_components/battery_notes/coordinator.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
ATTR_DEVICE_NAME,
3737
ATTR_BATTERY_LEVEL,
3838
ATTR_PREVIOUS_BATTERY_LEVEL,
39+
ATTR_BATTERY_THRESHOLD_REMINDER,
3940
ATTR_REMOVE,
4041
LAST_REPLACED,
4142
LAST_REPORTED,
@@ -96,6 +97,7 @@ def battery_low_template_state(self, value):
9697
ATTR_BATTERY_TYPE_AND_QUANTITY: self.battery_type_and_quantity,
9798
ATTR_BATTERY_TYPE: self.battery_type,
9899
ATTR_BATTERY_QUANTITY: self.battery_quantity,
100+
ATTR_BATTERY_THRESHOLD_REMINDER: False,
99101
},
100102
)
101103

@@ -145,6 +147,7 @@ def current_battery_level(self, value):
145147
ATTR_BATTERY_QUANTITY: self.battery_quantity,
146148
ATTR_BATTERY_LEVEL: self.rounded_battery_level,
147149
ATTR_PREVIOUS_BATTERY_LEVEL: self._previous_battery_level,
150+
ATTR_BATTERY_THRESHOLD_REMINDER: False,
148151
},
149152
)
150153

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"services": {
33
"set_battery_replaced": "mdi:battery-sync",
4-
"check_battery_reported": "mdi:battery-unknown"
4+
"check_battery_last_reported": "mdi:battery-unknown",
5+
"check_battery_low": "mdi:battery-alert"
56
}
67
}

custom_components/battery_notes/library_updater.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ async def get_library_updates(self, time):
100100

101101
except LibraryUpdaterClientError:
102102
_LOGGER.warning(
103-
"Library update failed, this could be a GitHub or internet connectivity issue, will retry later."
103+
"Unable to update library, this could be a GitHub or internet connectivity issue, will retry later."
104104
)
105105

106106
async def time_to_update_library(self) -> bool:

custom_components/battery_notes/services.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,6 @@ check_battery_last_reported:
2929
min: 1
3030
max: 100
3131
mode: box
32+
check_battery_low:
33+
name: Check battery low
34+
description: "Raise events for devices that have a low battery."

custom_components/battery_notes/store.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
import attr
1111
from homeassistant.core import callback, HomeAssistant
12-
from homeassistant.loader import bind_hass
1312
from homeassistant.helpers.storage import Store
1413

1514
from .const import (
@@ -140,8 +139,6 @@ def async_update_device(self, device_id: str, changes: dict) -> DeviceEntry:
140139
self.async_schedule_save()
141140
return new
142141

143-
144-
@bind_hass
145142
async def async_get_registry(hass: HomeAssistant) -> BatteryNotesStorage:
146143
"""Return battery notes storage instance."""
147144
task = hass.data.get(DATA_REGISTRY)

custom_components/battery_notes/translations/en.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,10 @@
139139
}
140140
},
141141
"name": "Check battery last reported"
142+
},
143+
"check_battery_low": {
144+
"description": "Raise events for devices that have a low battery.",
145+
"name": "Check battery low"
142146
}
143147
}
144148
}

docs/community.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,22 @@ action:
7272
mode: queued
7373
```
7474
75+
### Check Battery Low daily reminder
76+
Call the check battery low service every day to raise events for those that are still low.
77+
To be used in conjunction with a [Battery Low Notification](community.md/#battery-low-notification) or similar.
78+
79+
```yaml
80+
alias: Daily Battery Low Check
81+
description: Check whether a battery is low
82+
trigger:
83+
- platform: time
84+
at: "09:00:00"
85+
condition: []
86+
action:
87+
- service: battery_notes.check_battery_low
88+
mode: single
89+
```
90+
7591
### Battery Replaced
7692
Mark a battery as replaced when there is an increase in battery level.
7793

docs/events.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ You can use this to send notifications in your preferred method. An example aut
1919
| `battery_quantity` | `int` | Battery quantity. |
2020
| `battery_level` | `float` | Battery level % of the device. |
2121
| `previous_battery_level` | `float` | Previous battery level % of the device. |
22+
| `reminder` | `bool` | Returns true if the event was raised by a service call, false if it's from a device event. |
2223

2324
### Automation Example
2425

0 commit comments

Comments
 (0)