Skip to content

Debloat any Android or Google TV over Wi-Fi and get an Apple TV-style home.

addons: Smart Home shows fuel prices unrounded and lets never-run scenes run

maker-chrobok3090814a17fa
diff --git a/addons/smart-home/README.md b/addons/smart-home/README.md
index 14ae5e45b..3d35414ad 100644
--- a/addons/smart-home/README.md
+++ b/addons/smart-home/README.md
@@ -19,7 +19,9 @@ the [Clean Your TV](../../README.md) launcher it declares a [module card](../../
| Doors and windows | `binary_sensor.*` with device class door, window, garage door or opening | Shown only |
| Locks | `lock.*`, locked or unlocked | **Shown only: a TV remote never unlocks a door** |
-At most 80 entities, sorted by name within each section; unavailable ones are dimmed. The glance header takes the
+At most 80 entities, sorted by name within each section; unavailable ones are dimmed. A scene that has never
+run reads `unknown` in Home Assistant and can still be run. Fuel prices show as Home Assistant reports them, with
+all their decimals (1.689 €/L). The glance header takes the
first available weather entity and up to six fuel-price sensors unless you name the entities in the config (below).
It reads `GET /api/states` every 10 seconds while the screen is open, and nothing when it is closed.
diff --git a/addons/smart-home/app/src/main/java/io/github/maxhayim/smarthome/Ha.java b/addons/smart-home/app/src/main/java/io/github/maxhayim/smarthome/Ha.java
index 0af04cc42..54da20e41 100644
--- a/addons/smart-home/app/src/main/java/io/github/maxhayim/smarthome/Ha.java
+++ b/addons/smart-home/app/src/main/java/io/github/maxhayim/smarthome/Ha.java
@@ -50,6 +50,8 @@ final class Ha {
}
boolean available() {
+ // A scene's state is the time it last ran, and "unknown" until it has run once: that one can still run.
+ if (kind == Kind.SCENE) return !"unavailable".equals(state);
return !"unavailable".equals(state) && !"unknown".equals(state);
}
@@ -60,11 +62,7 @@ final class Ha {
case WEATHER:
return Double.isNaN(current) ? capital(state) : number(current) + "°";
case PRICE:
- try {
- return number(Double.parseDouble(state)) + (unit.isEmpty() ? "" : " " + unit);
- } catch (NumberFormatException e) {
- return state;
- }
+ return price(state) + (unit.isEmpty() ? "" : " " + unit);
case SCENE:
return "Run";
case TOGGLE:
@@ -97,7 +95,7 @@ final class Ha {
static final class Glance {
final Entity weather;
final List<Entity> prices;
- /** One line, for example "21° · E10 1.69 €/L · Diesel 1.55 €/L". Empty when nothing to show. */
+ /** One line, for example "21° · E10 1.689 €/L · Diesel 1.549 €/L". Empty when nothing to show. */
final String text;
/** clear, cloudy, rain, snow, storm, fog, night, or empty. */
final String icon;
@@ -122,11 +120,7 @@ final class Ha {
if (!p.available()) continue;
if (b.length() > 0) b.append(" · ");
b.append(shortName(p.name));
- try {
- b.append(' ').append(number(Double.parseDouble(p.state)));
- } catch (NumberFormatException e) {
- b.append(' ').append(p.state);
- }
+ b.append(' ').append(price(p.state));
if (p.unit != null && !p.unit.isEmpty()) b.append(' ').append(p.unit);
}
return b.toString();
@@ -378,6 +372,20 @@ final class Ha {
return v == Math.rint(v) ? String.valueOf((long) v) : String.format(Locale.ROOT, "%.1f", v);
}
+ /**
+ * A price exactly as Home Assistant reports it, never rounded: fuel prices come with three decimals (1.689),
+ * and the last one is the tenth of a cent the station charges. At least two decimals, so 2.1 reads 2.10.
+ */
+ static String price(String state) {
+ try {
+ java.math.BigDecimal d = new java.math.BigDecimal(state.trim());
+ if (d.scale() < 2) d = d.setScale(2);
+ return d.toPlainString();
+ } catch (NumberFormatException | ArithmeticException e) {
+ return state;
+ }
+ }
+
private static String capital(String s) {
return s.isEmpty() ? s : Character.toUpperCase(s.charAt(0)) + s.substring(1);
}
diff --git a/addons/smart-home/app/src/test/java/io/github/maxhayim/smarthome/HaTest.java b/addons/smart-home/app/src/test/java/io/github/maxhayim/smarthome/HaTest.java
index 4d47c5200..d6ae826a9 100644
--- a/addons/smart-home/app/src/test/java/io/github/maxhayim/smarthome/HaTest.java
+++ b/addons/smart-home/app/src/test/java/io/github/maxhayim/smarthome/HaTest.java
@@ -52,6 +52,18 @@ public class HaTest {
assertEquals(13, all.size());
}
+ @Test
+ public void aSceneThatNeverRanCanRun() throws Exception {
+ // Home Assistant reports a scene as "unknown" until it has been activated once.
+ List<Ha.Entity> all = Ha.parse("[{\"entity_id\":\"scene.never\",\"state\":\"unknown\",\"attributes\":{}},"
+ + "{\"entity_id\":\"scene.gone\",\"state\":\"unavailable\",\"attributes\":{}}]");
+ Ha.Entity never = find(all, "scene.never");
+ assertTrue(never.available());
+ assertEquals("Run", never.value());
+ assertTrue(Ha.press(never) != null);
+ assertNull(Ha.press(find(all, "scene.gone")));
+ }
+
@Test
public void values() throws Exception {
List<Ha.Entity> all = Ha.parse(STATES);
@@ -65,7 +77,10 @@ public class HaTest {
assertEquals("Unavailable", find(all, "light.garage").value());
assertEquals("Run", find(all, "scene.good_night").value());
assertEquals("21.3°", find(all, "weather.home").value());
- assertEquals("1.7 €/L", find(all, "sensor.station_e10").value());
+ assertEquals("1.689 €/L", find(all, "sensor.station_e10").value());
+ assertEquals("2.10", Ha.price("2.1"));
+ assertEquals("2.00", Ha.price("2"));
+ assertEquals("unknown", Ha.price("unknown"));
}
@Test
@@ -75,7 +90,7 @@ public class HaTest {
assertEquals("weather.home", g.weather.id);
assertEquals("cloudy", g.icon);
assertEquals(2, g.prices.size());
- assertEquals("21.3° · Diesel 1.5 EUR/L · E10 1.7 €/L", g.text);
+ assertEquals("21.3° · Diesel 1.549 EUR/L · E10 1.689 €/L", g.text);
assertFalse(g.empty());
}