2018-10-27 14:27:41 +03:00
|
|
|
part of '../main.dart';
|
|
|
|
|
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"
|
|
|
|
];
|
|
|
|
|
|
|
|
Map attributes;
|
|
|
|
String domain;
|
|
|
|
String entityId;
|
|
|
|
String state;
|
|
|
|
DateTime _lastUpdated;
|
|
|
|
|
|
|
|
List<Entity> childEntities = [];
|
|
|
|
List<String> attributesToShow = ["all"];
|
2018-10-29 00:58:52 +02:00
|
|
|
EntityHistoryConfig historyConfig = EntityHistoryConfig(
|
|
|
|
chartType: EntityHistoryWidgetType.simple
|
|
|
|
);
|
2018-10-27 14:27:41 +03:00
|
|
|
|
|
|
|
String get displayName =>
|
2018-11-04 22:25:22 +02:00
|
|
|
attributes["friendly_name"] ?? (attributes["name"] ?? entityId.split(".")[1].replaceAll("_", " "));
|
2018-10-27 14:27:41 +03:00
|
|
|
|
|
|
|
String get deviceClass => attributes["device_class"] ?? null;
|
|
|
|
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 entityPicture => attributes["entity_picture"];
|
|
|
|
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;
|
2018-10-27 14:27:41 +03:00
|
|
|
|
|
|
|
Entity(Map rawData) {
|
|
|
|
update(rawData);
|
|
|
|
}
|
|
|
|
|
|
|
|
void update(Map rawData) {
|
|
|
|
attributes = rawData["attributes"] ?? {};
|
|
|
|
domain = rawData["entity_id"].split(".")[0];
|
|
|
|
entityId = rawData["entity_id"];
|
|
|
|
state = rawData["state"];
|
|
|
|
_lastUpdated = DateTime.tryParse(rawData["last_updated"]);
|
|
|
|
}
|
|
|
|
|
|
|
|
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 buildEntityPageWidget(BuildContext context) {
|
|
|
|
return EntityModel(
|
2018-11-16 22:32:43 +02:00
|
|
|
entityWrapper: EntityWrapper(entity: this),
|
2018-10-27 14:27:41 +03:00
|
|
|
child: EntityPageContainer(children: <Widget>[
|
2018-11-04 22:57:53 +02:00
|
|
|
DefaultEntityContainer(state: _buildStatePartForPage(context)),
|
2018-10-27 14:27:41 +03:00
|
|
|
LastUpdatedWidget(),
|
|
|
|
Divider(),
|
|
|
|
_buildAdditionalControlsForPage(context),
|
2018-11-11 18:49:04 +02:00
|
|
|
Divider(),
|
|
|
|
buildHistoryWidget(),
|
2018-10-27 14:27:41 +03:00
|
|
|
EntityAttributesList()
|
|
|
|
]),
|
|
|
|
handleTap: false,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-10-28 14:56:23 +02:00
|
|
|
Widget buildHistoryWidget() {
|
|
|
|
return EntityHistoryWidget(
|
2018-10-29 00:58:52 +02:00
|
|
|
config: historyConfig,
|
2018-10-28 14:56:23 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-10-27 14:27:41 +03:00
|
|
|
Widget buildBadgeWidget(BuildContext context) {
|
|
|
|
return EntityModel(
|
2018-11-16 22:32:43 +02:00
|
|
|
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"];
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
String _getLastUpdatedFormatted() {
|
|
|
|
if (_lastUpdated == null) {
|
|
|
|
return "-";
|
|
|
|
} else {
|
|
|
|
DateTime now = DateTime.now();
|
|
|
|
Duration d = now.difference(_lastUpdated);
|
|
|
|
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";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|