From 81312d062b36a731c1a864c6524d8b8335154923 Mon Sep 17 00:00:00 2001 From: Olen Date: Tue, 20 Aug 2019 14:45:10 +0200 Subject: [PATCH] Another - and hopefully complete - PR (#14) * - Added "eta" and "location" - Fixed "end" - Fixed tzinfo - Updated ical_updater to allow HACS-users to update * Ensure end is set to something sane * Fix typo --- custom_components/ical/sensor.py | 21 +++++++++++++++++---- ical_updater.json | 4 ++-- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/custom_components/ical/sensor.py b/custom_components/ical/sensor.py index 18b8c59..5f1dbe3 100755 --- a/custom_components/ical/sensor.py +++ b/custom_components/ical/sensor.py @@ -60,7 +60,6 @@ def dateparser(calendar, date): if isinstance(event['DTSTART'].dt, dt.date): start = arrow.get(event['DTSTART'].dt) - start = start.replace(tzinfo='local') else: start = event['DTSTART'].dt @@ -76,10 +75,16 @@ def dateparser(calendar, date): if 'DTEND' in event: if isinstance(event['DTEND'].dt, dt.date): end = arrow.get(event['DTEND'].dt) - end = end.replace(tzinfo='local') else: end = event['DTEND'].dt event_dict['end'] = end + else: + # Use "start" as end if no end is set + event_dict['end'] = start + + # Add location if present + if 'LOCATION' in event: + event_dict['location'] = event['LOCATION'] events.append(event_dict) @@ -137,8 +142,10 @@ def update(self): # shouldn't change, so we should really prepopulate them. self._event_attributes = { 'name': None, + 'location': None, 'start': None, - 'end': None + 'end': None, + 'eta': None } # Get the data self.data_object.update() @@ -148,8 +155,13 @@ def update(self): val = event_list[self._eventno] start = val['start'].datetime self._event_attributes['start'] = start + end = val['end'].datetime + self._event_attributes['end'] = end + location = val.get('location', '') + self._event_attributes['location'] = location name = val.get('name', 'unknown') self._event_attributes['name'] = name + self._event_attributes['eta'] = (start - dt.datetime.now(start.tzinfo) + timedelta(days=1)).days self._state = "{} - {}".format( name, start.strftime("%-d %B %Y %H:%M") @@ -184,6 +196,7 @@ def update(self): self.data = events - except requests.exceptions.RequestException: + except requests.exceptions.RequestException as e: _LOGGER.error("Error fetching data: %s", self._request) + _LOGGER.error(e) self.data = None diff --git a/ical_updater.json b/ical_updater.json index 8dbc8f0..afe2cea 100644 --- a/ical_updater.json +++ b/ical_updater.json @@ -1,7 +1,7 @@ { "sensor.ical": { - "updated_at": "2019-04-25", - "version": "0.2", + "updated_at": "2019-08-20", + "version": "0.3", "local_location": "/custom_components/ical/__init__.py", "remote_location": "https://raw.githubusercontent.com/tybritten/ical-sensor-homeassistant/master/__init__.py", "visit_repo": "https://github.com/tybritten/ical-sensor-homeassistant/",