From f9f5e218f52f72da748654bba09cfa7826ef62dc Mon Sep 17 00:00:00 2001 From: Nick Campbell <661795+nickcampbell18@users.noreply.github.com> Date: Mon, 16 Sep 2024 16:17:22 +0100 Subject: [PATCH] Handle boundary condition when selecting battery icon (#235) We need to be able to handle floating point numbers right on the boundary between two icons. This means our from..to configurations must perfectly align at each end, and the algorithm to select those must not be both less-than-or-equal (<=) AND more-than-or-equal (>=). After this change: >>> get_icon_between(icon_states["battery_charge_level"], 0) 'battery-alert-variant-outline' >>> get_icon_between(icon_states["battery_charge_level"], 9.9) 'battery-alert-variant-outline' >>> get_icon_between(icon_states["battery_charge_level"], 10) 'battery-10' >>> get_icon_between(icon_states["battery_charge_level"], 39) 'battery-30' >>> get_icon_between(icon_states["battery_charge_level"], 39.9) 'battery-30' >>> get_icon_between(icon_states["battery_charge_level"], 39.9999) 'battery-30' >>> get_icon_between(icon_states["battery_charge_level"], 40) 'battery-40' >>> get_icon_between(icon_states["battery_charge_level"], 99.99) 'battery-90' >>> get_icon_between(icon_states["battery_charge_level"], 100) 'battery' >>> get_icon_between(icon_states["battery_charge_level"], 101) 'battery' --- src/const.py | 22 +++++++++++----------- src/util.py | 2 +- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/const.py b/src/const.py index 65c33f5..a99f699 100644 --- a/src/const.py +++ b/src/const.py @@ -75,17 +75,17 @@ "door_rear_right": {"ON": "car-door", "OFF": "car-door-lock"}, "engine_state": {"ON": "engine-outline", "OFF": "engine-off-outline"}, "battery_charge_level": [ - {"from": 100, "to": 100, "icon": "battery"}, - {"from": 99, "to": 90, "icon": "battery-90"}, - {"from": 89, "to": 80, "icon": "battery-80"}, - {"from": 79, "to": 70, "icon": "battery-70"}, - {"from": 69, "to": 60, "icon": "battery-60"}, - {"from": 59, "to": 50, "icon": "battery-50"}, - {"from": 49, "to": 40, "icon": "battery-40"}, - {"from": 39, "to": 30, "icon": "battery-30"}, - {"from": 29, "to": 20, "icon": "battery-20"}, - {"from": 19, "to": 10, "icon": "battery-10"}, - {"from": 9, "to": 0, "icon": "battery-alert-variant-outline"}, + {"from": float('inf'), "to": 100, "icon": "battery"}, + {"from": 100, "to": 90, "icon": "battery-90"}, + {"from": 90, "to": 80, "icon": "battery-80"}, + {"from": 80, "to": 70, "icon": "battery-70"}, + {"from": 70, "to": 60, "icon": "battery-60"}, + {"from": 60, "to": 50, "icon": "battery-50"}, + {"from": 50, "to": 40, "icon": "battery-40"}, + {"from": 40, "to": 30, "icon": "battery-30"}, + {"from": 30, "to": 20, "icon": "battery-20"}, + {"from": 20, "to": 10, "icon": "battery-10"}, + {"from": 10, "to": 0, "icon": "battery-alert-variant-outline"}, ] } diff --git a/src/util.py b/src/util.py index ba83c7a..e088943 100644 --- a/src/util.py +++ b/src/util.py @@ -57,7 +57,7 @@ def save_to_json(data, token_path): def get_icon_between(icon_list, state): icon = None for s in icon_list: - if s["to"] <= state <= s["from"]: + if s["to"] <= state < s["from"]: icon = s["icon"] return icon