Skip to content

Commit

Permalink
fix: Ensure refresh time is respected by making all sensor inherit co…
Browse files Browse the repository at this point in the history
…ordinator
  • Loading branch information
storm1er committed Oct 12, 2024
1 parent 85ad776 commit f3e4f18
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 13 deletions.
14 changes: 3 additions & 11 deletions custom_components/parcelsapp/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.update_coordinator import CoordinatorEntity

from .const import DOMAIN
from .coordinator import ParcelsAppCoordinator
Expand All @@ -22,14 +23,14 @@ async def async_setup_entry(
async_add_entities([ParcelsAppBinarySensor(coordinator)], True)


class ParcelsAppBinarySensor(BinarySensorEntity):
class ParcelsAppBinarySensor(CoordinatorEntity, BinarySensorEntity):
"""Representation of a Parcels App binary sensor."""

_attr_device_class = BinarySensorDeviceClass.CONNECTIVITY

def __init__(self, coordinator: ParcelsAppCoordinator) -> None:
"""Initialize the binary sensor."""
self.coordinator = coordinator
super().__init__(coordinator)
self._attr_unique_id = f"{DOMAIN}_status"
self._attr_name = "Parcels App Status"

Expand All @@ -50,12 +51,3 @@ def extra_state_attributes(self) -> dict[str, str]:
"response_code": status_data["response_code"],
}
return {}

@property
def available(self) -> bool:
"""Return True if entity is available."""
return self.coordinator.last_update_success

async def async_update(self) -> None:
"""Update the entity."""
await self.coordinator.async_request_refresh()
7 changes: 5 additions & 2 deletions custom_components/parcelsapp/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers import entity_registry as er
from homeassistant.helpers.update_coordinator import CoordinatorEntity

from .const import DOMAIN
from .coordinator import ParcelsAppCoordinator


async def async_setup_entry(
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
) -> None:
Expand Down Expand Up @@ -61,12 +63,13 @@ async def handle_remove_package(tracking_id: str):
# Store unsub functions to clean up later
hass.data[DOMAIN][entry.entry_id + "_unsub_dispatcher"] = [unsub_new_package, unsub_remove_package]

class ParcelsAppTrackingSensor(SensorEntity):

class ParcelsAppTrackingSensor(CoordinatorEntity, SensorEntity):
"""Representation of a Parcels App tracking sensor."""

def __init__(self, coordinator: ParcelsAppCoordinator, tracking_id: str, name: str = None) -> None:
"""Initialize the sensor."""
self.coordinator = coordinator
super().__init__(coordinator)
self.tracking_id = tracking_id
self._attr_unique_id = f"{DOMAIN}_tracking_{tracking_id}"
stored_name = self.coordinator.tracked_packages.get(tracking_id, {}).get("name")
Expand Down

0 comments on commit f3e4f18

Please sign in to comment.