Skip to content

Commit

Permalink
Change to use new CalendarEntity/CalendarEvent Classes (#97)
Browse files Browse the repository at this point in the history
* Change to use new CalendarEntity/CalendarEvent Classes

Previous Entity Type has been deprecated.
  • Loading branch information
tiaanv committed Jul 18, 2022
1 parent f24e653 commit f5c17be
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 16 deletions.
15 changes: 10 additions & 5 deletions custom_components/ical/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from dateutil.tz import gettz, tzutc
import icalendar
import voluptuous as vol
from homeassistant.components.calendar import CalendarEvent

from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_NAME, CONF_URL, CONF_VERIFY_SSL
Expand Down Expand Up @@ -106,21 +107,25 @@ async def async_get_events(self, hass, start_date, end_date):

if event["start"] < end_date and event["end"] > start_date:
_LOGGER.debug("... and it has")
events.append(event)
# strongly type class fix
events.append(
CalendarEvent(event["start"], event["end"], event["summary"])
)
# events.append(event)
return events

@Throttle(MIN_TIME_BETWEEN_UPDATES)
async def update(self):
"""Update list of upcoming events."""
_LOGGER.debug("Running ICalEvents update for calendar %s", self.name)
parts = urlparse(self.url)
if parts.scheme == 'file':
if parts.scheme == "file":
with open(parts.path) as f:
text = f.read()
else:
if parts.scheme == 'webcal':
if parts.scheme == "webcal":
# There is a potential issue here if the real URL is http, not https
self.url = parts.geturl().replace('webcal', 'https', 1)
self.url = parts.geturl().replace("webcal", "https", 1)
session = async_get_clientsession(self.hass, verify_ssl=self.verify_ssl)
async with session.get(self.url) as response:
text = await response.text()
Expand Down Expand Up @@ -402,7 +407,7 @@ def _ical_date_fixer(self, indate, timezone="UTC"):
if not str(indate.tzinfo).startswith("tzfile"):
# _LOGGER.debug("Pytz indate: %s. replacing with tz %s", str(indate), str(gettz(str(indate.tzinfo))))
indate = indate.replace(tzinfo=gettz(str(indate.tzinfo)))
if str(indate.tzinfo).endswith('/UTC'):
if str(indate.tzinfo).endswith("/UTC"):
indate = indate.replace(tzinfo=tzutc)
# _LOGGER.debug("Tzinfo 2: %s", str(indate.tzinfo))

Expand Down
19 changes: 11 additions & 8 deletions custom_components/ical/calendar.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@

from homeassistant.components.calendar import (
ENTITY_ID_FORMAT,
CalendarEventDevice,
CalendarEntity,
CalendarEvent,
extract_offset,
get_date,
is_offset_reached,
Expand Down Expand Up @@ -36,7 +37,7 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
async_add_entities([calendar], True)


class ICalCalendarEventDevice(CalendarEventDevice):
class ICalCalendarEventDevice(CalendarEntity):
"""A device for getting the next Task from a WebDav Calendar."""

def __init__(self, hass, name, entity_id, ical_events):
Expand Down Expand Up @@ -78,9 +79,11 @@ async def async_update(self):
(summary, offset) = extract_offset(event["summary"], OFFSET)
event["summary"] = summary
self._offset_reached = is_offset_reached(event["start"], offset)
self._event = copy.deepcopy(event)
self._event["start"] = {}
self._event["end"] = {}
self._event["start"]["dateTime"] = event["start"].isoformat()
self._event["end"]["dateTime"] = event["end"].isoformat()
self._event["all_day"] = event["all_day"]
self._event = CalendarEvent(event["start"], event["end"], event["summary"])
# strongly typed class required.
# self._event = copy.deepcopy(event)
# self._event["start"] = {}
# self._event["end"] = {}
# self._event["start"]["dateTime"] = event["start"].isoformat()
# self._event["end"]["dateTime"] = event["end"].isoformat()
# self._event["all_day"] = event["all_day"]
2 changes: 1 addition & 1 deletion hacs.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"name": "iCal Sensor",
"homeassistant":"2022.4.0"
"homeassistant":"2022.5.0"
}
4 changes: 2 additions & 2 deletions ical_updater.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"sensor.ical": {
"updated_at": "2022-04-07",
"version": "1.5.0",
"updated_at": "2022-07-18",
"version": "1.6.0",
"local_location": "/custom_components/ics/__init__.py",
"remote_location": "https://raw.githubusercontent.com/tybritten/ical-sensor-homeassistant/master/__init__.py",
"visit_repo": "https://github.com/tybritten/ical-sensor-homeassistant/",
Expand Down

0 comments on commit f5c17be

Please sign in to comment.