Skip to content

Commit d5bc0f6

Browse files
Fixes for starting with no existing battery notes
1 parent 723aa7e commit d5bc0f6

File tree

6 files changed

+52
-17
lines changed

6 files changed

+52
-17
lines changed

README.md

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,20 +30,30 @@ Platform | Description
3030
Or
3131
Search for `Battery Notes` in HACS and install it under the "Integrations" category.
3232
Restart Home Assistant
33+
Add the following entry to your ```configuration.yaml```
34+
```
35+
battery_notes:
36+
enable_autodiscovery: true
37+
```
38+
Restart Home Assistant a final time
3339
In the HA UI go to "Configuration" -> "Integrations" click "+" and search for "Battery Notes"
3440

3541
### Manual Installation
3642

37-
<details>
38-
<summary>Show detailed instructions</summary>
39-
1. Using the tool of choice open the directory (folder) for your HA configuration (where you find `configuration.yaml`).
40-
1. If you do not have a `custom_components` directory (folder) there, you need to create it.
41-
1. In the `custom_components` directory (folder) create a new folder called `battery_notes`.
42-
1. Download _all_ the files from the `custom_components/battery_notes/` directory (folder) in this repository.
43-
1. Place the files you downloaded in the new directory (folder) you created.
44-
1. Restart Home Assistant
45-
1. In the HA UI go to "Configuration" -> "Integrations" click "+" and search for "Battery Notes"
46-
</details>
43+
Using the tool of choice open the directory (folder) for your HA configuration (where you find `configuration.yaml`).
44+
If you do not have a `custom_components` directory (folder) there, you need to create it.
45+
In the `custom_components` directory (folder) create a new folder called `battery_notes`.
46+
Download _all_ the files from the `custom_components/battery_notes/` directory (folder) in this repository.
47+
Place the files you downloaded in the new directory (folder) you created.
48+
Restart Home Assistant
49+
Add the following entry to your ```configuration.yaml```
50+
```
51+
battery_notes:
52+
enable_autodiscovery: true
53+
```
54+
Restart Home Assistant a final time
55+
In the HA UI go to "Configuration" -> "Integrations" click "+" and search for "Battery Notes"
56+
4757

4858
## Configuration is done in the UI
4959

@@ -52,8 +62,8 @@ The battery type will then be displayed as a diagnostic sensor on the device.
5262

5363
## Automatic discovery
5464

55-
By default Battery Notes will automatically discover devices that it has in its library and create a notification to add a battery note.
56-
If you wish to disable this functionality then add the following to your ```configuration.yaml```
65+
Battery Notes will automatically discover devices (as long as you have followed the installation instructions above) that it has in its library and create a notification to add a battery note.
66+
If you wish to disable this functionality then change your ```configuration.yaml``` to set enable_autodiscovery to false
5767
```
5868
battery_notes:
5969
enable_autodiscovery: false

custom_components/battery_notes/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
5151
if domain_config.get(CONF_ENABLE_AUTODISCOVERY):
5252
discovery_manager = DiscoveryManager(hass, config)
5353
await discovery_manager.start_discovery()
54+
else:
55+
_LOGGER.debug("Auto discovery disabled")
5456

5557
return True
5658

custom_components/battery_notes/config_flow.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,13 +87,20 @@ async def async_step_user(
8787
device_registry = dr.async_get(self.hass)
8888
device_entry = device_registry.async_get(device_id)
8989

90-
library = Library(self.hass)
90+
_LOGGER.debug(
91+
"Looking up device %s %s", device_entry.manufacturer, device_entry.model
92+
)
93+
94+
library = Library.factory(self.hass)
9195

9296
device_battery_details = await library.get_device_battery_details(
9397
device_entry.manufacturer, device_entry.model
9498
)
9599

96100
if device_battery_details:
101+
_LOGGER.debug(
102+
"Found device %s %s", device_entry.manufacturer, device_entry.model
103+
)
97104
self.data[
98105
CONF_BATTERY_TYPE
99106
] = device_battery_details.battery_type_and_quantity

custom_components/battery_notes/const.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
DATA_CONFIGURED_ENTITIES = "configured_entities"
3232
DATA_DISCOVERED_ENTITIES = "discovered_entities"
3333
DATA_DOMAIN_ENTITIES = "domain_entities"
34+
DATA_LIBRARY = "library"
3435

3536
PLATFORMS: Final = [
3637
Platform.SENSOR,

custom_components/battery_notes/discovery.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,14 +75,13 @@ def __init__(self, hass: HomeAssistant, ha_config: ConfigType) -> None:
7575
"""Init."""
7676
self.hass = hass
7777
self.ha_config = ha_config
78-
self.manually_configured_entities: list[str] | None = None
7978

8079
async def start_discovery(self) -> None:
8180
"""Start the discovery procedure."""
8281
_LOGGER.debug("Start auto discovering devices")
8382
device_registry = dr.async_get(self.hass)
8483

85-
library = Library(self.hass)
84+
library = Library.factory(self.hass)
8685

8786
if library.loaded():
8887
for device_entry in list(device_registry.devices.values()):
@@ -105,6 +104,8 @@ async def start_discovery(self) -> None:
105104
continue
106105

107106
self._init_entity_discovery(device_entry, device_battery_details)
107+
else:
108+
_LOGGER.error("Library not loaded")
108109

109110
_LOGGER.debug("Done auto discovering devices")
110111

custom_components/battery_notes/library.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
from .const import (
1212
DOMAIN,
13+
DATA_LIBRARY,
1314
DOMAIN_CONFIG,
1415
CONF_LIBRARY,
1516
)
@@ -41,11 +42,24 @@ def __init__(self, hass: HomeAssistant) -> None:
4142

4243
except FileNotFoundError:
4344
_LOGGER.error(
44-
"%s file not found in directory %s",
45-
hass.data[DOMAIN][DOMAIN_CONFIG].get(CONF_LIBRARY, "library.json"),
45+
"library.json file not found in directory %s",
4646
BUILT_IN_DATA_DIRECTORY,
4747
)
4848

49+
@staticmethod
50+
def factory(hass: HomeAssistant) -> Library:
51+
"""Return the library or create."""
52+
53+
if DOMAIN not in hass.data:
54+
hass.data[DOMAIN] = {}
55+
56+
if DATA_LIBRARY in hass.data[DOMAIN]:
57+
return hass.data[DOMAIN][DATA_LIBRARY] # type: ignore
58+
59+
library = Library(hass)
60+
hass.data[DOMAIN][DATA_LIBRARY] = library
61+
return library
62+
4963
async def get_device_battery_details(
5064
self,
5165
manufacturer: str,

0 commit comments

Comments
 (0)