Skip to content

Commit

Permalink
Battery fix (#675)
Browse files Browse the repository at this point in the history
* Check for battery being percentage and enabled

* Battery plus fixes

* lint
  • Loading branch information
andrew-codechimp authored Jan 26, 2024
1 parent 2641e96 commit 9c7ac6a
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 19 deletions.
3 changes: 3 additions & 0 deletions custom_components/battery_notes/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,9 @@ async def async_remove_entry(hass: HomeAssistant, config_entry: ConfigEntry) ->
if "device_id" not in config_entry.data:
return

if config_entry.entry_id not in hass.data[DOMAIN][DATA].devices:
return

device: BatteryNotesDevice = hass.data[DOMAIN][DATA].devices[config_entry.entry_id]
if not device:
return
Expand Down
10 changes: 1 addition & 9 deletions custom_components/battery_notes/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
ATTR_PREVIOUS_BATTERY_LEVEL,
)

from .common import isfloat
from .device import BatteryNotesDevice
from .coordinator import BatteryNotesCoordinator

Expand Down Expand Up @@ -363,12 +364,3 @@ def extra_state_attributes(self) -> dict[str, str] | None:
if super_attrs:
attrs.update(super_attrs)
return attrs


def isfloat(num):
"""Is the value a float."""
try:
float(num)
return True
except ValueError:
return False
10 changes: 10 additions & 0 deletions custom_components/battery_notes/common.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
"""Common functions for battery_notes."""


def isfloat(num):
"""Is the value a float."""
try:
float(num)
return True
except ValueError:
return False
8 changes: 8 additions & 0 deletions custom_components/battery_notes/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

from homeassistant.const import (
CONF_DEVICE_ID,
PERCENTAGE,
)

from .const import (
Expand Down Expand Up @@ -91,10 +92,17 @@ async def async_setup(self) -> bool:
continue
if not entity.platform or entity.platform == DOMAIN:
continue

if entity.disabled:
continue

device_class = entity.device_class or entity.original_device_class
if device_class != SensorDeviceClass.BATTERY:
continue

if entity.unit_of_measurement != PERCENTAGE:
continue

self.wrapped_battery = entity_registry.async_get(entity.entity_id)

device_entry = device_registry.async_get(device_id)
Expand Down
23 changes: 13 additions & 10 deletions custom_components/battery_notes/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
CONF_NAME,
CONF_DEVICE_ID,
STATE_UNAVAILABLE,
STATE_UNKNOWN,
PERCENTAGE,
)

Expand All @@ -62,6 +63,7 @@
ATTR_BATTERY_LOW_THRESHOLD,
)

from .common import isfloat
from .device import BatteryNotesDevice
from .coordinator import BatteryNotesCoordinator

Expand Down Expand Up @@ -259,6 +261,7 @@ def __init__(
self.device = device
self.enable_replaced = enable_replaced

self._device_id = coordinator.device_id
if coordinator.device_id and (
device_entry := device_registry.async_get(coordinator.device_id)
):
Expand All @@ -273,14 +276,6 @@ def __init__(
device.wrapped_battery.entity_category if device.wrapped_battery else None
)

self._device_id = coordinator.device_id
if coordinator.device_id and (
device_entry := device_registry.async_get(coordinator.device_id)
):
self._attr_device_info = DeviceInfo(
connections=device_entry.connections,
identifiers=device_entry.identifiers,
)
self._attr_entity_category = entity_category
self._attr_unique_id = unique_id
self._battery_entity_id = (
Expand All @@ -302,8 +297,15 @@ def async_state_changed_listener(
return

if (
wrapped_battery_state := self.hass.states.get(self._battery_entity_id)
) is None or wrapped_battery_state.state == STATE_UNAVAILABLE:
(wrapped_battery_state := self.hass.states.get(self._battery_entity_id))
is None
or wrapped_battery_state.state
in [
STATE_UNAVAILABLE,
STATE_UNKNOWN,
]
or not isfloat(wrapped_battery_state.state)
):
self._attr_native_value = None
self._attr_available = False
self.async_write_ha_state()
Expand All @@ -312,6 +314,7 @@ def async_state_changed_listener(
self._attr_available = True

self._attr_native_value = round(float(wrapped_battery_state.state), 1)

self._wrapped_attributes = wrapped_battery_state.attributes

self.async_write_ha_state()
Expand Down

0 comments on commit 9c7ac6a

Please sign in to comment.