Skip to content

Commit

Permalink
Replace battery threshold attribute on battery low
Browse files Browse the repository at this point in the history
  • Loading branch information
andrew-codechimp committed Jan 23, 2025
1 parent 4e9373a commit 87bbeba
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
16 changes: 13 additions & 3 deletions custom_components/battery_notes/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -355,6 +360,7 @@ def extra_state_attributes(self) -> dict[str, Any] | None:
attrs.update(super_attrs)
return attrs


class BatteryNotesBatteryLowTemplateSensor(
BatteryNotesBatteryLowBaseSensor, RestoreEntity
):
Expand Down Expand Up @@ -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()

Expand All @@ -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)
Expand Down
7 changes: 5 additions & 2 deletions docs/entities.md
Original file line number Diff line number Diff line change
Expand Up @@ -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. |
| `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 |

0 comments on commit 87bbeba

Please sign in to comment.