Skip to content

Commit

Permalink
Merge pull request #26 from swartjean/issue-24
Browse files Browse the repository at this point in the history
Add polling to calendar entities (#24)
  • Loading branch information
swartjean authored Oct 13, 2022
2 parents a8f9d9a + 39deb83 commit 6e3c025
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
64 changes: 64 additions & 0 deletions custom_components/eskom_loadshedding/calendar.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,20 @@
import re

from homeassistant.components.calendar import CalendarEntity, CalendarEvent
from homeassistant.core import callback

from .const import (
DOMAIN,
LOCAL_EVENTS_ID,
LOCAL_EVENTS_NAME,
LOCAL_SCHEDULE_ID,
LOCAL_SCHEDULE_NAME,
DEFAULT_CALENDAR_SCAN_PERIOD,
)
from .entity import EskomEntity

SCAN_INTERVAL = timedelta(seconds=DEFAULT_CALENDAR_SCAN_PERIOD)


async def async_setup_entry(hass, entry, async_add_devices):
"""Setup calendar platform."""
Expand Down Expand Up @@ -56,6 +60,15 @@ def name(self):
"""Return the friendly name of the sensor."""
return self.friendly_name

@property
def should_poll(self) -> bool:
"""Enable polling for the entity.
The coordinator is used to query the API, but polling is used to update
the entity state more frequently.
"""
return True

@property
def event(self):
# Return the next event
Expand All @@ -66,6 +79,21 @@ def event(self):
next_event_end = datetime.strptime(events[0]["end"], time_format)
return CalendarEvent(next_event_start, next_event_end, events[0]["note"])

@callback
def _handle_coordinator_update(self) -> None:
"""Handle updated data from the coordinator."""
# Copy the state from the coordinator to this entity
events = self.coordinator.data.get("area_information", {}).get("events", {})
if events:
time_format = "%Y-%m-%dT%H:%M:%S%z"
next_event_start = datetime.strptime(events[0]["start"], time_format)
next_event_end = datetime.strptime(events[0]["end"], time_format)
self._event = CalendarEvent(
next_event_start, next_event_end, events[0]["note"]
)

super()._handle_coordinator_update()

async def async_get_events(
self,
hass,
Expand All @@ -87,6 +115,12 @@ async def async_get_events(
else:
return []

async def async_update(self) -> None:
"""Disable update behavior.
Event updates are performed through the coordinator callback.
This is simply used to evaluate the entity state
"""


class LoadsheddingLocalScheduleCalendar(EskomEntity, CalendarEntity):
"""Loadshedding Local Schedule Calendar class."""
Expand All @@ -109,6 +143,15 @@ def name(self):
"""Return the friendly name of the sensor."""
return self.friendly_name

@property
def should_poll(self) -> bool:
"""Enable polling for the entity.
The coordinator is used to query the API, but polling is used to update
the entity state more frequently.
"""
return True

@property
def event(self):
# Return the next event
Expand All @@ -119,6 +162,21 @@ def event(self):
next_event_end = datetime.strptime(events[0]["end"], time_format)
return CalendarEvent(next_event_start, next_event_end, events[0]["note"])

@callback
def _handle_coordinator_update(self) -> None:
"""Handle updated data from the coordinator."""
# Copy the state from the coordinator to this entity
events = self.coordinator.data.get("area_information", {}).get("events", {})
if events:
time_format = "%Y-%m-%dT%H:%M:%S%z"
next_event_start = datetime.strptime(events[0]["start"], time_format)
next_event_end = datetime.strptime(events[0]["end"], time_format)
self._event = CalendarEvent(
next_event_start, next_event_end, events[0]["note"]
)

super()._handle_coordinator_update()

async def async_get_events(
self,
hass,
Expand Down Expand Up @@ -159,3 +217,9 @@ async def async_get_events(
return calendar_events
else:
return []

async def async_update(self) -> None:
"""Disable update behavior.
Event updates are performed through the coordinator callback.
This is simply used to evaluate the entity state
"""
1 change: 1 addition & 0 deletions custom_components/eskom_loadshedding/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
# Defaults
DEFAULT_SCAN_PERIOD = 7200
MIN_SCAN_PERIOD = 1800
DEFAULT_CALENDAR_SCAN_PERIOD = 30

# Entity Identifiers
LOCAL_EVENTS_ID = "calendar_local_events"
Expand Down

0 comments on commit 6e3c025

Please sign in to comment.