Skip to content

Commit afe73bf

Browse files
committed
check for unavailable entities
1 parent ce42958 commit afe73bf

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

app/apis/home_assistant_api.rb

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,13 @@ def prepare_response(response)
1717
response.filter do
1818
entity_ids.include?(_1["entity_id"]) ||
1919
_1.dig("attributes", "device_class") == "battery" ||
20-
_1["entity_id"].include?("sensor.timeframe")
20+
_1["entity_id"].include?("sensor.timeframe") ||
21+
_1["state"] == "unavailable"
2122
end
2223
end
2324

2425
def problems
25-
data
26+
timeframe_sensor_problems = data
2627
.select { _1[:entity_id].include?("sensor.timeframe") }
2728
.map do
2829
if _1[:state] == "on"
@@ -41,6 +42,17 @@ def problems
4142
}
4243
end
4344
end.compact
45+
46+
unavailable_entity_problems = data
47+
.select { _1[:state] == "unavailable" }
48+
.map do
49+
{
50+
icon: "triangle-exclamation",
51+
message: "#{_1[:entity_id].split(".").last.humanize} unavailable"
52+
}
53+
end
54+
55+
timeframe_sensor_problems + unavailable_entity_problems
4456
end
4557

4658
def now_playing

test/apis/home_assistant_api_test.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,4 +379,13 @@ def test_problems_non_binary
379379
assert_equal(api.problems, [{icon: "print", message: "Ink low"}])
380380
end
381381
end
382+
383+
def test_problems_unavailable
384+
data = [{entity_id: "sensor.foo_bar", state: "unavailable"}]
385+
386+
api = HomeAssistantApi.new({})
387+
api.stub :data, data do
388+
assert_equal(api.problems, [{icon: "triangle-exclamation", message: "Foo bar unavailable"}])
389+
end
390+
end
382391
end

0 commit comments

Comments
 (0)