forked from home-assistant/core
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a calendar entity to ReCollect Waste (home-assistant#85347)
* Add a calendar entity to ReCollect Waste * Simplify and ensure return None * Ensure end date is after start date
- Loading branch information
Showing
4 changed files
with
142 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
"""Support for ReCollect Waste calendars.""" | ||
from __future__ import annotations | ||
|
||
import datetime | ||
|
||
from aiorecollect.client import PickupEvent | ||
|
||
from homeassistant.components.calendar import CalendarEntity, CalendarEvent | ||
from homeassistant.config_entries import ConfigEntry | ||
from homeassistant.core import HomeAssistant, callback | ||
from homeassistant.helpers.entity_platform import AddEntitiesCallback | ||
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator | ||
|
||
from .const import DOMAIN | ||
from .entity import ReCollectWasteEntity | ||
from .util import async_get_pickup_type_names | ||
|
||
|
||
@callback | ||
def async_get_calendar_event_from_pickup_event( | ||
entry: ConfigEntry, pickup_event: PickupEvent | ||
) -> CalendarEvent: | ||
"""Get a HASS CalendarEvent from an aiorecollect PickupEvent.""" | ||
pickup_type_string = ", ".join( | ||
async_get_pickup_type_names(entry, pickup_event.pickup_types) | ||
) | ||
return CalendarEvent( | ||
summary="ReCollect Waste Pickup", | ||
description=f"Pickup types: {pickup_type_string}", | ||
location=pickup_event.area_name, | ||
start=pickup_event.date, | ||
end=pickup_event.date + datetime.timedelta(days=1), | ||
) | ||
|
||
|
||
async def async_setup_entry( | ||
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback | ||
) -> None: | ||
"""Set up ReCollect Waste sensors based on a config entry.""" | ||
coordinator: DataUpdateCoordinator[list[PickupEvent]] = hass.data[DOMAIN][ | ||
entry.entry_id | ||
] | ||
|
||
async_add_entities([ReCollectWasteCalendar(coordinator, entry)]) | ||
|
||
|
||
class ReCollectWasteCalendar(ReCollectWasteEntity, CalendarEntity): | ||
"""Define a ReCollect Waste calendar.""" | ||
|
||
_attr_icon = "mdi:delete-empty" | ||
|
||
def __init__( | ||
self, | ||
coordinator: DataUpdateCoordinator[list[PickupEvent]], | ||
entry: ConfigEntry, | ||
) -> None: | ||
"""Initialize the ReCollect Waste entity.""" | ||
super().__init__(coordinator, entry) | ||
|
||
self._attr_unique_id = f"{self._identifier}_calendar" | ||
self._event: CalendarEvent | None = None | ||
|
||
@property | ||
def event(self) -> CalendarEvent | None: | ||
"""Return the next upcoming event.""" | ||
return self._event | ||
|
||
@callback | ||
def _handle_coordinator_update(self) -> None: | ||
"""Handle updated data from the coordinator.""" | ||
try: | ||
current_event = next( | ||
event | ||
for event in self.coordinator.data | ||
if event.date >= datetime.date.today() | ||
) | ||
except StopIteration: | ||
self._event = None | ||
else: | ||
self._event = async_get_calendar_event_from_pickup_event( | ||
self._entry, current_event | ||
) | ||
|
||
super()._handle_coordinator_update() | ||
|
||
async def async_get_events( | ||
self, | ||
hass: HomeAssistant, | ||
start_date: datetime.datetime, | ||
end_date: datetime.datetime, | ||
) -> list[CalendarEvent]: | ||
"""Return calendar events within a datetime range.""" | ||
return [ | ||
async_get_calendar_event_from_pickup_event(self._entry, event) | ||
for event in self.coordinator.data | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
"""Define ReCollect Waste utilities.""" | ||
from aiorecollect.client import PickupType | ||
|
||
from homeassistant.config_entries import ConfigEntry | ||
from homeassistant.const import CONF_FRIENDLY_NAME | ||
from homeassistant.core import callback | ||
|
||
|
||
@callback | ||
def async_get_pickup_type_names( | ||
entry: ConfigEntry, pickup_types: list[PickupType] | ||
) -> list[str]: | ||
"""Return proper pickup type names from their associated objects.""" | ||
return [ | ||
t.friendly_name | ||
if entry.options.get(CONF_FRIENDLY_NAME) and t.friendly_name | ||
else t.name | ||
for t in pickup_types | ||
] |