From 87bbeba042540e92b47d609b20e8066d63201cfe Mon Sep 17 00:00:00 2001 From: Andrew Jackson Date: Thu, 23 Jan 2025 09:27:07 +0000 Subject: [PATCH] Replace battery threshold attribute on battery low --- custom_components/battery_notes/binary_sensor.py | 16 +++++++++++++--- docs/entities.md | 7 +++++-- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/custom_components/battery_notes/binary_sensor.py b/custom_components/battery_notes/binary_sensor.py index eed2bad1e..ffb79894f 100644 --- a/custom_components/battery_notes/binary_sensor.py +++ b/custom_components/battery_notes/binary_sensor.py @@ -328,8 +328,12 @@ def handle_result( self.on_update(validated) return -class BatteryNotesBatteryLowBaseSensor(BinarySensorEntity, CoordinatorEntity[BatteryNotesCoordinator]): + +class BatteryNotesBatteryLowBaseSensor( + BinarySensorEntity, CoordinatorEntity[BatteryNotesCoordinator] +): """Low battery binary sensor base.""" + _unrecorded_attributes = frozenset( { ATTR_BATTERY_LOW_THRESHOLD, @@ -345,6 +349,7 @@ def extra_state_attributes(self) -> dict[str, Any] | None: # Battery related attributes attrs = { + ATTR_BATTERY_LOW_THRESHOLD: self.coordinator.battery_low_threshold, ATTR_BATTERY_QUANTITY: self.coordinator.battery_quantity, ATTR_BATTERY_TYPE: self.coordinator.battery_type, ATTR_BATTERY_TYPE_AND_QUANTITY: self.coordinator.battery_type_and_quantity, @@ -355,6 +360,7 @@ def extra_state_attributes(self) -> dict[str, Any] | None: attrs.update(super_attrs) return attrs + class BatteryNotesBatteryLowTemplateSensor( BatteryNotesBatteryLowBaseSensor, RestoreEntity ): @@ -746,7 +752,9 @@ async def async_state_changed_listener( self.async_write_ha_state() return - self.coordinator.battery_low_binary_state = wrapped_battery_low_state.state == "on" + self.coordinator.battery_low_binary_state = ( + wrapped_battery_low_state.state == "on" + ) await self.coordinator.async_request_refresh() @@ -764,7 +772,9 @@ async def _register_entity_id_change_listener( """Listen for battery entity_id changes and update battery_plus.""" @callback - async def _entity_rename_listener(event: Event[er.EventEntityRegistryUpdatedData]) -> None: + async def _entity_rename_listener( + event: Event[er.EventEntityRegistryUpdatedData], + ) -> None: """Handle renaming of the entity.""" new_entity_id = event.data["entity_id"] old_entity_id = event.data.get("old_entity_id", None) diff --git a/docs/entities.md b/docs/entities.md index b91c2c249..f3aa31a19 100644 --- a/docs/entities.md +++ b/docs/entities.md @@ -71,10 +71,13 @@ The last time the battery of the device was replaced. A button to set the battery_last_replaced entity to now. ## Battery Low -`sensor.{{device_name}}_battery_plus_low` +`binary_sensor.{{device_name}}_battery_plus_low` A boolean sensor indicating if the battery is low, true when the battery is below the device or global threshold. | Attribute | Type | Description | |-----------|------|-------------| -| `battery_low_threshold` | `int` | The device or global threshold for when the battery is low. | \ No newline at end of file +| `battery_low_threshold` | `int` | The device or global threshold for when the battery is low | +| `battery_quantity` | `int` | The quantity of batteries | +| `battery_type` | `string` | The type of batteries | +| `battery_type_and_quantity` | `string` | The type of batteries with the quantity if more than 1 |