From b6ac9f985f621eacb2af79add402336fb5f06e18 Mon Sep 17 00:00:00 2001 From: Yegor Vialov Date: Sat, 15 Dec 2018 14:37:00 +0200 Subject: [PATCH] Entity state by device class --- lib/entity_class/entity.class.dart | 51 ++++++++++++++++++++++ lib/entity_widgets/state/simple_state.dart | 2 +- 2 files changed, 52 insertions(+), 1 deletion(-) diff --git a/lib/entity_class/entity.class.dart b/lib/entity_class/entity.class.dart index 3a1c001..063a721 100644 --- a/lib/entity_class/entity.class.dart +++ b/lib/entity_class/entity.class.dart @@ -12,10 +12,60 @@ class Entity { "sensor" ]; + static Map StateByDeviceClass = { + "battery.on": "Low", + "battery.off": "Normal", + "cold.on": "Cold", + "cold.off": "Normal", + "connectivity.on": "Connected", + "connectivity.off": "Diconnected", + "door.on": "Open", + "door.off": "Closed", + "garage_door.on": "Open", + "garage_door.off": "Closed", + "gas.on": "Detected", + "gas.off": "Clear", + "heat.on": "Hot", + "heat.off": "Normal", + "light.on": "Detected", + "lignt.off": "No light", + "lock.on": "Unlocked", + "lock.off": "Locked", + "moisture.on": "Wet", + "moisture.off": "Dry", + "motion.on": "Detected", + "motion.off": "Cler", + "moving.on": "Moving", + "moving.off": "Stopped", + "occupancy.on": "Occupied", + "occupancy.off": "Clear", + "opening.on": "Open", + "opening.off": "Closed", + "plug.on": "Plugged in", + "plug.off": "Unplugged", + "power.on": "Powered", + "power.off": "No power", + "presence.on": "Home", + "presence.off": "Away", + "problem.on": "Problem", + "problem.off": "OK", + "safety.on": "Unsafe", + "safety.off": "Safe", + "smoke.on": "Detected", + "smoke.off": "Clear", + "sound.on": "Detected", + "sound.off": "Clear", + "vibration.on": "Detected", + "vibration.off": "Clear", + "window.on": "Open", + "window.off": "Closed" + }; + Map attributes; String domain; String entityId; String state; + String displayState; DateTime _lastUpdated; List childEntities = []; @@ -52,6 +102,7 @@ class Entity { entityId = rawData["entity_id"]; deviceClass = attributes["device_class"]; state = rawData["state"]; + displayState = Entity.StateByDeviceClass["$deviceClass.$state"] ?? state; _lastUpdated = DateTime.tryParse(rawData["last_updated"]); } diff --git a/lib/entity_widgets/state/simple_state.dart b/lib/entity_widgets/state/simple_state.dart index 3682c65..03b6083 100644 --- a/lib/entity_widgets/state/simple_state.dart +++ b/lib/entity_widgets/state/simple_state.dart @@ -12,7 +12,7 @@ class SimpleEntityState extends StatelessWidget { @override Widget build(BuildContext context) { final entityModel = EntityModel.of(context); - String state = entityModel.entityWrapper.entity.state ?? ""; + String state = entityModel.entityWrapper.entity.displayState ?? ""; state = state.replaceAll("\n", "").replaceAll("\t", " ").trim(); while (state.contains(" ")){ state = state.replaceAll(" ", " ");