Skip to content

Commit 4b935de

Browse files
Merge branch 'main' into docs-next
2 parents 942a633 + e375fdc commit 4b935de

File tree

8 files changed

+100
-43
lines changed

8 files changed

+100
-43
lines changed

custom_components/battery_notes/binary_sensor.py

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,14 @@
6666
from . import PLATFORMS
6767
from .common import validate_is_float
6868
from .const import (
69+
ATTR_BATTERY_LAST_REPLACED,
6970
ATTR_BATTERY_LOW_THRESHOLD,
7071
ATTR_BATTERY_QUANTITY,
7172
ATTR_BATTERY_TYPE,
7273
ATTR_BATTERY_TYPE_AND_QUANTITY,
74+
ATTR_DEVICE_ID,
75+
ATTR_DEVICE_NAME,
76+
ATTR_SOURCE_ENTITY_ID,
7377
CONF_SOURCE_ENTITY_ID,
7478
DOMAIN,
7579
)
@@ -330,12 +334,27 @@ class BatteryNotesBatteryLowBaseSensor(
330334
):
331335
"""Low battery binary sensor base."""
332336

337+
def __init__(
338+
self,
339+
hass: HomeAssistant,
340+
coordinator: BatteryNotesCoordinator,
341+
):
342+
"""Initialize the low battery binary sensor."""
343+
344+
super().__init__(coordinator=coordinator)
345+
346+
self.enable_replaced = hass.data[MY_KEY].enable_replaced
347+
333348
_unrecorded_attributes = frozenset(
334349
{
335350
ATTR_BATTERY_LOW_THRESHOLD,
336351
ATTR_BATTERY_QUANTITY,
337352
ATTR_BATTERY_TYPE,
338353
ATTR_BATTERY_TYPE_AND_QUANTITY,
354+
ATTR_BATTERY_LAST_REPLACED,
355+
ATTR_DEVICE_ID,
356+
ATTR_SOURCE_ENTITY_ID,
357+
ATTR_DEVICE_NAME,
339358
}
340359
)
341360

@@ -351,6 +370,14 @@ def extra_state_attributes(self) -> dict[str, Any] | None:
351370
ATTR_BATTERY_TYPE_AND_QUANTITY: self.coordinator.battery_type_and_quantity,
352371
}
353372

373+
if self.enable_replaced:
374+
attrs[ATTR_BATTERY_LAST_REPLACED] = self.coordinator.last_replaced
375+
376+
# Other attributes that should follow battery, attribute list is unsorted
377+
attrs[ATTR_DEVICE_ID] = self.coordinator.device_id or ""
378+
attrs[ATTR_SOURCE_ENTITY_ID] = self.coordinator.source_entity_id or ""
379+
attrs[ATTR_DEVICE_NAME] = self.coordinator.device_name
380+
354381
super_attrs = super().extra_state_attributes
355382
if super_attrs:
356383
attrs.update(super_attrs)
@@ -382,7 +409,7 @@ def __init__(
382409
self._attr_unique_id = unique_id
383410
self._template_attrs: dict[Template, list[_TemplateAttribute]] = {}
384411

385-
super().__init__(coordinator=coordinator)
412+
super().__init__(hass=hass, coordinator=coordinator)
386413

387414
if coordinator.device_id and (
388415
device_entry := device_registry.async_get(coordinator.device_id)
@@ -612,7 +639,7 @@ def __init__(
612639
self.entity_description = description
613640
self._attr_unique_id = unique_id
614641

615-
super().__init__(coordinator=coordinator)
642+
super().__init__(hass=hass, coordinator=coordinator)
616643

617644
if coordinator.device_id and (
618645
device_entry := device_registry.async_get(coordinator.device_id)
@@ -707,7 +734,7 @@ def __init__(
707734
self.entity_description = description
708735
self._attr_unique_id = unique_id
709736

710-
super().__init__(coordinator=coordinator)
737+
super().__init__(hass=hass, coordinator=coordinator)
711738

712739
if coordinator.device_id and (
713740
device_entry := device_registry.async_get(coordinator.device_id)

custom_components/battery_notes/config_flow.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -213,9 +213,9 @@ async def async_step_device(
213213

214214
schema = DEVICE_SCHEMA
215215
# If show_all_devices = is specified and true, don't filter
216-
domain_config = self.hass.data[MY_KEY]
217-
if domain_config.show_all_devices:
218-
schema = DEVICE_SCHEMA_ALL
216+
domain_config = self.hass.data.get(MY_KEY)
217+
if domain_config and domain_config.show_all_devices:
218+
schema = DEVICE_SCHEMA_ALL
219219

220220
return self.async_show_form(
221221
step_id="device",

custom_components/battery_notes/library.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,8 @@ def _load_library_json(library_file: str) -> dict[str, Any]:
4343
return cast(dict[str, Any], json.load(file))
4444

4545
# User Library
46-
domain_config = self.hass.data[MY_KEY]
47-
48-
if domain_config.user_library != "":
46+
domain_config = self.hass.data.get(MY_KEY)
47+
if domain_config and domain_config.user_library != "":
4948
json_user_path = self.hass.config.path(STORAGE_DIR, "battery_notes", domain_config.user_library)
5049
_LOGGER.debug("Using user library file at %s", json_user_path)
5150

custom_components/battery_notes/library_updater.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
from homeassistant.helpers.event import async_track_utc_time_change
1919
from homeassistant.helpers.storage import STORAGE_DIR
2020

21-
from .coordinator import MY_KEY
21+
from .coordinator import MY_KEY, BatteryNotesDomainConfig
2222
from .discovery import DiscoveryManager
2323

2424
_LOGGER = logging.getLogger(__name__)
@@ -38,7 +38,9 @@ def __init__(self, hass: HomeAssistant):
3838
"""Initialize the library updater."""
3939
self.hass = hass
4040

41-
domain_config = self.hass.data[MY_KEY]
41+
domain_config = self.hass.data.get(MY_KEY)
42+
if not domain_config:
43+
domain_config = BatteryNotesDomainConfig()
4244

4345
library_url = domain_config.library_url
4446
schema_url = domain_config.schema_url
@@ -61,7 +63,7 @@ async def timer_update(self, now: datetime):
6163

6264
domain_config = self.hass.data[MY_KEY]
6365

64-
if domain_config.enable_autodiscovery:
66+
if domain_config and domain_config.enable_autodiscovery:
6567
discovery_manager = DiscoveryManager(self.hass, domain_config)
6668
await discovery_manager.start_discovery()
6769
else:
@@ -90,7 +92,9 @@ def _update_library_json(library_file: str, content: str) -> None:
9092
_update_library_json, json_path, content
9193
)
9294

93-
self.hass.data[MY_KEY].library_last_update = datetime.now()
95+
domain_config = self.hass.data.get(MY_KEY)
96+
if domain_config:
97+
self.hass.data[MY_KEY].library_last_update = datetime.now()
9498

9599
_LOGGER.debug("Updated library")
96100
else:
@@ -117,6 +121,10 @@ async def copy_schema(self):
117121
async def time_to_update_library(self, hours: int) -> bool:
118122
"""Check when last updated and if OK to do a new library update."""
119123
try:
124+
domain_config = self.hass.data.get(MY_KEY)
125+
if not domain_config:
126+
return True
127+
120128
if library_last_update := self.hass.data[MY_KEY].library_last_update:
121129
time_since_last_update = (
122130
datetime.now() - library_last_update

library.md

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
## 1776 Devices in library
1+
## 1779 Devices in library
22

33
This file is auto generated, do not modify
44

@@ -377,6 +377,7 @@ Request new devices to be added to the library [here](https://github.com/andrew-
377377
|Danfoss |TRV001 |2× AA | | |
378378
|Datek |Eva water leak sensor |2× CR2450 |HSE2919E | |
379379
|Develco |AQSZB-110 |2× AA | | |
380+
|Develco |Fire detector with siren |CR123 |HESZB-120 |5.0.2 |
380381
|Develco |Fire detector with siren (HESZB-120) |CR123A | | |
381382
|Develco |Keypad (KEYZB-110) |4× AA | | |
382383
|Develco |KEYZB-110 |4× AA | | |
@@ -554,6 +555,7 @@ Request new devices to be added to the library [here](https://github.com/andrew-
554555
|Eve |Weather |CR2450 |20EBS9901 | 1.10|
555556
|Eve Systems |Eve Door 20EBN9901 |LS14250 | | |
556557
|Eve Systems |Eve Motion 20EBY9901 |2× AAA | | |
558+
|Eve Systems |Eve MotionBlinds 20CAA9901 |Rechargeable | 85| 1.10|
557559
|Eve Systems |Eve Thermo 20EBP1701 |2× AA | 79| 1.10|
558560
|Eve Systems |Thermo 20EBP1701 |2× AA | | |
559561
|Everspring |AC301 |2× AA | | |
@@ -1118,10 +1120,10 @@ Request new devices to be added to the library [here](https://github.com/andrew-
11181120
|SAF Tehnika |Aranet Radon Plus |2× AA | | |
11191121
|SAF Tehnika |Aranet2 HOME |2× AA | | |
11201122
|SAF Tehnika |Aranet4 HOME |2× AA | | |
1121-
|Samjin |button |CR2 | | |
1122-
|Samjin |motion |CR2 | | |
1123+
|Samjin |button |CR2450 | | |
1124+
|Samjin |motion |CR2450 | | |
11231125
|Samjin |multi |CR2450 | | |
1124-
|Samjin |water |CR2 | | |
1126+
|Samjin |water |CR2450 | | |
11251127
|Samsung |SM-A525F |Rechargeable | | |
11261128
|Samsung |SM-A528B |Rechargeable | | |
11271129
|Samsung |SM-A546B |Rechargeable | | |
@@ -1248,6 +1250,7 @@ Request new devices to be added to the library [here](https://github.com/andrew-
12481250
|SmartThings |Multi Sensor (2015 model) (3321-S) |CR2450 | | |
12491251
|SmartThings |Multipurpose sensor (2016 model) |CR2450 |F-MLT-US-2 | |
12501252
|SmartThings |Multipurpose sensor (2016 model) |CR2450 |F-MLT-US-2 | |
1253+
|SmartThings |Multipurpose sensor (2018 model) |CR2450 |IM6001-MPP01 | |
12511254
|SmartThings |Multipurpose sensor (2018 model) (IM6001-MPP01) |CR2450 | | |
12521255
|SmartThings |multiv4 |CR2450 | | |
12531256
|Smartthings |Water leak sensor (2016 model) (STS-WTR-250) |CR2 | | |
@@ -1318,7 +1321,7 @@ Request new devices to be added to the library [here](https://github.com/andrew-
13181321
|SwitchBot |Meter |2× AAA | | |
13191322
|SwitchBot |MeterPlus |2× AAA | | |
13201323
|switchbot |Roller Shade |Rechargeable | | |
1321-
|SwitchBot |Smart Lock Pro |Recharchable | |V2.3 |
1324+
|SwitchBot |Smart Lock Pro |Rechargeable | |V2.3 |
13221325
|Switchbot |W070160X |Rechargeable | | |
13231326
|SwitchBot |W1101500 |2× AAA | | |
13241327
|SwitchBot |W1201500 |2× AAA | | |

library/library.json

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2099,6 +2099,13 @@
20992099
"battery_type": "AA",
21002100
"battery_quantity": 2
21012101
},
2102+
{
2103+
"manufacturer": "Develco",
2104+
"model": "Fire detector with siren",
2105+
"model_id": "HESZB-120",
2106+
"hw_version": "5.0.2",
2107+
"battery_type": "CR123"
2108+
},
21022109
{
21032110
"manufacturer": "Develco",
21042111
"model": "Fire detector with siren (HESZB-120)",
@@ -3115,6 +3122,13 @@
31153122
"battery_type": "AAA",
31163123
"battery_quantity": 2
31173124
},
3125+
{
3126+
"manufacturer": "Eve Systems",
3127+
"model": "Eve MotionBlinds 20CAA9901",
3128+
"model_id": "85",
3129+
"hw_version": "1.1",
3130+
"battery_type": "Rechargeable"
3131+
},
31183132
{
31193133
"manufacturer": "Eve Systems",
31203134
"model": "Eve Thermo 20EBP1701",
@@ -6283,12 +6297,12 @@
62836297
{
62846298
"manufacturer": "Samjin",
62856299
"model": "button",
6286-
"battery_type": "CR2"
6300+
"battery_type": "CR2450"
62876301
},
62886302
{
62896303
"manufacturer": "Samjin",
62906304
"model": "motion",
6291-
"battery_type": "CR2"
6305+
"battery_type": "CR2450"
62926306
},
62936307
{
62946308
"manufacturer": "Samjin",
@@ -6298,7 +6312,7 @@
62986312
{
62996313
"manufacturer": "Samjin",
63006314
"model": "water",
6301-
"battery_type": "CR2"
6315+
"battery_type": "CR2450"
63026316
},
63036317
{
63046318
"manufacturer": "Samsung",
@@ -6977,6 +6991,12 @@
69776991
"model_id": "F-MLT-US-2",
69786992
"battery_type": "CR2450"
69796993
},
6994+
{
6995+
"manufacturer": "SmartThings",
6996+
"model": "Multipurpose sensor (2018 model)",
6997+
"model_id": "IM6001-MPP01",
6998+
"battery_type": "CR2450"
6999+
},
69807000
{
69817001
"manufacturer": "SmartThings",
69827002
"model": "Multipurpose sensor (2018 model) (IM6001-MPP01)",
@@ -7372,7 +7392,7 @@
73727392
"manufacturer": "SwitchBot",
73737393
"model": "Smart Lock Pro",
73747394
"hw_version": "V2.3",
7375-
"battery_type": "Recharchable",
7395+
"battery_type": "Rechargeable",
73767396
"battery_quantity": 2
73777397
},
73787398
{

0 commit comments

Comments
 (0)