core/homeassistant/components/demo/calendar.py

90 lines
2.8 KiB
Python
Raw Normal View History

"""Demo platform that has two fake binary sensors."""
import copy
import homeassistant.util.dt as dt_util
from homeassistant.components.calendar import CalendarEventDevice, get_date
2017-05-19 14:39:13 +00:00
def setup_platform(hass, config, add_entities, discovery_info=None):
2017-05-19 14:39:13 +00:00
"""Set up the Demo Calendar platform."""
calendar_data_future = DemoGoogleCalendarDataFuture()
calendar_data_current = DemoGoogleCalendarDataCurrent()
2019-07-31 19:25:30 +00:00
add_entities(
[
DemoGoogleCalendar(hass, calendar_data_future, "Calendar 1"),
DemoGoogleCalendar(hass, calendar_data_current, "Calendar 2"),
]
)
2017-05-19 14:39:13 +00:00
class DemoGoogleCalendarData:
2017-05-19 14:39:13 +00:00
"""Representation of a Demo Calendar element."""
event = None
2017-05-19 14:39:13 +00:00
async def async_get_events(self, hass, start_date, end_date):
"""Get all events in a specific time frame."""
event = copy.copy(self.event)
2019-07-31 19:25:30 +00:00
event["title"] = event["summary"]
event["start"] = get_date(event["start"]).isoformat()
event["end"] = get_date(event["end"]).isoformat()
return [event]
2017-05-19 14:39:13 +00:00
class DemoGoogleCalendarDataFuture(DemoGoogleCalendarData):
"""Representation of a Demo Calendar for a future event."""
def __init__(self):
"""Set the event to a future event."""
2019-07-31 19:25:30 +00:00
one_hour_from_now = dt_util.now() + dt_util.dt.timedelta(minutes=30)
2017-05-19 14:39:13 +00:00
self.event = {
2019-07-31 19:25:30 +00:00
"start": {"dateTime": one_hour_from_now.isoformat()},
"end": {
"dateTime": (
one_hour_from_now + dt_util.dt.timedelta(minutes=60)
).isoformat()
2017-05-19 14:39:13 +00:00
},
2019-07-31 19:25:30 +00:00
"summary": "Future Event",
2017-05-19 14:39:13 +00:00
}
class DemoGoogleCalendarDataCurrent(DemoGoogleCalendarData):
"""Representation of a Demo Calendar for a current event."""
def __init__(self):
"""Set the event data."""
2019-07-31 19:25:30 +00:00
middle_of_event = dt_util.now() - dt_util.dt.timedelta(minutes=30)
2017-05-19 14:39:13 +00:00
self.event = {
2019-07-31 19:25:30 +00:00
"start": {"dateTime": middle_of_event.isoformat()},
"end": {
"dateTime": (
middle_of_event + dt_util.dt.timedelta(minutes=60)
).isoformat()
2017-05-19 14:39:13 +00:00
},
2019-07-31 19:25:30 +00:00
"summary": "Current Event",
2017-05-19 14:39:13 +00:00
}
class DemoGoogleCalendar(CalendarEventDevice):
"""Representation of a Demo Calendar element."""
def __init__(self, hass, calendar_data, name):
"""Initialize demo calendar."""
2017-05-19 14:39:13 +00:00
self.data = calendar_data
self._name = name
@property
def event(self):
"""Return the next upcoming event."""
return self.data.event
@property
def name(self):
"""Return the name of the entity."""
return self._name
async def async_get_events(self, hass, start_date, end_date):
"""Return calendar events within a datetime range."""
return await self.data.async_get_events(hass, start_date, end_date)