Skip to content

Commit

Permalink
add home assistant lightning API
Browse files Browse the repository at this point in the history
  • Loading branch information
joelhawksley committed Nov 2, 2024
1 parent 63181cd commit 2526f88
Show file tree
Hide file tree
Showing 7 changed files with 164 additions and 0 deletions.
34 changes: 34 additions & 0 deletions app/apis/home_assistant_lightning_api.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
class HomeAssistantLightningApi < Api
def initialize(config = Timeframe::Application.config.local)
@config = config
end

def fetch
start_time = (Time.now - 30.minutes).utc.iso8601
end_time = Time.now.utc.iso8601

res = HTTParty.get(
"#{url}#{start_time}?filter_entity_id=#{@config["home_assistant"]["lightning_distance_sensor_entity_id"]}" \
"&end_time=#{end_time}",
headers: headers
)

save_response(res)
end

def headers
{
Authorization: "Bearer #{@config["home_assistant_token"]}",
"content-type": "application/json"
}
end

def distance
this_data = data.flatten
return nil if this_data.empty?

value = this_data.reject { _1[:state] == "unknown" }.map { _1[:state].to_i }.min

"#{value}mi" if value
end
end
2 changes: 2 additions & 0 deletions app/jobs/schedule_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ def perform(task)
HomeAssistantApi.new(Timeframe::Application.config.local).fetch
when :home_assistant_calendar
HomeAssistantCalendarApi.new(Timeframe::Application.config.local).fetch
when :home_assistant_lightning
HomeAssistantLightningApi.new(Timeframe::Application.config.local).fetch
when :weather_kit
WeatherKitApi.new.fetch
when :birdnet
Expand Down
5 changes: 5 additions & 0 deletions app/models/display_content.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ def call(
calendar_feed: CalendarFeed.new,
home_assistant_api: HomeAssistantApi.new,
home_assistant_calendar_api: HomeAssistantCalendarApi.new,
home_assistant_lightning_api: HomeAssistantLightningApi.new,
birdnet_api: BirdnetApi.new,
air_now_api: AirNowApi.new
)
Expand Down Expand Up @@ -89,6 +90,10 @@ def call(
raw_events << air_now_api.daily_calendar_events
end

if home_assistant_lightning_api.healthy? && home_assistant_lightning_api.distance.present?
out[:status_icons_with_labels] << ["cloud-bolt", home_assistant_lightning_api.distance]
end

if home_assistant_calendar_api.healthy? && home_assistant_calendar_api.private_mode?
out[:status_icons] << "eye-slash"
end
Expand Down
7 changes: 7 additions & 0 deletions config/initializers/sidekiq_cron.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@
class: "ScheduleJob"
)

Sidekiq::Cron::Job.create(
name: "Fetch Home Assistant Lightning",
cron: "every 1 minute",
args: ["home_assistant_lightning"],
class: "ScheduleJob"
)

Sidekiq::Cron::Job.create(
name: "Fetch WeatherKit",
cron: "*/1 * * * *",
Expand Down
66 changes: 66 additions & 0 deletions test/apis/home_assistant_lightning_api_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# frozen_string_literal: true

require "test_helper"

class HomeAssistantLightningApiTest < Minitest::Test
include ActiveSupport::Testing::TimeHelpers

def test_fetch
VCR.use_cassette(:home_assistant_lightning_states) do
travel_to DateTime.new(2024, 11, 1, 15, 15, 0, "-0600") do
api = HomeAssistantLightningApi.new
api.fetch

assert(api.data.length == 1)
end
end
end

def test_miles_unknown
api = HomeAssistantLightningApi.new

data = [
[
{entity_id: "sensor.blitzortung_lightning_distance",
state: "unknown",
attributes: {lat: 40.664612,
lon: -106.736329,
unit_of_measurement: "mi",
device_class: "distance",
friendly_name: "Blitzortung Lightning distance"},
last_changed: "2024-11-01T20:45:00+00:00",
last_reported: "2024-11-01T20:45:00+00:00",
last_updated: "2024-11-01T20:45:00+00:00",
context: {id: "01JB7AHGJ001BVZJP660G9JKXA", parent_id: nil, user_id: nil}}
]
]

api.stub :data, data do
assert_nil(api.distance)
end
end

def test_miles_close
api = HomeAssistantLightningApi.new

data = [
[
{entity_id: "sensor.blitzortung_lightning_distance",
state: "10.2",
attributes: {lat: 40.664612,
lon: -106.736329,
unit_of_measurement: "mi",
device_class: "distance",
friendly_name: "Blitzortung Lightning distance"},
last_changed: "2024-11-01T20:45:00+00:00",
last_reported: "2024-11-01T20:45:00+00:00",
last_updated: "2024-11-01T20:45:00+00:00",
context: {id: "01JB7AHGJ001BVZJP660G9JKXA", parent_id: nil, user_id: nil}}
]
]

api.stub :data, data do
assert_equal(api.distance, "10mi")
end
end
end
8 changes: 8 additions & 0 deletions test/models/calendar_feed_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@ def test_baby_age_event_36_mos
end
end

def test_baby_age_event_36_mos_1_day
travel_to DateTime.new(2026, 7, 12, 8, 0, 0, "-0600") do
result = CalendarFeed.new.baby_age_event(Date.new(2023, 7, 11))

assert_equal("3y1d", result.summary)
end
end

# CalendarEvents coming from the DB look different than those
# constructed on the fly. DB events have string keys, for example.
# This is not ideal and we should probably move to a standard value
Expand Down
42 changes: 42 additions & 0 deletions test/vcr_cassettes/home_assistant_lightning_states.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 2526f88

Please sign in to comment.