Skip to content

Commit 7d3c1e0

Browse files
Filterdevices (#146)
* Filter to devices with batteries * Update filters to include binary sensor * Update readme * Update readme * Update readme
1 parent a52b73d commit 7d3c1e0

File tree

4 files changed

+47
-3
lines changed

4 files changed

+47
-3
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,13 @@ Unfortunately where there are multiple integrations associated with a device Hom
8787
* Can I edit a battery note?
8888
Go into Settings -> Integrations -> Battery Notes and click Configure on the device you want to edit.
8989

90+
* Why am I only able to see some of my devices when adding manually?
91+
By default Battery Notes filters the device list to only devices with a battery, if you want to add a battery note to a random device then you can disable this filtering by adding the following configuration to your `configuration.yaml` and restart Home Assistant to see all devices.
92+
```
93+
battery_notes:
94+
show_all_devices: true
95+
```
96+
9097
* I only want to add notes to a few devices, can I disable auto discovery?
9198
If you want to disable this functionality you can add the following to your `configuration.yaml`, after a restart of Home Assistant you will not see discovered battery notes.
9299
```
@@ -98,6 +105,7 @@ battery_notes:
98105
[!["Buy Me A Coffee"](https://www.buymeacoffee.com/assets/img/custom_images/yellow_img.png)](https://www.buymeacoffee.com/codechimp)
99106

100107

108+
101109
## Contributing to the Battery Library
102110

103111
<!-- To add a device definition to the battery library so that it will be automatically configured there are two options:

custom_components/battery_notes/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
CONF_ENABLE_AUTODISCOVERY,
3232
CONF_LIBRARY,
3333
DATA_UPDATE_COORDINATOR,
34+
CONF_SHOW_ALL_DEVICES,
3435
)
3536

3637
MIN_HA_VERSION = "2023.7"
@@ -44,6 +45,7 @@
4445
{
4546
vol.Optional(CONF_ENABLE_AUTODISCOVERY, default=True): cv.boolean,
4647
vol.Optional(CONF_LIBRARY, default="library.json"): cv.string,
48+
vol.Optional(CONF_SHOW_ALL_DEVICES, default=False): cv.boolean,
4749
},
4850
),
4951
),
@@ -66,6 +68,7 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
6668

6769
domain_config: ConfigType = config.get(DOMAIN) or {
6870
CONF_ENABLE_AUTODISCOVERY: True,
71+
CONF_SHOW_ALL_DEVICES: False,
6972
}
7073

7174
hass.data[DOMAIN] = {

custom_components/battery_notes/config_flow.py

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
from homeassistant.config_entries import ConfigEntry, OptionsFlow
1414
from homeassistant.helpers import selector
1515
from homeassistant.helpers.typing import DiscoveryInfoType
16-
16+
from homeassistant.const import Platform
17+
from homeassistant.components.sensor import SensorDeviceClass
1718
import homeassistant.helpers.device_registry as dr
1819

1920
from homeassistant.const import (
@@ -30,14 +31,38 @@
3031
CONF_MANUFACTURER,
3132
CONF_MODEL,
3233
DATA_UPDATE_COORDINATOR,
34+
DOMAIN_CONFIG,
35+
CONF_SHOW_ALL_DEVICES,
3336
)
3437

3538
_LOGGER = logging.getLogger(__name__)
3639

40+
DEVICE_SCHEMA_ALL = vol.Schema(
41+
{
42+
vol.Required(CONF_DEVICE_ID): selector.DeviceSelector(
43+
config=selector.DeviceFilterSelectorConfig()
44+
),
45+
vol.Optional(CONF_NAME): selector.TextSelector(
46+
selector.TextSelectorConfig(type=selector.TextSelectorType.TEXT),
47+
),
48+
}
49+
)
50+
3751
DEVICE_SCHEMA = vol.Schema(
3852
{
3953
vol.Required(CONF_DEVICE_ID): selector.DeviceSelector(
40-
# selector.DeviceSelectorConfig(model="otgw-nodo")
54+
config=selector.DeviceSelectorConfig(
55+
entity=[
56+
selector.EntityFilterSelectorConfig(
57+
domain=Platform.SENSOR,
58+
device_class=SensorDeviceClass.BATTERY,
59+
),
60+
selector.EntityFilterSelectorConfig(
61+
domain=Platform.BINARY_SENSOR,
62+
device_class=SensorDeviceClass.BATTERY,
63+
),
64+
]
65+
)
4166
),
4267
vol.Optional(CONF_NAME): selector.TextSelector(
4368
selector.TextSelectorConfig(type=selector.TextSelectorType.TEXT),
@@ -112,9 +137,16 @@ async def async_step_user(
112137

113138
return await self.async_step_battery()
114139

140+
domain_config = self.hass.data[DOMAIN][DOMAIN_CONFIG]
141+
142+
schema = DEVICE_SCHEMA
143+
# If show_all_devices = is specified and true, don't filter
144+
if domain_config.get(CONF_SHOW_ALL_DEVICES, False):
145+
schema = DEVICE_SCHEMA_ALL
146+
115147
return self.async_show_form(
116148
step_id="user",
117-
data_schema=DEVICE_SCHEMA,
149+
data_schema=schema,
118150
errors=_errors,
119151
last_step=False,
120152
)

custom_components/battery_notes/const.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
CONF_MANUFACTURER = "manufacturer"
2929
CONF_DEVICE_NAME = "device_name"
3030
CONF_LIBRARY_URL = "https://raw.githubusercontent.com/andrew-codechimp/HA-Battery-Notes/main/custom_components/battery_notes/data/library.json" # pylint: disable=line-too-long
31+
CONF_SHOW_ALL_DEVICES = "show_all_devices"
3132

3233
DATA_CONFIGURED_ENTITIES = "configured_entities"
3334
DATA_DISCOVERED_ENTITIES = "discovered_entities"

0 commit comments

Comments
 (0)