Skip to content

Commit 6ac1ada

Browse files
committed
add support for non-binary sensors
1 parent 96c3628 commit 6ac1ada

File tree

2 files changed

+27
-10
lines changed

2 files changed

+27
-10
lines changed

app/apis/home_assistant_api.rb

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,29 @@ 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?("binary_sensor.timeframe")
20+
_1["entity_id"].include?("sensor.timeframe")
2121
end
2222
end
2323

2424
def problems
2525
data
26-
.select { _1[:entity_id].include?("binary_sensor.timeframe") && _1[:state] == "on" }
27-
.map { _1[:entity_id] }
28-
.map do |entity_id|
29-
_, icon, raw_message = entity_id.split("0")
30-
31-
{
32-
icon: icon.tr("_", "-"),
33-
message: raw_message.tr("_", " ").humanize
34-
}
26+
.select { _1[:entity_id].include?("sensor.timeframe") }
27+
.map do
28+
if _1[:state] == "on"
29+
_, icon, raw_message = _1[:entity_id].split("0")
30+
31+
{
32+
icon: icon.tr("_", "-"),
33+
message: raw_message.tr("_", " ").humanize
34+
}
35+
elsif !["on", "off", ""].include?(_1[:state])
36+
_, icon, _ = _1[:entity_id].split("0")
37+
38+
{
39+
icon: icon.tr("_", "-"),
40+
message: _1[:state]
41+
}
42+
end
3543
end
3644
end
3745

test/apis/home_assistant_api_test.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,4 +370,13 @@ def test_problems
370370
assert_equal(api.problems, [{icon: "print", message: "Printer ink low"}])
371371
end
372372
end
373+
374+
def test_problems_non_binary
375+
data = [{entity_id: "sensor.timeframe0print0printer_ink_low", state: "Ink low"}]
376+
377+
api = HomeAssistantApi.new({})
378+
api.stub :data, data do
379+
assert_equal(api.problems, [{icon: "print", message: "Ink low"}])
380+
end
381+
end
373382
end

0 commit comments

Comments
 (0)