Skip to content

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

addons/smart-home: the glance header doesn't count against the row cap

מקס חייםdee7a67b5294
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 18282630b..0af04cc42 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
@@ -15,7 +15,7 @@ import java.util.Locale;
* so the JVM tests cover it against the documented responses.
*/
final class Ha {
- /** At most this many rows, so a large installation can't make the screen slow. */
+ /** At most this many rows in the list, so a large installation can't make the screen slow. */
static final int MAX_ENTITIES = 80;
/** At most this many fuel-price sensors in the glance header. */
static final int MAX_PRICES = 6;
@@ -157,6 +157,15 @@ final class Ha {
/** The rows, sorted into sections (scenes, lights and switches, climate, sensors, doors and windows, locks). */
static List<Entity> parse(String json) throws JSONException {
+ return parse(json, Collections.emptyList());
+ }
+
+ /**
+ * The same, with the glance header's entities (picked by {@code glanceIds} as in {@link #glance}) kept ahead of
+ * the cap: one weather, up to {@link #MAX_PRICES} prices, then up to {@link #MAX_ENTITIES} list rows. Many price
+ * or weather entities can't push the switches out.
+ */
+ static List<Entity> parse(String json, List<String> glanceIds) throws JSONException {
JSONArray all = new JSONArray(json);
List<Entity> out = new ArrayList<>();
for (int i = 0; i < all.length(); i++) {
@@ -166,7 +175,17 @@ final class Ha {
if (e != null) out.add(e);
}
out.sort((a, b) -> a.kind != b.kind ? a.kind.compareTo(b.kind) : a.name.compareToIgnoreCase(b.name));
- return out.size() > MAX_ENTITIES ? new ArrayList<>(out.subList(0, MAX_ENTITIES)) : out;
+ Glance header = glance(out, glanceIds);
+ List<Entity> kept = new ArrayList<>();
+ if (header.weather != null) kept.add(header.weather);
+ kept.addAll(header.prices);
+ int rows = 0;
+ for (Entity e : out) {
+ if (!inList(e)) continue;
+ if (rows++ == MAX_ENTITIES) break;
+ kept.add(e);
+ }
+ return kept;
}
/**
diff --git a/addons/smart-home/app/src/main/java/io/github/maxhayim/smarthome/MainActivity.java b/addons/smart-home/app/src/main/java/io/github/maxhayim/smarthome/MainActivity.java
index 4a0609de4..8edae937e 100644
--- a/addons/smart-home/app/src/main/java/io/github/maxhayim/smarthome/MainActivity.java
+++ b/addons/smart-home/app/src/main/java/io/github/maxhayim/smarthome/MainActivity.java
@@ -100,7 +100,7 @@ public class MainActivity extends Activity {
String why = null;
if (r.ok()) {
try {
- found = Ha.parse(r.body);
+ found = Ha.parse(r.body, c.glance);
} catch (JSONException e) {
why = "Home Assistant's answer couldn't be read.";
}
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 fc5fb45ad..4d47c5200 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
@@ -145,6 +145,74 @@ public class HaTest {
assertEquals(Ha.MAX_ENTITIES, Ha.parse(b.append(']').toString()).size());
}
+ /** n entities of one shape: switch.s0…, sensor.p0… (a price) or weather.w0…. */
+ private static void add(StringBuilder b, String kind, int n) {
+ for (int i = 0; i < n; i++) {
+ if (b.length() > 1) b.append(',');
+ if (kind.equals("switch")) {
+ b.append("{\"entity_id\":\"switch.s").append(i).append("\",\"state\":\"off\",\"attributes\":{}}");
+ } else if (kind.equals("price")) {
+ b.append("{\"entity_id\":\"sensor.p").append(i)
+ .append("\",\"state\":\"1.5\",\"attributes\":{\"unit_of_measurement\":\"€/L\"}}");
+ } else {
+ b.append("{\"entity_id\":\"weather.w").append(i)
+ .append("\",\"state\":\"sunny\",\"attributes\":{\"temperature\":20}}");
+ }
+ }
+ }
+
+ private static long listRows(List<Ha.Entity> all) {
+ return all.stream().filter(Ha::inList).count();
+ }
+
+ @Test
+ public void manyPricesDontPushTheSwitchesOut() throws Exception {
+ StringBuilder b = new StringBuilder("[");
+ add(b, "price", 80);
+ add(b, "switch", 1);
+ List<Ha.Entity> all = Ha.parse(b.append(']').toString());
+ assertEquals(1, listRows(all));
+ assertEquals(Ha.MAX_PRICES, Ha.glance(all, Collections.emptyList()).prices.size());
+ assertEquals(1 + Ha.MAX_PRICES, all.size());
+ }
+
+ @Test
+ public void manyWeathersDontPushTheSwitchesOut() throws Exception {
+ StringBuilder b = new StringBuilder("[");
+ add(b, "weather", 80);
+ add(b, "switch", 1);
+ List<Ha.Entity> all = Ha.parse(b.append(']').toString());
+ assertEquals(1, listRows(all));
+ assertEquals("weather.w0", Ha.glance(all, Collections.emptyList()).weather.id);
+ }
+
+ @Test
+ public void theHeaderDoesntCountAgainstTheRowCap() throws Exception {
+ StringBuilder b = new StringBuilder("[");
+ add(b, "price", 6);
+ add(b, "weather", 1);
+ add(b, "switch", 200);
+ List<Ha.Entity> all = Ha.parse(b.append(']').toString());
+ assertEquals(Ha.MAX_ENTITIES, listRows(all));
+ Ha.Glance g = Ha.glance(all, Collections.emptyList());
+ assertEquals(6, g.prices.size());
+ assertEquals("weather.w0", g.weather.id);
+ }
+
+ @Test
+ public void aChosenPriceBeyondTheOldCutoffIsKept() throws Exception {
+ StringBuilder b = new StringBuilder("[");
+ add(b, "price", 90);
+ add(b, "switch", 3);
+ List<String> ids = Arrays.asList("sensor.p89", "sensor.p88");
+ List<Ha.Entity> all = Ha.parse(b.append(']').toString(), ids);
+ Ha.Glance g = Ha.glance(all, ids);
+ assertEquals(2, g.prices.size());
+ assertEquals("sensor.p89", g.prices.get(0).id);
+ assertEquals(3, listRows(all));
+ }
+
+
@Test
public void addresses() {
assertEquals("http://192.168.1.10:8123", Ha.normalizeUrl(" 192.168.1.10:8123/ "));