Skip to content

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

addons: Smart Home weather and fuel glance header

Alexander Over6382d0fadaef
diff --git a/addons/README.md b/addons/README.md
index 393f0c0fc..96f5fcfc0 100644
--- a/addons/README.md
+++ b/addons/README.md
@@ -29,7 +29,7 @@ the core bigger or change how it behaves. You install an add-on only if you want
| [Bluetooth Connect](bluetooth-connect/) | Paired Bluetooth speakers and headphones with a Connect / Disconnect button, which the TV's own settings lack | [@thenewnano](https://github.com/thenewnano) | Philips UHD Android TV (Android 14) | Nearby devices (`BLUETOOTH_CONNECT`), granted by ADB | <img src="bluetooth-connect/screenshot.jpg" width="240" alt="Bluetooth Connect on a TV"> |
| [AirPlay Status](airplay-status/) | How iPhones and Macs see this TV: its AirPlay name, screen mirroring offered or audio only (and why), and the other AirPlay receivers on the network | [@maxhayim](https://github.com/maxhayim) | MOKA R8G (Android 14) | network (mDNS listening only), network state | <img src="airplay-status/screenshot.jpg" width="240" alt="AirPlay Status on a TV"> |
| [Sources](sources/) | The TV's sources by the names devices give themselves over HDMI-CEC ("PlayStation 5", not "HDMI 2"), with their state, and Live TV; OK switches | [@maxhayim](https://github.com/maxhayim) | MOKA R8G (Android 14) | None | <img src="sources/screenshot.jpg" width="240" alt="Sources on a TV"> |
-| [Smart Home](smart-home/) | Home Assistant on the TV: scenes, lights and switches (OK toggles, left and right dim), climate, temperature and humidity, doors and windows, and locks shown but never opened | [@maxhayim](https://github.com/maxhayim) | MOKA R8G (Android 14) | `INTERNET` (your Home Assistant only) | <img src="smart-home/screenshot.jpg" width="240" alt="Smart Home on a TV"> |
+| [Smart Home](smart-home/) | Home Assistant on the TV: weather and fuel prices at a glance, scenes, lights and switches (OK toggles, left and right dim), climate, temperature and humidity, doors and windows, and locks shown but never opened | [@maxhayim](https://github.com/maxhayim) | MOKA R8G (Android 14); Sony BRAVIA (Android 9) | `INTERNET` (your Home Assistant only) | <img src="smart-home/screenshot.jpg" width="240" alt="Smart Home on a TV"> |
| [Ad Blocking](ad-blocking/) | Your Pi-hole (6 or 5) or AdGuard Home on the TV: today's queries and blocked share, and one OK to pause blocking for five minutes | [@maxhayim](https://github.com/maxhayim) | MOKA R8G (Android 14) | `INTERNET` (your blocker only) | <img src="ad-blocking/screenshot.jpg" width="240" alt="Ad Blocking on a TV"> |
| [Startup & Branding](startup-branding/) | A welcome screen of your own: your picture, a background colour and a line of text, full screen for a few seconds or until Back or OK, with a live preview, Fit/Fill/Stretch/Original scaling and a compatibility check of what the TV allows without root | [@maxhayim](https://github.com/maxhayim) | MOKA M1G (Android TV 11) | None | <img src="startup-branding/screenshot.jpg" width="240" alt="Startup & Branding on a TV"> |
| [World Clock](world-clock/) | One to six offline clocks with dates, UTC offsets and day differences; city search and D-pad controls | [@SaviorOneZero](https://github.com/SaviorOneZero) | Samsung C71KW-200 / AT&T Osprey (Android TV 11) | None | <img src="world-clock/screenshot.jpg" width="240" alt="World Clock on a TV"> |
diff --git a/addons/smart-home/README.md b/addons/smart-home/README.md
index 45773a1a4..e02eb4c23 100644
--- a/addons/smart-home/README.md
+++ b/addons/smart-home/README.md
@@ -1,16 +1,17 @@
# Smart Home
-Your [Home Assistant](https://www.home-assistant.io) on the TV: scenes, lights and switches, climate, temperature
-and humidity sensors, doors and windows, and locks, in sections you work with the remote. OK runs a scene or
-switches a light; ◀ ▶ dims a light by 10 % or moves a thermostat by half a degree. A community add-on, not made by
-or affiliated with the Home Assistant project. For the [Clean Your TV](../../README.md) launcher it declares a
-[module card](../../docs/modules.md): once you answer **Show card** its tile reads "Smart Home" with a blue glow.
-On any other Android TV home it is an ordinary app.
+Your [Home Assistant](https://www.home-assistant.io) on the TV: a glance of weather and fuel prices at the top,
+then scenes, lights and switches, climate, temperature and humidity sensors, doors and windows, and locks, in
+sections you work with the remote. OK runs a scene or switches a light; ◀ ▶ dims a light by 10 % or moves a
+thermostat by half a degree. A community add-on, not made by or affiliated with the Home Assistant project. For
+the [Clean Your TV](../../README.md) launcher it declares a [module card](../../docs/modules.md): once you answer
+**Show card** its tile reads "Smart Home" with a blue glow. On any other Android TV home it is an ordinary app.
![Smart Home on a TV, with demo data](screenshot.jpg)
| Section | Shows | With the remote |
|---|---|---|
+| Glance | `weather.*` temperature and condition icon; sensors whose unit is `€`, `EUR`, `€/L` or `EUR/L` (Tankerkönig and the like) | Shown only |
| Scenes | `scene.*` | OK runs it |
| Lights and switches | `light.*`, `switch.*`, `fan.*`, `input_boolean.*`, on or off with brightness | OK toggles, ◀ ▶ dims a light |
| Climate | `climate.*`: room temperature → set temperature | ◀ ▶ moves the set temperature |
@@ -18,8 +19,9 @@ On any other Android TV home it is an ordinary app.
| 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. It reads `GET /api/states`
-every 10 seconds while the screen is open, and nothing when it is closed.
+At most 80 entities, sorted by name within each section; unavailable ones are dimmed. 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.
## Setup
@@ -31,10 +33,11 @@ Assistant, open your profile, then Security, then Long-lived access tokens, Crea
- put both in a file and push it from a computer, so the token is never typed on the TV or put on a command line:
```bash
- echo '{"url": "http://192.168.1.10:8123", "token": "eyJ..."}' > backup/smart-home.json
+ echo '{"url": "http://192.168.1.10:8123", "token": "eyJ...", "glance": ["weather.home", "sensor.station_e10", "sensor.station_diesel"]}' > backup/smart-home.json
TV_HOST=<tv ip> addons/smart-home/install.sh backup/smart-home.json
```
+ `glance` is optional: without it the app picks the first weather entity and up to six euro fuel-price sensors.
The app takes the file into its own storage and deletes it. Keep the file outside the repo (`backup/` is
git-ignored).
@@ -47,7 +50,7 @@ Assistant, open your profile, then Security, then Long-lived access tokens, Crea
Plain `http://` is allowed for the home network (`res/xml/network.xml`); use `https://` with a certificate from a
public authority (Nabu Casa, Let's Encrypt). The token stays in the app's own storage (backups are off) and is
sent only to the address you entered, as `Authorization: Bearer`. `ConfigProvider` takes a pushed file and only
-the ADB shell can use it (it requires `WRITE_SECURE_SETTINGS`, as Frigate Glance's does). No background service,
+the ADB shell can use it (it requires `WRITE_SECURE_SETTINGS`, as Frigate Glance's does). No foreground service,
accounts or libraries.
## Build and install
@@ -61,16 +64,20 @@ TV_HOST=<tv ip> addons/smart-home/install.sh [config.json]
To build only: `cd addons/smart-home && ./gradlew assembleRelease`; the JVM tests run with
`./gradlew testReleaseUnitTest`.
+A new `screenshot.jpg` from a real TV (16:9, under 300 KB, address and token blurred) belongs with any visual
+change to the glance header.
+
## Limits
- **Self-signed HTTPS** certificates are refused. Use `http://` on the home network or a public certificate.
- **`homeassistant.local`** names only resolve where the TV's network does mDNS name lookups; use the IP address
if it doesn't.
-- **Which entities** is fixed by domain and device class; there is no picker yet.
+- **Which entities** for the remote list is fixed by domain and device class; the glance list can be named in
+ `config.json`. There is no on-TV picker yet.
## Tested on
-MOKA R8G (Android 14), on a home app that implements the module-card spec, against a stand-in server on the home network answering with Home Assistant's documented response shapes: the config pushed through `ConfigProvider`, reading the states, running a scene and dimming a light (the requests arrived as the REST API documents them). On a TCL Smart TV Pro (G08, Android 14), [#1752](https://github.com/Gathin23/cleanyourtv/issues/1752) reports a connection to a real Home Assistant server and lists entities with their current states; running a scene and toggling a light were not tested there. The screenshot shows the stand-in data, with the address blurred. The JVM tests cover parsing, the service calls, the request headers against a local stand-in, and errors.
+MOKA R8G (Android 14), on a home app that implements the module-card spec, against a stand-in server on the home network answering with Home Assistant's documented response shapes: the config pushed through `ConfigProvider`, reading the states, running a scene and dimming a light (the requests arrived as the REST API documents them). The screenshot shows that demo data, with the address blurred. Also tried on a Sony BRAVIA (Android 9) against a real Home Assistant: weather and a euro fuel-price sensor on the glance header (with unit). The JVM tests cover parsing, the service calls, the request headers against a local stand-in, and errors.
## Maintainer
diff --git a/addons/smart-home/app/src/main/java/io/github/maxhayim/smarthome/Config.java b/addons/smart-home/app/src/main/java/io/github/maxhayim/smarthome/Config.java
index e2aa69aed..cd821228c 100644
--- a/addons/smart-home/app/src/main/java/io/github/maxhayim/smarthome/Config.java
+++ b/addons/smart-home/app/src/main/java/io/github/maxhayim/smarthome/Config.java
@@ -3,25 +3,32 @@ package io.github.maxhayim.smarthome;
import android.content.Context;
import android.content.SharedPreferences;
+import org.json.JSONArray;
import org.json.JSONObject;
import java.io.File;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
/**
- * The server address and token, in the app's own storage (backups are off). They arrive typed on the TV, or as
- * config.json pushed by install.sh, which is read once and deleted.
+ * The server address, token and optional glance entity ids, in the app's own storage (backups are off). They
+ * arrive typed on the TV, or as config.json pushed by install.sh, which is read once and deleted.
*/
final class Config {
static final String PUSHED = "config.json";
final String url;
final String token;
+ /** Entity ids for the glance header, in order; empty means auto-pick weather and fuel prices. */
+ final List<String> glance;
- Config(String url, String token) {
+ Config(String url, String token, List<String> glance) {
this.url = url;
this.token = token;
+ this.glance = Collections.unmodifiableList(glance == null ? Collections.emptyList() : glance);
}
boolean complete() {
@@ -34,7 +41,7 @@ final class Config {
static Config load(Context c) {
SharedPreferences p = prefs(c);
- return new Config(p.getString("url", ""), p.getString("token", ""));
+ return new Config(p.getString("url", ""), p.getString("token", ""), readGlance(p.getString("glance", "")));
}
static void saveUrl(Context c, String url) {
@@ -45,7 +52,11 @@ final class Config {
prefs(c).edit().putString("token", token == null ? "" : token.trim()).apply();
}
- /** Takes a pushed config.json ({"url": ..., "token": ...}) if there is one, and deletes it. True when taken. */
+ static void saveGlance(Context c, List<String> ids) {
+ prefs(c).edit().putString("glance", joinGlance(ids)).apply();
+ }
+
+ /** Takes a pushed config.json ({"url": ..., "token": ..., "glance": [...]}) if there is one, and deletes it. */
static boolean importPushed(Context c) {
File dir = c.getExternalFilesDir(null);
File f = dir == null ? null : new File(dir, PUSHED);
@@ -54,6 +65,7 @@ final class Config {
JSONObject o = new JSONObject(new String(Files.readAllBytes(f.toPath()), StandardCharsets.UTF_8));
if (o.has("url")) saveUrl(c, o.optString("url"));
if (o.has("token")) saveToken(c, o.optString("token"));
+ if (o.has("glance")) saveGlance(c, parseGlance(o.opt("glance")));
return true;
} catch (Exception e) {
return false;
@@ -62,4 +74,39 @@ final class Config {
f.delete();
}
}
+
+ static List<String> parseGlance(Object raw) {
+ List<String> out = new ArrayList<>();
+ if (raw instanceof JSONArray) {
+ JSONArray a = (JSONArray) raw;
+ for (int i = 0; i < a.length(); i++) {
+ String id = a.optString(i, "").trim();
+ if (!id.isEmpty() && !out.contains(id)) out.add(id);
+ }
+ } else if (raw instanceof String) {
+ out.addAll(readGlance((String) raw));
+ }
+ return out;
+ }
+
+ private static List<String> readGlance(String joined) {
+ List<String> out = new ArrayList<>();
+ if (joined == null || joined.isEmpty()) return out;
+ for (String part : joined.split(",")) {
+ String id = part.trim();
+ if (!id.isEmpty() && !out.contains(id)) out.add(id);
+ }
+ return out;
+ }
+
+ private static String joinGlance(List<String> ids) {
+ if (ids == null || ids.isEmpty()) return "";
+ StringBuilder b = new StringBuilder();
+ for (String id : ids) {
+ if (id == null || id.trim().isEmpty()) continue;
+ if (b.length() > 0) b.append(',');
+ b.append(id.trim());
+ }
+ return b.toString();
+ }
}
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 0a0c78c72..18282630b 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
@@ -5,6 +5,7 @@ import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
+import java.util.Collections;
import java.util.List;
import java.util.Locale;
@@ -16,8 +17,10 @@ import java.util.Locale;
final class Ha {
/** At most this many rows, 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;
- enum Kind { SCENE, TOGGLE, CLIMATE, SENSOR, OPENING, LOCK }
+ enum Kind { WEATHER, PRICE, SCENE, TOGGLE, CLIMATE, SENSOR, OPENING, LOCK }
static final class Entity {
final String id;
@@ -54,6 +57,14 @@ final class Ha {
String value() {
if (!available()) return "Unavailable";
switch (kind) {
+ 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;
+ }
case SCENE:
return "Run";
case TOGGLE:
@@ -79,6 +90,57 @@ final class Ha {
}
}
+ /**
+ * What the glance header shows: one weather entity and up to {@link #MAX_PRICES} fuel prices.
+ * {@link #icon} names the weather drawable in the header.
+ */
+ 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. */
+ final String text;
+ /** clear, cloudy, rain, snow, storm, fog, night, or empty. */
+ final String icon;
+
+ Glance(Entity weather, List<Entity> prices) {
+ this.weather = weather;
+ this.prices = Collections.unmodifiableList(prices);
+ this.icon = weather == null || !weather.available() ? "" : weatherIcon(weather.state);
+ this.text = line(weather, prices);
+ }
+
+ boolean empty() {
+ return text.isEmpty();
+ }
+
+ private static String line(Entity weather, List<Entity> prices) {
+ StringBuilder b = new StringBuilder();
+ if (weather != null && weather.available() && !Double.isNaN(weather.current)) {
+ b.append(number(weather.current)).append('°');
+ }
+ for (Entity p : prices) {
+ 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);
+ }
+ if (p.unit != null && !p.unit.isEmpty()) b.append(' ').append(p.unit);
+ }
+ return b.toString();
+ }
+
+ /** First word of the friendly name, capped so the header line stays short. */
+ private static String shortName(String name) {
+ String n = name == null ? "" : name.trim();
+ int space = n.indexOf(' ');
+ if (space > 0) n = n.substring(0, space);
+ return n.length() > 12 ? n.substring(0, 12) : n;
+ }
+ }
+
/** One service call: POST path with body. */
static final class Call {
final String path;
@@ -107,6 +169,85 @@ final class Ha {
return out.size() > MAX_ENTITIES ? new ArrayList<>(out.subList(0, MAX_ENTITIES)) : out;
}
+ /**
+ * Weather and fuel prices for the glance header. With {@code ids} empty: the first available weather
+ * entity and up to {@link #MAX_PRICES} price sensors. With ids set: those entities, in that order.
+ */
+ static Glance glance(List<Entity> all, List<String> ids) {
+ if (ids != null && !ids.isEmpty()) {
+ Entity weather = null;
+ List<Entity> prices = new ArrayList<>();
+ for (String id : ids) {
+ Entity e = find(all, id);
+ if (e == null) continue;
+ if (e.kind == Kind.WEATHER && weather == null) weather = e;
+ else if (e.kind == Kind.PRICE && prices.size() < MAX_PRICES) prices.add(e);
+ }
+ return new Glance(weather, prices);
+ }
+ Entity weather = null;
+ List<Entity> prices = new ArrayList<>();
+ for (Entity e : all) {
+ if (e.kind == Kind.WEATHER && weather == null && e.available()) weather = e;
+ else if (e.kind == Kind.PRICE && prices.size() < MAX_PRICES) prices.add(e);
+ }
+ if (weather == null) {
+ for (Entity e : all) {
+ if (e.kind == Kind.WEATHER) {
+ weather = e;
+ break;
+ }
+ }
+ }
+ return new Glance(weather, prices);
+ }
+
+ /** Rows for the remote list: weather and prices live in the glance header only. */
+ static boolean inList(Entity e) {
+ return e.kind != Kind.WEATHER && e.kind != Kind.PRICE;
+ }
+
+ /**
+ * Closed vocabulary for the Home line and the in-app icon: clear, cloudy, rain, snow, storm, fog, night.
+ * Unknown Home Assistant states map to cloudy.
+ */
+ static String weatherIcon(String state) {
+ if (state == null) return "cloudy";
+ switch (state) {
+ case "sunny":
+ case "clear":
+ return "clear";
+ case "clear-night":
+ return "night";
+ case "cloudy":
+ case "partlycloudy":
+ case "windy":
+ case "windy-variant":
+ return "cloudy";
+ case "fog":
+ return "fog";
+ case "rainy":
+ case "pouring":
+ case "hail":
+ return "rain";
+ case "snowy":
+ case "snowy-rainy":
+ return "snow";
+ case "lightning":
+ case "lightning-rainy":
+ return "storm";
+ default:
+ return "cloudy";
+ }
+ }
+
+ static boolean priceUnit(String unit) {
+ if (unit == null) return false;
+ String u = unit.trim().toUpperCase(Locale.ROOT).replace(" ", "");
+ return u.equals("€") || u.equals("EUR") || u.equals("€/L") || u.equals("EUR/L")
+ || u.equals("€/LITRE") || u.equals("EUR/LITRE");
+ }
+
static Entity entity(JSONObject o) {
String id = o.optString("entity_id", "");
String state = o.optString("state", "");
@@ -119,6 +260,9 @@ final class Ha {
String deviceClass = a.optString("device_class", "");
String unit = a.optString("unit_of_measurement", "");
switch (domain) {
+ case "weather":
+ return new Entity(id, Kind.WEATHER, name, state, -1, a.optDouble("temperature", Double.NaN),
+ Double.NaN, "°");
case "scene":
return new Entity(id, Kind.SCENE, name, state, -1, Double.NaN, Double.NaN, "");
case "light":
@@ -132,6 +276,9 @@ final class Ha {
return new Entity(id, Kind.CLIMATE, name, state, -1, a.optDouble("current_temperature", Double.NaN),
a.optDouble("temperature", Double.NaN), "°");
case "sensor":
+ if (priceUnit(unit)) {
+ return new Entity(id, Kind.PRICE, name, state, -1, Double.NaN, Double.NaN, unit);
+ }
if (!deviceClass.equals("temperature") && !deviceClass.equals("humidity")) return null;
return new Entity(id, Kind.SENSOR, name, state, -1, Double.NaN, Double.NaN, unit);
case "binary_sensor":
@@ -215,4 +362,9 @@ final class Ha {
private static String capital(String s) {
return s.isEmpty() ? s : Character.toUpperCase(s.charAt(0)) + s.substring(1);
}
+
+ private static Entity find(List<Entity> all, String id) {
+ for (Entity e : all) if (e.id.equals(id)) return e;
+ return null;
+ }
}
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 502f7875a..4a0609de4 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
@@ -6,9 +6,11 @@ import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.text.InputType;
+import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
+import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;
@@ -21,9 +23,10 @@ import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
- * The tile and module card open this: your Home Assistant's scenes, lights and switches, climate, sensors, doors
- * and windows, and locks, in sections. OK runs a scene or switches a light; Left and Right dim a light or move a
- * thermostat. Locks are shown, never opened. It reads Home Assistant only while this screen is open.
+ * The tile and module card open this: a glance of weather and fuel prices at the top, then your Home
+ * Assistant's scenes, lights and switches, climate, sensors, doors and windows, and locks. OK runs a
+ * scene or switches a light; Left and Right dim a light or move a thermostat. Locks are shown, never
+ * opened. It reads Home Assistant only while this screen is open.
*/
public class MainActivity extends Activity {
private static final long REFRESH_MS = 10_000;
@@ -139,8 +142,10 @@ public class MainActivity extends Activity {
} else {
status.setText(entities.isEmpty() ? "Reading Home Assistant…" : "Connected to " + config.url + ".");
}
+ addGlance(Ha.glance(entities, config.glance));
Ha.Kind section = null;
for (Ha.Entity e : entities) {
+ if (!Ha.inList(e)) continue;
if (e.kind != section) {
section = e.kind;
heading(Ha.section(section));
@@ -173,6 +178,58 @@ public class MainActivity extends Activity {
}
}
+ private void addGlance(Ha.Glance g) {
+ if (g.empty()) return;
+ LinearLayout box = new LinearLayout(this);
+ box.setOrientation(LinearLayout.HORIZONTAL);
+ box.setGravity(Gravity.CENTER_VERTICAL);
+ box.setPadding(px(16), px(8), px(16), px(20));
+ if (!g.icon.isEmpty()) {
+ ImageView icon = new ImageView(this);
+ icon.setImageResource(weatherDrawable(g.icon));
+ LinearLayout.LayoutParams ip = new LinearLayout.LayoutParams(px(56), px(56));
+ ip.setMarginEnd(px(20));
+ box.addView(icon, ip);
+ }
+ LinearLayout text = new LinearLayout(this);
+ text.setOrientation(LinearLayout.VERTICAL);
+ if (g.weather != null && g.weather.available() && !Double.isNaN(g.weather.current)) {
+ TextView temp = new TextView(this);
+ temp.setText(Ha.number(g.weather.current) + "°");
+ temp.setTextSize(42);
+ temp.setTextColor(0xFFFFFFFF);
+ temp.setIncludeFontPadding(false);
+ text.addView(temp);
+ }
+ if (!g.prices.isEmpty()) {
+ TextView prices = new TextView(this);
+ StringBuilder line = new StringBuilder();
+ for (Ha.Entity p : g.prices) {
+ if (line.length() > 0) line.append(" ");
+ line.append(p.name).append(' ').append(p.value());
+ }
+ prices.setText(line.toString());
+ prices.setTextSize(16);
+ prices.setTextColor(0xFF8B949E);
+ prices.setPadding(0, px(4), 0, 0);
+ text.addView(prices);
+ }
+ box.addView(text);
+ rows.addView(box);
+ }
+
+ private static int weatherDrawable(String icon) {
+ switch (icon) {
+ case "clear": return R.drawable.ic_weather_clear;
+ case "rain": return R.drawable.ic_weather_rain;
+ case "snow": return R.drawable.ic_weather_snow;
+ case "storm": return R.drawable.ic_weather_storm;
+ case "fog": return R.drawable.ic_weather_fog;
+ case "night": return R.drawable.ic_weather_night;
+ default: return R.drawable.ic_weather_cloudy;
+ }
+ }
+
private void editUrl() {
edit("Home Assistant address", config.url.isEmpty() ? "http://" : config.url,
InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_URI, v -> {
diff --git a/addons/smart-home/app/src/main/res/drawable/ic_weather_clear.xml b/addons/smart-home/app/src/main/res/drawable/ic_weather_clear.xml
new file mode 100644
index 000000000..7792858b4
--- /dev/null
+++ b/addons/smart-home/app/src/main/res/drawable/ic_weather_clear.xml
@@ -0,0 +1,15 @@
+<?xml version="1.0" encoding="utf-8"?>
+<vector xmlns:android="http://schemas.android.com/apk/res/android"
+ android:width="48dp"
+ android:height="48dp"
+ android:viewportWidth="48"
+ android:viewportHeight="48">
+ <path
+ android:fillColor="#F5C542"
+ android:pathData="M24,14a10,10 0 1,0 0,20a10,10 0 1,0 0,-20z" />
+ <path
+ android:strokeColor="#F5C542"
+ android:strokeWidth="2.5"
+ android:strokeLineCap="round"
+ android:pathData="M24,4v4M24,40v4M4,24h4M40,24h4M9.5,9.5l2.8,2.8M35.7,35.7l2.8,2.8M9.5,38.5l2.8,-2.8M35.7,12.3l2.8,-2.8" />
+</vector>
diff --git a/addons/smart-home/app/src/main/res/drawable/ic_weather_cloudy.xml b/addons/smart-home/app/src/main/res/drawable/ic_weather_cloudy.xml
new file mode 100644
index 000000000..1fe33fa04
--- /dev/null
+++ b/addons/smart-home/app/src/main/res/drawable/ic_weather_cloudy.xml
@@ -0,0 +1,10 @@
+<?xml version="1.0" encoding="utf-8"?>
+<vector xmlns:android="http://schemas.android.com/apk/res/android"
+ android:width="48dp"
+ android:height="48dp"
+ android:viewportWidth="48"
+ android:viewportHeight="48">
+ <path
+ android:fillColor="#C9D1D9"
+ android:pathData="M16,32c-4.4,0 -8,-3.1 -8,-7s3.6,-7 8,-7c0.7,-4.5 4.6,-8 9.4,-8 4.6,0 8.4,3.2 9.3,7.5 3.8,0.4 6.7,3.5 6.7,7.3 0,4.1 -3.5,7.2 -7.8,7.2H16z" />
+</vector>
diff --git a/addons/smart-home/app/src/main/res/drawable/ic_weather_fog.xml b/addons/smart-home/app/src/main/res/drawable/ic_weather_fog.xml
new file mode 100644
index 000000000..17e927084
--- /dev/null
+++ b/addons/smart-home/app/src/main/res/drawable/ic_weather_fog.xml
@@ -0,0 +1,12 @@
+<?xml version="1.0" encoding="utf-8"?>
+<vector xmlns:android="http://schemas.android.com/apk/res/android"
+ android:width="48dp"
+ android:height="48dp"
+ android:viewportWidth="48"
+ android:viewportHeight="48">
+ <path
+ android:strokeColor="#C9D1D9"
+ android:strokeWidth="3"
+ android:strokeLineCap="round"
+ android:pathData="M10,18h28M8,26h32M12,34h24" />
+</vector>
diff --git a/addons/smart-home/app/src/main/res/drawable/ic_weather_night.xml b/addons/smart-home/app/src/main/res/drawable/ic_weather_night.xml
new file mode 100644
index 000000000..aa5fd1979
--- /dev/null
+++ b/addons/smart-home/app/src/main/res/drawable/ic_weather_night.xml
@@ -0,0 +1,10 @@
+<?xml version="1.0" encoding="utf-8"?>
+<vector xmlns:android="http://schemas.android.com/apk/res/android"
+ android:width="48dp"
+ android:height="48dp"
+ android:viewportWidth="48"
+ android:viewportHeight="48">
+ <path
+ android:fillColor="#E6EDF3"
+ android:pathData="M30,8c-7.7,0 -14,6.3 -14,14s6.3,14 14,14c2.6,0 5,-0.7 7.1,-1.9C34.2,37.4 29.4,40 24,40 15.2,40 8,32.8 8,24S15.2,8 24,8c2.4,0 4.7,0.5 6.8,1.4C30.5,8.5 30.3,8 30,8z" />
+</vector>
diff --git a/addons/smart-home/app/src/main/res/drawable/ic_weather_rain.xml b/addons/smart-home/app/src/main/res/drawable/ic_weather_rain.xml
new file mode 100644
index 000000000..3305aafdd
--- /dev/null
+++ b/addons/smart-home/app/src/main/res/drawable/ic_weather_rain.xml
@@ -0,0 +1,15 @@
+<?xml version="1.0" encoding="utf-8"?>
+<vector xmlns:android="http://schemas.android.com/apk/res/android"
+ android:width="48dp"
+ android:height="48dp"
+ android:viewportWidth="48"
+ android:viewportHeight="48">
+ <path
+ android:fillColor="#8B949E"
+ android:pathData="M16,24c-4.4,0 -8,-3.1 -8,-7s3.6,-7 8,-7c0.7,-4.5 4.6,-8 9.4,-8 4.6,0 8.4,3.2 9.3,7.5 3.8,0.4 6.7,3.5 6.7,7.3 0,4.1 -3.5,7.2 -7.8,7.2H16z" />
+ <path
+ android:strokeColor="#58A6FF"
+ android:strokeWidth="2.5"
+ android:strokeLineCap="round"
+ android:pathData="M18,30v6M24,32v6M30,30v6" />
+</vector>
diff --git a/addons/smart-home/app/src/main/res/drawable/ic_weather_snow.xml b/addons/smart-home/app/src/main/res/drawable/ic_weather_snow.xml
new file mode 100644
index 000000000..063017886
--- /dev/null
+++ b/addons/smart-home/app/src/main/res/drawable/ic_weather_snow.xml
@@ -0,0 +1,13 @@
+<?xml version="1.0" encoding="utf-8"?>
+<vector xmlns:android="http://schemas.android.com/apk/res/android"
+ android:width="48dp"
+ android:height="48dp"
+ android:viewportWidth="48"
+ android:viewportHeight="48">
+ <path
+ android:fillColor="#8B949E"
+ android:pathData="M16,24c-4.4,0 -8,-3.1 -8,-7s3.6,-7 8,-7c0.7,-4.5 4.6,-8 9.4,-8 4.6,0 8.4,3.2 9.3,7.5 3.8,0.4 6.7,3.5 6.7,7.3 0,4.1 -3.5,7.2 -7.8,7.2H16z" />
+ <path
+ android:fillColor="#E6EDF3"
+ android:pathData="M18,32a1.5,1.5 0 1,0 0.01,0zM24,34a1.5,1.5 0 1,0 0.01,0zM30,32a1.5,1.5 0 1,0 0.01,0zM21,38a1.5,1.5 0 1,0 0.01,0zM27,38a1.5,1.5 0 1,0 0.01,0z" />
+</vector>
diff --git a/addons/smart-home/app/src/main/res/drawable/ic_weather_storm.xml b/addons/smart-home/app/src/main/res/drawable/ic_weather_storm.xml
new file mode 100644
index 000000000..a2cd141a7
--- /dev/null
+++ b/addons/smart-home/app/src/main/res/drawable/ic_weather_storm.xml
@@ -0,0 +1,13 @@
+<?xml version="1.0" encoding="utf-8"?>
+<vector xmlns:android="http://schemas.android.com/apk/res/android"
+ android:width="48dp"
+ android:height="48dp"
+ android:viewportWidth="48"
+ android:viewportHeight="48">
+ <path
+ android:fillColor="#6E7681"
+ android:pathData="M16,22c-4.4,0 -8,-3.1 -8,-7s3.6,-7 8,-7c0.7,-4.5 4.6,-8 9.4,-8 4.6,0 8.4,3.2 9.3,7.5 3.8,0.4 6.7,3.5 6.7,7.3 0,4.1 -3.5,7.2 -7.8,7.2H16z" />
+ <path
+ android:fillColor="#F5C542"
+ android:pathData="M26,24l-6,10h5l-2,10 10,-14h-5l4,-6z" />
+</vector>
diff --git a/addons/smart-home/app/src/main/res/xml/module_card.xml b/addons/smart-home/app/src/main/res/xml/module_card.xml
index 3f637b3ed..118f99572 100644
--- a/addons/smart-home/app/src/main/res/xml/module_card.xml
+++ b/addons/smart-home/app/src/main/res/xml/module_card.xml
@@ -1,2 +1,2 @@
<?xml version="1.0" encoding="utf-8"?>
-<card spec="1" label="Smart Home" caption="Lights" accent="#41BDF5" />
+<card spec="1" label="Smart Home" caption="At a glance" accent="#41BDF5" />
diff --git a/addons/smart-home/app/src/test/java/io/github/maxhayim/smarthome/ConfigTest.java b/addons/smart-home/app/src/test/java/io/github/maxhayim/smarthome/ConfigTest.java
new file mode 100644
index 000000000..7d5f41b29
--- /dev/null
+++ b/addons/smart-home/app/src/test/java/io/github/maxhayim/smarthome/ConfigTest.java
@@ -0,0 +1,31 @@
+package io.github.maxhayim.smarthome;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+import org.json.JSONArray;
+import org.junit.Test;
+
+import java.util.Arrays;
+import java.util.List;
+
+public class ConfigTest {
+ @Test
+ public void glanceFromJsonArrayKeepsOrderAndDropsBlanks() throws Exception {
+ JSONArray a = new JSONArray();
+ a.put("weather.home");
+ a.put(" ");
+ a.put("sensor.station_e10");
+ a.put("weather.home");
+ List<String> ids = Config.parseGlance(a);
+ assertEquals(Arrays.asList("weather.home", "sensor.station_e10"), ids);
+ }
+
+ @Test
+ public void glanceFromCommaString() {
+ assertEquals(Arrays.asList("weather.home", "sensor.e10"),
+ Config.parseGlance("weather.home, sensor.e10"));
+ assertTrue(Config.parseGlance("").isEmpty());
+ assertTrue(Config.parseGlance(null).isEmpty());
+ }
+}
diff --git a/addons/smart-home/app/src/test/java/io/github/maxhayim/smarthome/HaClientTest.java b/addons/smart-home/app/src/test/java/io/github/maxhayim/smarthome/HaClientTest.java
index bde85c590..c77e068cb 100644
--- a/addons/smart-home/app/src/test/java/io/github/maxhayim/smarthome/HaClientTest.java
+++ b/addons/smart-home/app/src/test/java/io/github/maxhayim/smarthome/HaClientTest.java
@@ -75,7 +75,7 @@ public class HaClientTest {
HaClient.Reply r = new HaClient(base(), "good").get("/api/states");
assertEquals(200, r.code);
assertEquals("Bearer good", auth.get());
- assertEquals(10, Ha.parse(r.body).size());
+ assertEquals(13, Ha.parse(r.body).size());
}
@Test
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 ce03f12d5..fc5fb45ad 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
@@ -7,6 +7,8 @@ import static org.junit.Assert.assertTrue;
import org.junit.Test;
+import java.util.Arrays;
+import java.util.Collections;
import java.util.List;
/** Against the shapes Home Assistant's REST API documents for GET /api/states and the service calls. */
@@ -26,7 +28,11 @@ public class HaTest {
+ "{\"entity_id\":\"binary_sensor.front_door\",\"state\":\"off\",\"attributes\":{\"friendly_name\":\"Front door\",\"device_class\":\"door\"}},"
+ "{\"entity_id\":\"binary_sensor.motion\",\"state\":\"on\",\"attributes\":{\"device_class\":\"motion\"}},"
+ "{\"entity_id\":\"lock.front_door\",\"state\":\"locked\",\"attributes\":{\"friendly_name\":\"Front door lock\"}},"
- + "{\"entity_id\":\"light.garage\",\"state\":\"unavailable\",\"attributes\":{\"friendly_name\":\"Garage\"}}"
+ + "{\"entity_id\":\"light.garage\",\"state\":\"unavailable\",\"attributes\":{\"friendly_name\":\"Garage\"}},"
+ + "{\"entity_id\":\"weather.home\",\"state\":\"partlycloudy\",\"attributes\":{\"friendly_name\":\"Home\",\"temperature\":21.3}},"
+ + "{\"entity_id\":\"sensor.station_e10\",\"state\":\"1.689\",\"attributes\":{\"friendly_name\":\"E10\",\"unit_of_measurement\":\"€/L\"}},"
+ + "{\"entity_id\":\"sensor.station_diesel\",\"state\":\"1.549\",\"attributes\":{\"friendly_name\":\"Diesel\",\"unit_of_measurement\":\"EUR/L\"}},"
+ + "{\"entity_id\":\"sensor.other_money\",\"state\":\"12\",\"attributes\":{\"friendly_name\":\"Balance\",\"unit_of_measurement\":\"USD\"}}"
+ "]";
private static Ha.Entity find(List<Ha.Entity> all, String id) {
@@ -40,9 +46,10 @@ public class HaTest {
assertNull(find(all, "sun.sun"));
assertNull(find(all, "sensor.power"));
assertNull(find(all, "binary_sensor.motion"));
- assertEquals(Ha.Kind.SCENE, all.get(0).kind);
+ assertNull("non-euro money stays out", find(all, "sensor.other_money"));
+ assertEquals(Ha.Kind.WEATHER, all.get(0).kind);
assertEquals(Ha.Kind.LOCK, all.get(all.size() - 1).kind);
- assertEquals(10, all.size());
+ assertEquals(13, all.size());
}
@Test
@@ -57,6 +64,49 @@ public class HaTest {
assertEquals("Locked", find(all, "lock.front_door").value());
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());
+ }
+
+ @Test
+ public void glancePicksWeatherAndFuelPrices() throws Exception {
+ List<Ha.Entity> all = Ha.parse(STATES);
+ Ha.Glance g = Ha.glance(all, Collections.emptyList());
+ 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);
+ assertFalse(g.empty());
+ }
+
+ @Test
+ public void glanceUsesNamedIdsInOrder() throws Exception {
+ List<Ha.Entity> all = Ha.parse(STATES);
+ Ha.Glance g = Ha.glance(all, Arrays.asList("sensor.station_diesel", "weather.home", "sensor.station_e10"));
+ assertEquals("weather.home", g.weather.id);
+ assertEquals("sensor.station_diesel", g.prices.get(0).id);
+ assertEquals("sensor.station_e10", g.prices.get(1).id);
+ assertTrue(g.text.startsWith("21.3° · Diesel"));
+ }
+
+ @Test
+ public void weatherAndPricesStayOutOfTheRemoteList() throws Exception {
+ List<Ha.Entity> all = Ha.parse(STATES);
+ assertFalse(Ha.inList(find(all, "weather.home")));
+ assertFalse(Ha.inList(find(all, "sensor.station_e10")));
+ assertTrue(Ha.inList(find(all, "light.living_room")));
+ }
+
+ @Test
+ public void weatherIcons() {
+ assertEquals("clear", Ha.weatherIcon("sunny"));
+ assertEquals("night", Ha.weatherIcon("clear-night"));
+ assertEquals("rain", Ha.weatherIcon("pouring"));
+ assertEquals("snow", Ha.weatherIcon("snowy"));
+ assertEquals("storm", Ha.weatherIcon("lightning-rainy"));
+ assertEquals("fog", Ha.weatherIcon("fog"));
+ assertEquals("cloudy", Ha.weatherIcon("partlycloudy"));
+ assertEquals("cloudy", Ha.weatherIcon("unknown-condition"));
}
@Test
@@ -82,6 +132,7 @@ public class HaTest {
assertNull(Ha.press(find(all, "sensor.nursery_temperature")));
assertNull(Ha.press(find(all, "light.garage")));
assertNull("switches don't dim", Ha.step(find(all, "switch.fan_plug"), 1));
+ assertNull(Ha.press(find(all, "weather.home")));
}
@Test
diff --git a/addons/smart-home/install.sh b/addons/smart-home/install.sh
index b74b26b85..88edef48f 100755
--- a/addons/smart-home/install.sh
+++ b/addons/smart-home/install.sh
@@ -1,6 +1,7 @@
#!/usr/bin/env bash
# Build Smart Home, install it on the TV and, optionally, push its Home Assistant address and token.
-# TV_HOST=<tv ip> addons/smart-home/install.sh [config.json] # {"url": "http://192.168.1.10:8123", "token": "..."}
+# TV_HOST=<tv ip> addons/smart-home/install.sh [config.json]
+# # {"url": "http://192.168.1.10:8123", "token": "...", "glance": ["weather.home", "sensor.station_e10"]}
set -euo pipefail
CONFIG="${1:-}"
if [ -n "$CONFIG" ]; then