Skip to content

Commit 61ca12e

Browse files
Hide battery low (#4962)
1 parent e16200b commit 61ca12e

6 files changed

Lines changed: 49 additions & 3 deletions

File tree

custom_components/battery_notes/__init__.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
CONF_ENABLE_REPLACED,
4747
CONF_FILTER_OUTLIERS,
4848
CONF_HIDE_BATTERY,
49+
CONF_HIDE_BATTERY_LOW,
4950
CONF_HW_VERSION,
5051
CONF_MANUFACTURER,
5152
CONF_MODEL,
@@ -88,6 +89,7 @@
8889
vol.Optional(CONF_SHOW_ALL_DEVICES, default=False): cv.boolean,
8990
vol.Optional(CONF_ENABLE_REPLACED, default=True): cv.boolean,
9091
vol.Optional(CONF_HIDE_BATTERY, default=False): cv.boolean,
92+
vol.Optional(CONF_HIDE_BATTERY_LOW, default=False): cv.boolean,
9193
vol.Optional(CONF_ROUND_BATTERY, default=False): cv.boolean,
9294
vol.Optional(
9395
CONF_DEFAULT_BATTERY_LOW_THRESHOLD,
@@ -158,6 +160,9 @@ async def async_setup_entry(
158160
domain_config.enable_replaced = config_entry.options[CONF_ADVANCED_SETTINGS][
159161
CONF_ENABLE_REPLACED
160162
]
163+
domain_config.hide_battery_low = config_entry.options[CONF_ADVANCED_SETTINGS].get(
164+
CONF_HIDE_BATTERY_LOW, False
165+
)
161166
domain_config.user_library = config_entry.options[CONF_ADVANCED_SETTINGS][
162167
CONF_USER_LIBRARY
163168
]

custom_components/battery_notes/binary_sensor.py

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,35 @@ def extra_state_attributes(self) -> dict[str, Any] | None:
256256
return attrs
257257

258258

259+
class BatteryNotesNonTemplateBatteryLowSensor(BatteryNotesBatteryLowBaseSensor):
260+
"""Low battery binary sensor base for non templated entities."""
261+
262+
entity_description: BatteryNotesBinarySensorEntityDescription
263+
264+
def __init__(
265+
self,
266+
hass: HomeAssistant,
267+
coordinator: BatteryNotesSubentryCoordinator,
268+
entity_description: BatteryNotesBinarySensorEntityDescription,
269+
):
270+
"""Initialize the low battery binary sensor."""
271+
272+
super().__init__(
273+
hass, entity_description=entity_description, coordinator=coordinator
274+
)
275+
276+
registry = er.async_get(hass)
277+
entity = registry.async_get(self.entity_id)
278+
279+
if entity is not None and entity.hidden_by != er.RegistryEntryHider.USER:
280+
registry.async_update_entity(
281+
self.entity_id,
282+
hidden_by=er.RegistryEntryHider.INTEGRATION
283+
if hass.data[MY_KEY].hide_battery_low
284+
else None,
285+
)
286+
287+
259288
class BatteryNotesBatteryLowBinaryTemplateSensor(
260289
BatteryNotesBatteryLowBaseSensor, RestoreEntity
261290
):
@@ -436,7 +465,9 @@ def is_on(self) -> bool | None:
436465
return self._state
437466

438467

439-
class BatteryNotesBatteryPercentageTemplateLowSensor(BatteryNotesBatteryLowBaseSensor):
468+
class BatteryNotesBatteryPercentageTemplateLowSensor(
469+
BatteryNotesNonTemplateBatteryLowSensor
470+
):
440471
"""Represents a low battery threshold binary sensor from a template percentage."""
441472

442473
_attr_should_poll = False
@@ -485,7 +516,7 @@ def _handle_coordinator_update(self) -> None:
485516
)
486517

487518

488-
class BatteryNotesBatteryWrappedLowSensor(BatteryNotesBatteryLowBaseSensor):
519+
class BatteryNotesBatteryWrappedLowSensor(BatteryNotesNonTemplateBatteryLowSensor):
489520
"""Represents a low battery threshold binary sensor from a device percentage."""
490521

491522
_attr_should_poll = False
@@ -547,7 +578,7 @@ def _handle_coordinator_update(self) -> None:
547578
)
548579

549580

550-
class BatteryNotesBatteryBinaryLowSensor(BatteryNotesBatteryLowBaseSensor):
581+
class BatteryNotesBatteryBinaryLowSensor(BatteryNotesNonTemplateBatteryLowSensor):
551582
"""Represents a low battery binary sensor from a binary sensor."""
552583

553584
_attr_should_poll = False

custom_components/battery_notes/config_flow.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
CONF_ENABLE_REPLACED,
4343
CONF_FILTER_OUTLIERS,
4444
CONF_HIDE_BATTERY,
45+
CONF_HIDE_BATTERY_LOW,
4546
CONF_HW_VERSION,
4647
CONF_INTEGRATION_NAME,
4748
CONF_MANUFACTURER,
@@ -88,6 +89,9 @@
8889
{
8990
vol.Required(CONF_ENABLE_AUTODISCOVERY): selector.BooleanSelector(),
9091
vol.Required(CONF_ENABLE_REPLACED): selector.BooleanSelector(),
92+
vol.Required(
93+
CONF_HIDE_BATTERY_LOW, default=False
94+
): selector.BooleanSelector(),
9195
vol.Optional(CONF_USER_LIBRARY): selector.TextSelector(),
9296
}
9397
),
@@ -291,6 +295,7 @@ async def async_step_integration_discovery(
291295
CONF_ADVANCED_SETTINGS: {
292296
CONF_ENABLE_AUTODISCOVERY: True,
293297
CONF_ENABLE_REPLACED: True,
298+
CONF_HIDE_BATTERY_LOW: False,
294299
CONF_USER_LIBRARY: "",
295300
},
296301
}
@@ -342,6 +347,7 @@ async def async_step_user(
342347
CONF_ADVANCED_SETTINGS: {
343348
CONF_ENABLE_AUTODISCOVERY: True,
344349
CONF_ENABLE_REPLACED: True,
350+
CONF_HIDE_BATTERY_LOW: False,
345351
CONF_USER_LIBRARY: "",
346352
},
347353
}

custom_components/battery_notes/const.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
CONF_BATTERY_PERCENTAGE_TEMPLATE = "battery_percentage_template"
5252
CONF_FILTER_OUTLIERS = "filter_outliers"
5353
CONF_ADVANCED_SETTINGS = "advanced_settings"
54+
CONF_HIDE_BATTERY_LOW = "hide_battery_low"
5455

5556
DATA_CONFIGURED_ENTITIES = "configured_entities"
5657
DATA_DISCOVERED_ENTITIES = "discovered_entities"

custom_components/battery_notes/coordinator.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ class BatteryNotesDomainConfig:
8888
show_all_devices: bool = False
8989
enable_replaced: bool = True
9090
hide_battery: bool = False
91+
hide_battery_low: bool = False
9192
round_battery: bool = False
9293
default_battery_low_threshold: int = DEFAULT_BATTERY_LOW_THRESHOLD
9394
battery_increased_threshod: int = DEFAULT_BATTERY_INCREASE_THRESHOLD

custom_components/battery_notes/translations/en.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,11 +173,13 @@
173173
"data": {
174174
"enable_autodiscovery": "Auto discovery",
175175
"enable_replaced": "Enable battery replaced",
176+
"hide_battery_low": "Hide battery low",
176177
"user_library": "User library"
177178
},
178179
"data_description": {
179180
"enable_autodiscovery": "Auto discovery of devices that are in the library.",
180181
"enable_replaced": "Enable the battery replaced button on each battery note.",
182+
"hide_battery_low": "Hide the battery low binary sensor when a battery percentage is available, avoiding duplication on generated dashboards.",
181183
"user_library": "If specified then a user library file will be searched prior to the main library, the user library must be in the same format as the library and placed in `config/.storage/battery_notes`."
182184
}
183185
}

0 commit comments

Comments
 (0)