2019-02-14 16:01:46 +01:00
|
|
|
"""Support for Google Calendar Search binary sensors."""
|
2022-01-03 13:14:02 +01:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2019-07-11 05:59:37 +02:00
|
|
|
import copy
|
2022-02-01 08:28:32 -08:00
|
|
|
from datetime import datetime, timedelta
|
2019-03-20 22:56:46 -07:00
|
|
|
import logging
|
2022-02-01 08:28:32 -08:00
|
|
|
from typing import Any
|
2017-05-19 16:39:13 +02:00
|
|
|
|
2021-03-02 10:02:04 +02:00
|
|
|
from httplib2 import ServerNotFoundError
|
2019-10-18 02:11:51 +02:00
|
|
|
|
2019-07-11 05:59:37 +02:00
|
|
|
from homeassistant.components.calendar import (
|
2019-07-31 12:25:30 -07:00
|
|
|
ENTITY_ID_FORMAT,
|
|
|
|
CalendarEventDevice,
|
|
|
|
calculate_offset,
|
|
|
|
is_offset_reached,
|
|
|
|
)
|
2021-02-08 06:24:48 -05:00
|
|
|
from homeassistant.const import CONF_DEVICE_ID, CONF_ENTITIES, CONF_NAME, CONF_OFFSET
|
2022-01-03 13:14:02 +01:00
|
|
|
from homeassistant.core import HomeAssistant
|
2019-07-11 05:59:37 +02:00
|
|
|
from homeassistant.helpers.entity import generate_entity_id
|
2022-01-03 13:14:02 +01:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
|
|
|
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
2022-02-26 16:19:45 -08:00
|
|
|
from homeassistant.util import Throttle
|
2017-05-19 16:39:13 +02:00
|
|
|
|
2019-03-20 22:56:46 -07:00
|
|
|
from . import (
|
2019-07-31 12:25:30 -07:00
|
|
|
CONF_CAL_ID,
|
|
|
|
CONF_IGNORE_AVAILABILITY,
|
|
|
|
CONF_SEARCH,
|
|
|
|
CONF_TRACK,
|
2022-02-26 16:19:45 -08:00
|
|
|
DATA_SERVICE,
|
2019-07-31 12:25:30 -07:00
|
|
|
DEFAULT_CONF_OFFSET,
|
2022-02-26 16:19:45 -08:00
|
|
|
DOMAIN,
|
2019-07-31 12:25:30 -07:00
|
|
|
)
|
2022-02-26 16:19:45 -08:00
|
|
|
from .api import GoogleCalendarService
|
2019-03-20 22:56:46 -07:00
|
|
|
|
2017-05-19 16:39:13 +02:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
DEFAULT_GOOGLE_SEARCH_PARAMS = {
|
2019-07-31 12:25:30 -07:00
|
|
|
"orderBy": "startTime",
|
|
|
|
"singleEvents": True,
|
2017-05-19 16:39:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
MIN_TIME_BETWEEN_UPDATES = timedelta(minutes=15)
|
|
|
|
|
2022-02-26 15:27:08 -08:00
|
|
|
# Events have a transparency that determine whether or not they block time on calendar.
|
|
|
|
# When an event is opaque, it means "Show me as busy" which is the default. Events that
|
|
|
|
# are not opaque are ignored by default.
|
|
|
|
TRANSPARENCY = "transparency"
|
|
|
|
OPAQUE = "opaque"
|
|
|
|
|
2017-05-19 16:39:13 +02:00
|
|
|
|
2022-01-03 13:14:02 +01:00
|
|
|
def setup_platform(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
config: ConfigType,
|
|
|
|
add_entities: AddEntitiesCallback,
|
|
|
|
disc_info: DiscoveryInfoType | None = None,
|
|
|
|
) -> None:
|
2017-05-19 16:39:13 +02:00
|
|
|
"""Set up the calendar platform for event devices."""
|
|
|
|
if disc_info is None:
|
|
|
|
return
|
|
|
|
|
2017-06-16 22:44:14 +03:00
|
|
|
if not any(data[CONF_TRACK] for data in disc_info[CONF_ENTITIES]):
|
2017-05-19 16:39:13 +02:00
|
|
|
return
|
|
|
|
|
2022-02-26 16:19:45 -08:00
|
|
|
calendar_service = hass.data[DOMAIN][DATA_SERVICE]
|
2019-07-11 05:59:37 +02:00
|
|
|
entities = []
|
|
|
|
for data in disc_info[CONF_ENTITIES]:
|
|
|
|
if not data[CONF_TRACK]:
|
|
|
|
continue
|
|
|
|
entity_id = generate_entity_id(
|
2019-07-31 12:25:30 -07:00
|
|
|
ENTITY_ID_FORMAT, data[CONF_DEVICE_ID], hass=hass
|
|
|
|
)
|
2019-07-11 05:59:37 +02:00
|
|
|
entity = GoogleCalendarEventDevice(
|
2019-07-31 12:25:30 -07:00
|
|
|
calendar_service, disc_info[CONF_CAL_ID], data, entity_id
|
|
|
|
)
|
2019-07-11 05:59:37 +02:00
|
|
|
entities.append(entity)
|
|
|
|
|
|
|
|
add_entities(entities, True)
|
2017-05-19 16:39:13 +02:00
|
|
|
|
|
|
|
|
|
|
|
class GoogleCalendarEventDevice(CalendarEventDevice):
|
|
|
|
"""A calendar event device."""
|
|
|
|
|
2022-02-01 08:28:32 -08:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
calendar_service: GoogleCalendarService,
|
|
|
|
calendar_id: str,
|
|
|
|
data: dict[str, Any],
|
|
|
|
entity_id: str,
|
|
|
|
) -> None:
|
2017-05-19 16:39:13 +02:00
|
|
|
"""Create the Calendar event device."""
|
2019-07-11 05:59:37 +02:00
|
|
|
self.data = GoogleCalendarData(
|
2019-07-31 12:25:30 -07:00
|
|
|
calendar_service,
|
2022-02-01 08:28:32 -08:00
|
|
|
calendar_id,
|
2019-07-31 12:25:30 -07:00
|
|
|
data.get(CONF_SEARCH),
|
2022-02-01 08:28:32 -08:00
|
|
|
data.get(CONF_IGNORE_AVAILABILITY, False),
|
2019-07-31 12:25:30 -07:00
|
|
|
)
|
2022-02-01 08:28:32 -08:00
|
|
|
self._event: dict[str, Any] | None = None
|
|
|
|
self._name: str = data[CONF_NAME]
|
2019-07-11 05:59:37 +02:00
|
|
|
self._offset = data.get(CONF_OFFSET, DEFAULT_CONF_OFFSET)
|
|
|
|
self._offset_reached = False
|
|
|
|
self.entity_id = entity_id
|
|
|
|
|
|
|
|
@property
|
2022-02-01 08:28:32 -08:00
|
|
|
def extra_state_attributes(self) -> dict[str, bool]:
|
2019-07-11 05:59:37 +02:00
|
|
|
"""Return the device state attributes."""
|
2019-07-31 12:25:30 -07:00
|
|
|
return {"offset_reached": self._offset_reached}
|
2019-07-11 05:59:37 +02:00
|
|
|
|
|
|
|
@property
|
2022-02-01 08:28:32 -08:00
|
|
|
def event(self) -> dict[str, Any] | None:
|
2019-07-11 05:59:37 +02:00
|
|
|
"""Return the next upcoming event."""
|
|
|
|
return self._event
|
|
|
|
|
|
|
|
@property
|
2022-02-01 08:28:32 -08:00
|
|
|
def name(self) -> str:
|
2019-07-11 05:59:37 +02:00
|
|
|
"""Return the name of the entity."""
|
|
|
|
return self._name
|
2017-05-19 16:39:13 +02:00
|
|
|
|
2022-02-01 08:28:32 -08:00
|
|
|
async def async_get_events(
|
|
|
|
self, hass: HomeAssistant, start_date: datetime, end_date: datetime
|
|
|
|
) -> list[dict[str, Any]]:
|
2018-06-15 11:16:31 -04:00
|
|
|
"""Get all events in a specific time frame."""
|
|
|
|
return await self.data.async_get_events(hass, start_date, end_date)
|
|
|
|
|
2022-02-01 08:28:32 -08:00
|
|
|
def update(self) -> None:
|
2019-07-11 05:59:37 +02:00
|
|
|
"""Update event data."""
|
|
|
|
self.data.update()
|
|
|
|
event = copy.deepcopy(self.data.event)
|
|
|
|
if event is None:
|
|
|
|
self._event = event
|
|
|
|
return
|
|
|
|
event = calculate_offset(event, self._offset)
|
|
|
|
self._offset_reached = is_offset_reached(event)
|
|
|
|
self._event = event
|
|
|
|
|
2017-05-19 16:39:13 +02:00
|
|
|
|
2018-07-20 11:45:20 +03:00
|
|
|
class GoogleCalendarData:
|
2017-05-19 16:39:13 +02:00
|
|
|
"""Class to utilize calendar service object to get next event."""
|
|
|
|
|
2022-02-01 08:28:32 -08:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
calendar_service: GoogleCalendarService,
|
|
|
|
calendar_id: str,
|
|
|
|
search: str | None,
|
|
|
|
ignore_availability: bool,
|
|
|
|
) -> None:
|
2017-05-19 16:39:13 +02:00
|
|
|
"""Set up how we are going to search the google calendar."""
|
|
|
|
self.calendar_service = calendar_service
|
|
|
|
self.calendar_id = calendar_id
|
|
|
|
self.search = search
|
2018-04-06 21:48:50 +02:00
|
|
|
self.ignore_availability = ignore_availability
|
2022-02-01 08:28:32 -08:00
|
|
|
self.event: dict[str, Any] | None = None
|
2017-05-19 16:39:13 +02:00
|
|
|
|
2022-02-26 15:27:08 -08:00
|
|
|
def _event_filter(self, event: dict[str, Any]) -> bool:
|
|
|
|
"""Return True if the event is visible."""
|
|
|
|
if self.ignore_availability:
|
|
|
|
return True
|
|
|
|
return event.get(TRANSPARENCY, OPAQUE) == OPAQUE
|
|
|
|
|
2022-02-01 08:28:32 -08:00
|
|
|
async def async_get_events(
|
|
|
|
self, hass: HomeAssistant, start_date: datetime, end_date: datetime
|
|
|
|
) -> list[dict[str, Any]]:
|
2018-06-15 11:16:31 -04:00
|
|
|
"""Get all events in a specific time frame."""
|
2022-02-01 08:28:32 -08:00
|
|
|
event_list: list[dict[str, Any]] = []
|
|
|
|
page_token: str | None = None
|
2021-08-17 05:20:16 +02:00
|
|
|
while True:
|
2022-02-26 16:19:45 -08:00
|
|
|
try:
|
|
|
|
items, page_token = await self.calendar_service.async_list_events(
|
|
|
|
self.calendar_id,
|
|
|
|
start_time=start_date,
|
|
|
|
end_time=end_date,
|
|
|
|
search=self.search,
|
|
|
|
page_token=page_token,
|
|
|
|
)
|
|
|
|
except ServerNotFoundError as err:
|
|
|
|
_LOGGER.error("Unable to connect to Google: %s", err)
|
|
|
|
return []
|
|
|
|
|
|
|
|
event_list.extend(filter(self._event_filter, items))
|
2021-08-17 05:20:16 +02:00
|
|
|
if not page_token:
|
|
|
|
break
|
|
|
|
return event_list
|
|
|
|
|
2018-06-15 11:16:31 -04:00
|
|
|
@Throttle(MIN_TIME_BETWEEN_UPDATES)
|
2022-02-01 08:28:32 -08:00
|
|
|
def update(self) -> None:
|
2018-06-15 11:16:31 -04:00
|
|
|
"""Get the latest data."""
|
2022-02-26 16:19:45 -08:00
|
|
|
try:
|
|
|
|
items, _ = self.calendar_service.list_events(
|
|
|
|
self.calendar_id, search=self.search
|
|
|
|
)
|
|
|
|
except ServerNotFoundError as err:
|
|
|
|
_LOGGER.error("Unable to connect to Google: %s", err)
|
2019-07-11 05:59:37 +02:00
|
|
|
return
|
2017-05-19 16:39:13 +02:00
|
|
|
|
2022-02-26 15:27:08 -08:00
|
|
|
valid_events = filter(self._event_filter, items)
|
|
|
|
self.event = next(valid_events, None)
|