Skip to content

Commit 9c7ac6a

Browse files
Battery fix (#675)
* Check for battery being percentage and enabled * Battery plus fixes * lint
1 parent 2641e96 commit 9c7ac6a

File tree

5 files changed

+35
-19
lines changed

5 files changed

+35
-19
lines changed

custom_components/battery_notes/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,9 @@ async def async_remove_entry(hass: HomeAssistant, config_entry: ConfigEntry) ->
171171
if "device_id" not in config_entry.data:
172172
return
173173

174+
if config_entry.entry_id not in hass.data[DOMAIN][DATA].devices:
175+
return
176+
174177
device: BatteryNotesDevice = hass.data[DOMAIN][DATA].devices[config_entry.entry_id]
175178
if not device:
176179
return

custom_components/battery_notes/binary_sensor.py

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
ATTR_PREVIOUS_BATTERY_LEVEL,
6060
)
6161

62+
from .common import isfloat
6263
from .device import BatteryNotesDevice
6364
from .coordinator import BatteryNotesCoordinator
6465

@@ -363,12 +364,3 @@ def extra_state_attributes(self) -> dict[str, str] | None:
363364
if super_attrs:
364365
attrs.update(super_attrs)
365366
return attrs
366-
367-
368-
def isfloat(num):
369-
"""Is the value a float."""
370-
try:
371-
float(num)
372-
return True
373-
except ValueError:
374-
return False
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
"""Common functions for battery_notes."""
2+
3+
4+
def isfloat(num):
5+
"""Is the value a float."""
6+
try:
7+
float(num)
8+
return True
9+
except ValueError:
10+
return False

custom_components/battery_notes/device.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
from homeassistant.const import (
2020
CONF_DEVICE_ID,
21+
PERCENTAGE,
2122
)
2223

2324
from .const import (
@@ -91,10 +92,17 @@ async def async_setup(self) -> bool:
9192
continue
9293
if not entity.platform or entity.platform == DOMAIN:
9394
continue
95+
96+
if entity.disabled:
97+
continue
98+
9499
device_class = entity.device_class or entity.original_device_class
95100
if device_class != SensorDeviceClass.BATTERY:
96101
continue
97102

103+
if entity.unit_of_measurement != PERCENTAGE:
104+
continue
105+
98106
self.wrapped_battery = entity_registry.async_get(entity.entity_id)
99107

100108
device_entry = device_registry.async_get(device_id)

custom_components/battery_notes/sensor.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
CONF_NAME,
4242
CONF_DEVICE_ID,
4343
STATE_UNAVAILABLE,
44+
STATE_UNKNOWN,
4445
PERCENTAGE,
4546
)
4647

@@ -62,6 +63,7 @@
6263
ATTR_BATTERY_LOW_THRESHOLD,
6364
)
6465

66+
from .common import isfloat
6567
from .device import BatteryNotesDevice
6668
from .coordinator import BatteryNotesCoordinator
6769

@@ -259,6 +261,7 @@ def __init__(
259261
self.device = device
260262
self.enable_replaced = enable_replaced
261263

264+
self._device_id = coordinator.device_id
262265
if coordinator.device_id and (
263266
device_entry := device_registry.async_get(coordinator.device_id)
264267
):
@@ -273,14 +276,6 @@ def __init__(
273276
device.wrapped_battery.entity_category if device.wrapped_battery else None
274277
)
275278

276-
self._device_id = coordinator.device_id
277-
if coordinator.device_id and (
278-
device_entry := device_registry.async_get(coordinator.device_id)
279-
):
280-
self._attr_device_info = DeviceInfo(
281-
connections=device_entry.connections,
282-
identifiers=device_entry.identifiers,
283-
)
284279
self._attr_entity_category = entity_category
285280
self._attr_unique_id = unique_id
286281
self._battery_entity_id = (
@@ -302,8 +297,15 @@ def async_state_changed_listener(
302297
return
303298

304299
if (
305-
wrapped_battery_state := self.hass.states.get(self._battery_entity_id)
306-
) is None or wrapped_battery_state.state == STATE_UNAVAILABLE:
300+
(wrapped_battery_state := self.hass.states.get(self._battery_entity_id))
301+
is None
302+
or wrapped_battery_state.state
303+
in [
304+
STATE_UNAVAILABLE,
305+
STATE_UNKNOWN,
306+
]
307+
or not isfloat(wrapped_battery_state.state)
308+
):
307309
self._attr_native_value = None
308310
self._attr_available = False
309311
self.async_write_ha_state()
@@ -312,6 +314,7 @@ def async_state_changed_listener(
312314
self._attr_available = True
313315

314316
self._attr_native_value = round(float(wrapped_battery_state.state), 1)
317+
315318
self._wrapped_attributes = wrapped_battery_state.attributes
316319

317320
self.async_write_ha_state()

0 commit comments

Comments
 (0)