File tree Expand file tree Collapse file tree 3 files changed +81
-1
lines changed Expand file tree Collapse file tree 3 files changed +81
-1
lines changed Original file line number Diff line number Diff line change 1
1
class Api
2
+ def url
3
+ Timeframe ::Application . config . local [ "#{ self . class . name . underscore } _url" ]
4
+ end
5
+
2
6
def fetch
3
- response = HTTParty . get ( Timeframe :: Application . config . local [ " #{ self . class . name . underscore } _url" ] , headers : headers )
7
+ response = HTTParty . get ( url , headers : headers )
4
8
5
9
return if response . code != 200
6
10
Original file line number Diff line number Diff line change
1
+ class HomeAssistantCalendarApi < Api
2
+ def initialize ( config = Timeframe ::Application . config . local )
3
+ @config = config
4
+ end
5
+
6
+ def prepare_response ( response )
7
+ start_time = ( Time . now - 1 . day ) . utc . iso8601
8
+ end_time = ( Time . now + 5 . days ) . utc . iso8601
9
+
10
+ out = [ ]
11
+
12
+ response . each do |calendar |
13
+ res = HTTParty . get ( "#{ url } /#{ calendar [ "entity_id" ] } ?start=#{ start_time } &end=#{ end_time } " , headers : headers )
14
+ config = Timeframe ::Application . config . local [ "calendars" ] . find { _1 [ "entity_id" ] == calendar [ "entity_id" ] }
15
+
16
+ res . map! do |event |
17
+ event [ "calendar_entity_id" ] = calendar [ "entity_id" ]
18
+ event [ "calendar_name" ] = calendar [ "name" ]
19
+
20
+ event [ "starts_at" ] =
21
+ if event [ "start" ] [ "date" ]
22
+ event [ "start" ] [ "date" ]
23
+ else
24
+ event [ "start" ] [ "dateTime" ]
25
+ end
26
+
27
+ event [ "ends_at" ] =
28
+ if event [ "end" ] [ "date" ]
29
+ event [ "end" ] [ "date" ]
30
+ else
31
+ event [ "end" ] [ "dateTime" ]
32
+ end
33
+
34
+ if config
35
+ event [ "icon" ] = config [ "icon" ]
36
+ event [ "letter" ] = config [ "letter" ]
37
+ end
38
+
39
+ event [ "id" ] = event [ "uid" ]
40
+
41
+ event . delete ( "uid" )
42
+ event . delete ( "start" )
43
+ event . delete ( "end" )
44
+ event . delete ( "recurrence_id" )
45
+ event . delete ( "rrule" )
46
+ event . delete ( "calendar_entity_id" )
47
+ event . delete ( "calendar_name" )
48
+
49
+ event
50
+ end
51
+
52
+ out . concat ( res )
53
+ end
54
+
55
+ out
56
+ end
57
+
58
+ def headers
59
+ {
60
+ Authorization : "Bearer #{ @config [ "home_assistant_token" ] } " ,
61
+ "content-type" : "application/json"
62
+ }
63
+ end
64
+
65
+ def data
66
+ @data ||=
67
+ if super . empty?
68
+ [ ]
69
+ else
70
+ super . map { CalendarEvent . new ( **_1 . symbolize_keys! ) }
71
+ end
72
+ end
73
+ end
Original file line number Diff line number Diff line change @@ -5,6 +5,7 @@ def call(
5
5
google_calendar_api : GoogleCalendarApi . new ,
6
6
calendar_feed : CalendarFeed . new ,
7
7
home_assistant_api : HomeAssistantApi . new ,
8
+ home_assistant_calendar_api : HomeAssistantCalendarApi . new ,
8
9
birdnet_api : BirdnetApi . new ,
9
10
air_now_api : AirNowApi . new
10
11
)
@@ -95,6 +96,8 @@ def call(
95
96
out [ :status_icons_with_labels ] << [ "triangle-exclamation" , "Google Calendar" ]
96
97
end
97
98
99
+ raw_events << home_assistant_calendar_api . data
100
+
98
101
# :nocov:
99
102
100
103
out [ :day_groups ] =
You can’t perform that action at this time.
0 commit comments