This repository has been archived on 2023-11-18. You can view files and clone it, but cannot push or open issues or pull requests.
ha_client/lib/entities/entity.class.dart

257 lines
7.1 KiB
Dart
Raw Normal View History

2018-10-27 14:27:41 +03:00
part of '../main.dart';
enum StatelessEntityType {none, missed, ghost, divider, section, callService, webLink}
2018-10-28 18:07:52 +02:00
class Entity {
2018-10-27 14:27:41 +03:00
static List badgeDomains = [
"alarm_control_panel",
"binary_sensor",
"device_tracker",
"updater",
"sun",
"timer",
"sensor"
];
2018-12-15 14:37:00 +02:00
static Map StateByDeviceClass = {
"battery.on": "Low",
"battery.off": "Normal",
"cold.on": "Cold",
"cold.off": "Normal",
"connectivity.on": "Connected",
2019-06-21 12:05:55 +03:00
"connectivity.off": "Disconnected",
2018-12-15 14:37:00 +02:00
"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",
2019-01-25 21:14:27 +02:00
"motion.off": "Clear",
2018-12-15 14:37:00 +02:00
"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"
};
2018-10-27 14:27:41 +03:00
Map attributes;
String domain;
String entityId;
2019-03-19 23:07:40 +02:00
String entityPicture;
2018-10-27 14:27:41 +03:00
String state;
2018-12-15 14:37:00 +02:00
String displayState;
2020-04-25 20:38:21 +03:00
DateTime lastUpdatedTimestamp;
StatelessEntityType statelessType = StatelessEntityType.none;
2018-10-27 14:27:41 +03:00
List<Entity> childEntities = [];
2018-12-14 15:59:47 +02:00
String deviceClass;
EntityHistoryConfig historyConfig = EntityHistoryConfig(
chartType: EntityHistoryWidgetType.simple
);
2018-10-27 14:27:41 +03:00
String get displayName =>
2020-05-08 15:55:44 +03:00
attributes["friendly_name"] ??
(attributes["name"] ?? (entityId != null && entityId.contains('.')) ? entityId.split(".")[1].replaceAll("_", " ") : "");
2018-10-27 14:27:41 +03:00
bool get isView =>
(domain == "group") &&
(attributes != null ? attributes["view"] ?? false : false);
bool get isGroup => domain == "group";
bool get isBadge => Entity.badgeDomains.contains(domain);
String get icon => attributes["icon"] ?? "";
2018-11-14 15:14:46 +02:00
bool get isOn => state == EntityState.on;
2018-10-27 14:27:41 +03:00
String get unitOfMeasurement => attributes["unit_of_measurement"] ?? "";
List get childEntityIds => attributes["entity_id"] ?? [];
String get lastUpdated => _getLastUpdatedFormatted();
bool get isHidden => attributes["hidden"] ?? false;
2018-10-28 20:01:01 +02:00
double get doubleState => double.tryParse(state) ?? 0.0;
int get supportedFeatures => attributes["supported_features"] ?? 0;
2018-10-27 14:27:41 +03:00
2019-03-19 23:07:40 +02:00
String _getEntityPictureUrl(String webHost) {
String result = attributes["entity_picture"];
if (result == null) return result;
if (!result.startsWith("http")) {
if (result.startsWith("/")) {
2019-03-19 23:07:40 +02:00
result = "$webHost$result";
} else {
2019-03-19 23:07:40 +02:00
result = "$webHost/$result";
}
}
return result;
}
2019-03-19 23:07:40 +02:00
Entity(Map rawData, String webHost) {
update(rawData, webHost);
2018-10-27 14:27:41 +03:00
}
2019-03-12 23:35:33 +02:00
Entity.missed(String entityId) {
statelessType = StatelessEntityType.missed;
2019-03-12 23:35:33 +02:00
attributes = {"hidden": false};
this.entityId = entityId;
}
Entity.divider() {
statelessType = StatelessEntityType.divider;
attributes = {"hidden": false};
}
Entity.section(String label) {
statelessType = StatelessEntityType.section;
attributes = {"hidden": false, "friendly_name": "$label"};
}
Entity.ghost(String name, String icon) {
statelessType = StatelessEntityType.ghost;
attributes = {"icon": icon, "hidden": false, "friendly_name": name};
}
Entity.callService({String icon, String name, String service, String actionName}) {
statelessType = StatelessEntityType.callService;
entityId = service;
displayState = actionName?.toUpperCase() ?? "RUN";
attributes = {"hidden": false, "friendly_name": "$name", "icon": "$icon"};
}
Entity.weblink({String url, String name, String icon}) {
statelessType = StatelessEntityType.webLink;
2020-04-25 20:38:21 +03:00
entityId = "custom.custom";
attributes = {"hidden": false, "friendly_name": "${name ?? url}", "icon": "${icon ?? 'mdi:link'}"};
}
2019-03-19 23:07:40 +02:00
void update(Map rawData, String webHost) {
2018-10-27 14:27:41 +03:00
attributes = rawData["attributes"] ?? {};
domain = rawData["entity_id"] != null ? rawData["entity_id"].split(".")[0] : null;
2018-10-27 14:27:41 +03:00
entityId = rawData["entity_id"];
2018-12-14 15:59:47 +02:00
deviceClass = attributes["device_class"];
2019-11-10 15:48:05 +02:00
state = rawData["state"] is bool ? (rawData["state"] ? EntityState.on : EntityState.off) : rawData["state"];
displayState = Entity.StateByDeviceClass["$deviceClass.$state"] ?? (state.toLowerCase() == 'unknown' ? '-' : state);
2020-04-25 20:38:21 +03:00
lastUpdatedTimestamp = DateTime.tryParse(rawData["last_updated"]);
2019-03-19 23:07:40 +02:00
entityPicture = _getEntityPictureUrl(webHost);
2018-10-27 14:27:41 +03:00
}
double _getDoubleAttributeValue(String attributeName) {
var temp1 = attributes["$attributeName"];
if (temp1 is int) {
return temp1.toDouble();
} else if (temp1 is double) {
return temp1;
} else {
return double.tryParse("$temp1");
}
}
int _getIntAttributeValue(String attributeName) {
var temp1 = attributes["$attributeName"];
if (temp1 is int) {
return temp1;
} else if (temp1 is double) {
return temp1.round();
} else {
return int.tryParse("$temp1");
}
}
2018-11-23 14:11:34 +02:00
List<String> getStringListAttributeValue(String attribute) {
if (attributes["$attribute"] != null) {
List<String> result = (attributes["$attribute"] as List).cast<String>();
return result;
} else {
return null;
}
}
2018-10-27 14:27:41 +03:00
Widget buildDefaultWidget(BuildContext context) {
2018-11-15 19:08:47 +02:00
return DefaultEntityContainer(
state: _buildStatePart(context)
2018-10-27 14:27:41 +03:00
);
}
Widget _buildStatePart(BuildContext context) {
return SimpleEntityState();
}
Widget _buildStatePartForPage(BuildContext context) {
return _buildStatePart(context);
}
Widget _buildAdditionalControlsForPage(BuildContext context) {
return Container(
width: 0.0,
height: 0.0,
);
}
Widget buildBadgeWidget(BuildContext context) {
return EntityModel(
entityWrapper: EntityWrapper(entity: this),
2018-10-27 14:27:41 +03:00
child: BadgeWidget(),
handleTap: true,
);
}
String getAttribute(String attributeName) {
if (attributes != null) {
return attributes["$attributeName"].toString();
2018-10-27 14:27:41 +03:00
}
return null;
}
String _getLastUpdatedFormatted() {
2020-04-25 20:38:21 +03:00
if (lastUpdatedTimestamp == null) {
2018-10-27 14:27:41 +03:00
return "-";
} else {
DateTime now = DateTime.now();
2020-04-25 20:38:21 +03:00
Duration d = now.difference(lastUpdatedTimestamp);
2018-10-27 14:27:41 +03:00
String text;
int v;
if (d.inDays == 0) {
if (d.inHours == 0) {
if (d.inMinutes == 0) {
text = "seconds ago";
v = d.inSeconds;
} else {
text = "minutes ago";
v = d.inMinutes;
}
} else {
text = "hours ago";
v = d.inHours;
}
} else {
text = "days ago";
v = d.inDays;
}
return "$v $text";
}
}
}