Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update coordinator handling #12

Merged
merged 1 commit into from
May 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 11 additions & 18 deletions custom_components/bms_ble/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import ATTR_BATTERY_CHARGING
from homeassistant.core import HomeAssistant, callback
from homeassistant.core import HomeAssistant
from homeassistant.helpers.device_registry import format_mac
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.update_coordinator import CoordinatorEntity
Expand Down Expand Up @@ -43,25 +43,18 @@ def __init__(
self, bms: BTBmsCoordinator, descr: BinarySensorEntityDescription
) -> None:
"""Intialize BMS binary sensor."""
self._bms: BTBmsCoordinator = bms
self._attr_unique_id = f"{format_mac(self._bms.name)}-{descr.key}"
self._attr_unique_id = f"{format_mac(bms.name)}-{descr.key}"
self._attr_device_info = bms.device_info
self._attr_has_entity_name = True
self.entity_description = descr
super().__init__(self._bms)
super().__init__(bms)

@callback
def _handle_coordinator_update(self) -> None:
@property
def is_on(self) -> bool | None: # type: ignore
"""Handle updated data from the coordinator."""

if self._bms.data is None:
return

if self.entity_description.key in self._bms.data:
self._attr_is_on = bool(self._bms.data.get(self.entity_description.key))
self._attr_available = True
elif self._attr_available:
self._attr_available = False
LOGGER.info("No update available for sensor '%s'", self.entity_description.key)

self.async_write_ha_state()
LOGGER.debug(
"binary update handler for %s (%s)",
self.entity_description.key,
str(self.coordinator.data.get(self.entity_description.key)),
)
return bool(self.coordinator.data.get(self.entity_description.key))
26 changes: 7 additions & 19 deletions custom_components/bms_ble/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
UnitOfTemperature,
UnitOfTime,
)
from homeassistant.core import HomeAssistant, callback
from homeassistant.core import HomeAssistant
from homeassistant.helpers.device_registry import format_mac
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.update_coordinator import CoordinatorEntity
Expand All @@ -34,7 +34,6 @@
ATTR_RSSI,
ATTR_RUNTIME,
DOMAIN,
LOGGER,
)
from .coordinator import BTBmsCoordinator

Expand Down Expand Up @@ -130,23 +129,12 @@ class BMSSensor(CoordinatorEntity[BTBmsCoordinator], SensorEntity): # type: ign

def __init__(self, bms: BTBmsCoordinator, descr: SensorEntityDescription) -> None:
"""Intitialize the BMS sensor."""
self._bms: BTBmsCoordinator = bms
self._attr_unique_id = f"{format_mac(self._bms.name)}-{descr.key}"
self._attr_unique_id = f"{format_mac(bms.name)}-{descr.key}"
self._attr_device_info = bms.device_info
self.entity_description = descr
super().__init__(self._bms)
super().__init__(bms)

@callback
def _handle_coordinator_update(self) -> None:
"""Handle updated data from the coordinator."""
if self._bms.data is None:
return

if self.entity_description.key in self._bms.data:
self._attr_native_value = self._bms.data.get(self.entity_description.key)
self._attr_available = True
elif self._attr_available:
self._attr_available = False
LOGGER.info("No update available for sensor '%s'", self.entity_description.key)

self.async_write_ha_state()
@property
def native_value(self) -> int | float | None: # type: ignore
"""Return the sensor value."""
return self.coordinator.data.get(self.entity_description.key)