Skip to content

Commit

Permalink
Feat: Teach calendars timezones
Browse files Browse the repository at this point in the history
Calendars should have timezones. This allows for managing properties
that are in a different timezone that the HA instance. Most useful for
folks that may not have an HA instance at the property but are still
wanting to do automations related to the property.

Issue: #55
Signed-off-by: Andrew Grimberg <tykeal@bardicgrove.org>
  • Loading branch information
tykeal committed Jan 22, 2022
1 parent 13d7b3b commit 82a54a4
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 6 deletions.
28 changes: 22 additions & 6 deletions custom_components/rental_control/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import logging
from datetime import datetime
from datetime import timedelta
from zoneinfo import ZoneInfo

import homeassistant.helpers.config_validation as cv
import icalendar
Expand All @@ -34,6 +35,7 @@
from .const import CONF_EVENT_PREFIX
from .const import CONF_IGNORE_NON_RESERVED
from .const import CONF_MAX_EVENTS
from .const import CONF_TIMEZONE
from .const import DOMAIN
from .const import PLATFORMS

Expand Down Expand Up @@ -120,6 +122,13 @@ def __init__(self, hass, config):
self.name = config.get(CONF_NAME)
self.event_prefix = config.get(CONF_EVENT_PREFIX)
self.url = config.get(CONF_URL)
# Early versions did not have this variable, as such it may not be
# set, this should guard against issues until we're certain we can
# remove this guard.
try:
self.timezone = ZoneInfo(config.get(CONF_TIMEZONE))
except TypeError:
self.timezone = dt.DEFAULT_TIME_ZONE
# our config flow guarantees that checkin and checkout are valid times
# just use cv.time to get the parsed time object
self.checkin = cv.time(config.get(CONF_CHECKIN))
Expand Down Expand Up @@ -194,6 +203,13 @@ def update_config(self, config):
"""Update config entries."""
self.name = config.get(CONF_NAME)
self.url = config.get(CONF_URL)
# Early versions did not have this variable, as such it may not be
# set, this should guard against issues until we're certain
# we can remove this guard.
try:
self.timezone = ZoneInfo(config.get(CONF_TIMEZONE))
except TypeError:
self.timezone = dt.DEFAULT_TIME_ZONE
self.event_prefix = config.get(CONF_EVENT_PREFIX)
# our config flow guarantees that checkin and checkout are valid times
# just use cv.time to get the parsed time object
Expand Down Expand Up @@ -259,7 +275,7 @@ def _ical_parser(self, calendar, from_date, to_date):

_LOGGER.debug("DTSTART in event: %s", event["DTSTART"].dt)
dtstart = datetime.combine(
event["DTSTART"].dt, self.checkin, dt.DEFAULT_TIME_ZONE
event["DTSTART"].dt, self.checkin, self.timezone
)

start = dtstart
Expand All @@ -269,7 +285,7 @@ def _ical_parser(self, calendar, from_date, to_date):
else:
_LOGGER.debug("DTEND in event: %s", event["DTEND"].dt)
dtend = datetime.combine(
event["DTEND"].dt, self.checkout, dt.DEFAULT_TIME_ZONE
event["DTEND"].dt, self.checkout, self.timezone
)
end = dtend

Expand Down Expand Up @@ -304,13 +320,13 @@ def _ical_event_dict(self, start, end, from_date, event):
"Start: %s Tzinfo: %s Default: %s StartAs %s",
str(start),
str(start.tzinfo),
dt.DEFAULT_TIME_ZONE,
start.astimezone(dt.DEFAULT_TIME_ZONE),
self.timezone,
start.astimezone(self.timezone),
)
event_dict = {
"summary": event.get("SUMMARY", "Unknown"),
"start": start.astimezone(dt.DEFAULT_TIME_ZONE),
"end": end.astimezone(dt.DEFAULT_TIME_ZONE),
"start": start.astimezone(self.timezone),
"end": end.astimezone(self.timezone),
"location": event.get("LOCATION"),
"description": event.get("DESCRIPTION"),
"all_day": self.all_day,
Expand Down
11 changes: 11 additions & 0 deletions custom_components/rental_control/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
from homeassistant.core import callback
from homeassistant.core import HomeAssistant
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from homeassistant.util import dt
from pytz import common_timezones
from voluptuous.schema_builder import ALLOW_EXTRA

from .const import CONF_CHECKIN
Expand All @@ -27,6 +29,7 @@
from .const import CONF_LOCK_ENTRY
from .const import CONF_MAX_EVENTS
from .const import CONF_START_SLOT
from .const import CONF_TIMEZONE
from .const import DEFAULT_CHECKIN
from .const import DEFAULT_CHECKOUT
from .const import DEFAULT_DAYS
Expand All @@ -39,6 +42,9 @@

_LOGGER = logging.getLogger(__name__)

sorted_tz = common_timezones
sorted_tz.sort()


@config_entries.HANDLERS.register(DOMAIN)
class RentalControlFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
Expand All @@ -53,6 +59,7 @@ class RentalControlFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
CONF_IGNORE_NON_RESERVED: True,
CONF_EVENT_PREFIX: DEFAULT_EVENT_PREFIX,
CONF_MAX_EVENTS: DEFAULT_MAX_EVENTS,
CONF_TIMEZONE: str(dt.DEFAULT_TIME_ZONE),
CONF_VERIFY_SSL: True,
}

Expand Down Expand Up @@ -154,6 +161,10 @@ def _get_default(key: str, fallback_default: Any = None) -> None:
{
vol.Required(CONF_NAME, default=_get_default(CONF_NAME)): cv.string,
vol.Required(CONF_URL, default=_get_default(CONF_URL)): cv.string,
vol.Optional(
CONF_TIMEZONE,
default=_get_default(CONF_TIMEZONE, str(dt.DEFAULT_TIME_ZONE)),
): vol.In(sorted_tz),
vol.Optional(
CONF_EVENT_PREFIX,
default=_get_default(CONF_EVENT_PREFIX, DEFAULT_EVENT_PREFIX),
Expand Down
1 change: 1 addition & 0 deletions custom_components/rental_control/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
CONF_LOCK_ENTRY = "keymaster_entry_id"
CONF_MAX_EVENTS = "max_events"
CONF_START_SLOT = "start_slot"
CONF_TIMEZONE = "timezone"

# Defaults
DEFAULT_CHECKIN = "16:00"
Expand Down
2 changes: 2 additions & 0 deletions custom_components/rental_control/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"max_events": "Number of event sensors to create",
"name": "Calendar name",
"start_slot": "Starting lock slot for management",
"timezone": "Calendar Timezone",
"url": "Calendar URL",
"verify_ssl": "Verify SSL certificates"
}
Expand All @@ -45,6 +46,7 @@
"max_events": "Number of event sensors to create",
"name": "Calendar name",
"start_slot": "Starting lock slot for management",
"timezone": "Calendar Timezone",
"url": "Calendar URL",
"verify_ssl": "Verify SSL certificates"
}
Expand Down
2 changes: 2 additions & 0 deletions custom_components/rental_control/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"max_events": "Number of event sensors to create",
"name": "Calendar name",
"start_slot": "Starting lock slot for management",
"timezone": "Calendar Timezone",
"url": "Calendar URL",
"verify_ssl": "Verify SSL certificates"
}
Expand All @@ -45,6 +46,7 @@
"max_events": "Number of event sensors to create",
"name": "Calendar name",
"start_slot": "Starting lock slot for management",
"timezone": "Calendar Timezone",
"url": "Calendar URL",
"verify_ssl": "Verify SSL certificates"
}
Expand Down

0 comments on commit 82a54a4

Please sign in to comment.