Skip to content

Commit

Permalink
Another - and hopefully complete - PR (#14)
Browse files Browse the repository at this point in the history
* - 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
  • Loading branch information
Olen authored and tybritten committed Aug 20, 2019
1 parent 64b802e commit 81312d0
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
21 changes: 17 additions & 4 deletions custom_components/ical/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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)

Expand Down Expand Up @@ -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()
Expand All @@ -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")
Expand Down Expand Up @@ -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
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": "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/",
Expand Down

0 comments on commit 81312d0

Please sign in to comment.