-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
63181cd
commit 2526f88
Showing
7 changed files
with
164 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.