Skip to content

Commit 50b0cca

Browse files
committed
wip
1 parent aa4816c commit 50b0cca

File tree

3 files changed

+81
-1
lines changed

3 files changed

+81
-1
lines changed

app/apis/api.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
class Api
2+
def url
3+
Timeframe::Application.config.local["#{self.class.name.underscore}_url"]
4+
end
5+
26
def fetch
3-
response = HTTParty.get(Timeframe::Application.config.local["#{self.class.name.underscore}_url"], headers: headers)
7+
response = HTTParty.get(url, headers: headers)
48

59
return if response.code != 200
610

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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

app/models/display_content.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ def call(
55
google_calendar_api: GoogleCalendarApi.new,
66
calendar_feed: CalendarFeed.new,
77
home_assistant_api: HomeAssistantApi.new,
8+
home_assistant_calendar_api: HomeAssistantCalendarApi.new,
89
birdnet_api: BirdnetApi.new,
910
air_now_api: AirNowApi.new
1011
)
@@ -95,6 +96,8 @@ def call(
9596
out[:status_icons_with_labels] << ["triangle-exclamation", "Google Calendar"]
9697
end
9798

99+
raw_events << home_assistant_calendar_api.data
100+
98101
# :nocov:
99102

100103
out[:day_groups] =

0 commit comments

Comments
 (0)