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/cards/card.class.dart

562 lines
16 KiB
Dart
Raw Normal View History

2018-10-27 17:28:47 +03:00
part of '../main.dart';
2020-04-26 21:44:21 +03:00
class CardData {
String type;
List<EntityWrapper> entities = [];
List conditions;
2020-04-27 01:46:37 +03:00
bool showEmpty;
2020-04-26 21:44:21 +03:00
List stateFilter;
2020-04-29 00:03:00 +03:00
bool stateColor = true;
2020-04-26 21:44:21 +03:00
2020-04-27 01:46:37 +03:00
EntityWrapper get entity => entities.isNotEmpty ? entities[0] : null;
2020-05-04 17:47:47 +03:00
factory CardData.parse(rawData) {
try {
2020-05-07 19:14:23 +03:00
if (rawData['type'] == null) {
rawData['type'] = CardType.ENTITIES;
} else if (!(rawData['type'] is String)) {
return CardData(null);
}
switch (rawData['type']) {
2020-05-04 17:47:47 +03:00
case CardType.ENTITIES:
case CardType.HISTORY_GRAPH:
case CardType.MAP:
case CardType.PICTURE_GLANCE:
case CardType.SENSOR:
case CardType.ENTITY:
case CardType.WEATHER_FORECAST:
case CardType.PLANT_STATUS:
if (rawData['entity'] != null) {
rawData['entities'] = [rawData['entity']];
}
return EntitiesCardData(rawData);
2020-05-04 17:47:47 +03:00
break;
case CardType.ALARM_PANEL:
return AlarmPanelCardData(rawData);
break;
case CardType.ENTITY_BUTTON:
case CardType.LIGHT:
case CardType.BUTTON:
case CardType.PICTURE_ENTITY:
2020-05-04 17:47:47 +03:00
return ButtonCardData(rawData);
break;
case CardType.CONDITIONAL:
return CardData.parse(rawData['card']);
break;
case CardType.ENTITY_FILTER:
Map cardData = Map.from(rawData);
cardData.remove('type');
if (rawData.containsKey('card')) {
cardData.addAll(rawData['card']);
}
cardData['type'] ??= CardType.ENTITIES;
return CardData.parse(cardData);
break;
case CardType.GAUGE:
return GaugeCardData(rawData);
break;
case CardType.GLANCE:
case CardType.THERMOSTAT:
if (rawData['entity'] != null) {
rawData['entities'] = [rawData['entity']];
}
2020-05-04 17:47:47 +03:00
return GlanceCardData(rawData);
break;
case CardType.HORIZONTAL_STACK:
return HorizontalStackCardData(rawData);
break;
case CardType.VERTICAL_STACK:
return VerticalStackCardData(rawData);
break;
case CardType.MARKDOWN:
return MarkdownCardData(rawData);
break;
case CardType.MEDIA_CONTROL:
return MediaControlCardData(rawData);
break;
default:
2020-05-07 19:14:23 +03:00
return CardData(null);
2020-05-04 17:47:47 +03:00
}
2020-05-01 19:24:13 +03:00
} catch (error, stacktrace) {
2020-05-04 17:47:47 +03:00
Logger.e('Error parsing card $rawData: $error', stacktrace: stacktrace);
return ErrorCardData(rawData);
2020-04-26 21:44:21 +03:00
}
}
2020-05-04 17:47:47 +03:00
CardData(rawData) {
if (rawData != null && rawData is Map) {
2020-05-07 19:14:23 +03:00
type = rawData['type'];
conditions = rawData['conditions'] ?? [];
showEmpty = rawData['show_empty'] ?? true;
stateFilter = rawData['state_filter'] ?? [];
} else {
type = CardType.UNKNOWN;
conditions = [];
showEmpty = true;
stateFilter = [];
}
2020-04-26 21:44:21 +03:00
}
2020-04-27 01:46:37 +03:00
Widget buildCardWidget() {
return UnsupportedCard(card: this);
}
2020-04-26 21:44:21 +03:00
List<EntityWrapper> getEntitiesToShow() {
return entities.where((entityWrapper) {
if (entityWrapper.entity.isHidden) {
2020-04-26 21:44:21 +03:00
return false;
}
List currentStateFilter;
if (entityWrapper.stateFilter != null && entityWrapper.stateFilter.isNotEmpty) {
currentStateFilter = entityWrapper.stateFilter;
} else {
currentStateFilter = stateFilter;
}
bool showByFilter = currentStateFilter.isEmpty;
for (var allowedState in currentStateFilter) {
if (allowedState is String && allowedState == entityWrapper.entity.state) {
showByFilter = true;
break;
} else if (allowedState is Map) {
try {
var tmpVal = allowedState['attribute'] != null ? entityWrapper.entity.getAttribute(allowedState['attribute']) : entityWrapper.entity.state;
var valToCompareWith = allowedState['value'];
var valToCompare;
if (valToCompareWith is! String && tmpVal is String) {
valToCompare = double.tryParse(tmpVal);
} else {
valToCompare = tmpVal;
}
if (valToCompare != null) {
bool result;
switch (allowedState['operator']) {
case '<=': { result = valToCompare <= valToCompareWith;}
break;
case '<': { result = valToCompare < valToCompareWith;}
break;
case '>=': { result = valToCompare >= valToCompareWith;}
break;
case '>': { result = valToCompare > valToCompareWith;}
break;
case '!=': { result = valToCompare != valToCompareWith;}
break;
case 'regex': {
RegExp regExp = RegExp(valToCompareWith.toString());
result = regExp.hasMatch(valToCompare.toString());
}
break;
default: {
result = valToCompare == valToCompareWith;
}
}
if (result) {
showByFilter = true;
break;
}
}
2020-05-01 19:24:13 +03:00
} catch (e, stacktrace) {
Logger.e('Error filtering ${entityWrapper.entity.entityId} by $allowedState: $e', stacktrace: stacktrace);
2020-04-26 21:44:21 +03:00
}
}
}
return showByFilter;
}).toList();
}
}
class EntitiesCardData extends CardData {
String title;
String icon;
bool showHeaderToggle;
2020-04-27 01:46:37 +03:00
@override
Widget buildCardWidget() {
return EntitiesCard(card: this);
}
2020-04-26 21:44:21 +03:00
2020-05-04 17:47:47 +03:00
EntitiesCardData(rawData) : super(rawData) {
2020-04-26 21:44:21 +03:00
//Parsing card data
2020-05-08 15:37:53 +03:00
title = rawData['title'];
icon = '${rawData['icon']}';
2020-04-29 00:03:00 +03:00
stateColor = rawData['state_color'] ?? false;
showHeaderToggle = rawData['show_header_toggle'] ?? false;
2020-04-26 21:44:21 +03:00
//Parsing entities
2020-05-08 15:37:53 +03:00
var rawEntities = rawData['entities'] ?? [];
2020-04-26 21:44:21 +03:00
rawEntities.forEach((rawEntity) {
if (rawEntity is String) {
if (HomeAssistant().entities.isExist(rawEntity)) {
entities.add(EntityWrapper(entity: HomeAssistant().entities.get(rawEntity)));
} else {
entities.add(EntityWrapper(entity: Entity.missed(rawEntity)));
}
} else {
if (rawEntity["type"] == "divider") {
entities.add(EntityWrapper(entity: Entity.divider()));
} else if (rawEntity["type"] == "section") {
entities.add(EntityWrapper(entity: Entity.section(rawEntity["label"] ?? "")));
} else if (rawEntity["type"] == "call-service") {
Map uiActionData = {
"tap_action": {
"action": EntityUIAction.callService,
"service": rawEntity["service"],
"service_data": rawEntity["service_data"]
},
"hold_action": EntityUIAction.none
};
entities.add(
EntityWrapper(
entity: Entity.callService(
2020-04-29 00:03:00 +03:00
icon: rawEntity["icon"],
name: rawEntity["name"],
service: rawEntity["service"],
actionName: rawEntity["action_name"]
),
stateColor: rawEntity["state_color"] ?? stateColor,
uiAction: EntityUIAction(rawEntityData: uiActionData)
)
2020-04-26 21:44:21 +03:00
);
} else if (rawEntity["type"] == "weblink") {
Map uiActionData = {
"tap_action": {
"action": EntityUIAction.navigate,
"service": rawEntity["url"]
},
"hold_action": EntityUIAction.none
};
entities.add(EntityWrapper(
entity: Entity.weblink(
icon: rawEntity["icon"],
name: rawEntity["name"],
url: rawEntity["url"]
),
2020-04-29 00:03:00 +03:00
stateColor: rawEntity["state_color"] ?? stateColor,
2020-04-26 21:44:21 +03:00
uiAction: EntityUIAction(rawEntityData: uiActionData)
)
);
} else if (HomeAssistant().entities.isExist(rawEntity["entity"])) {
Entity e = HomeAssistant().entities.get(rawEntity["entity"]);
entities.add(
EntityWrapper(
entity: e,
2020-04-29 00:03:00 +03:00
stateColor: rawEntity["state_color"] ?? stateColor,
2020-04-26 21:44:21 +03:00
overrideName: rawEntity["name"],
overrideIcon: rawEntity["icon"],
stateFilter: rawEntity['state_filter'] ?? [],
uiAction: EntityUIAction(rawEntityData: rawEntity)
)
);
} else {
entities.add(EntityWrapper(entity: Entity.missed(rawEntity["entity"])));
}
}
});
}
}
class AlarmPanelCardData extends CardData {
String name;
2020-04-27 01:46:37 +03:00
List<dynamic> states;
@override
Widget buildCardWidget() {
return AlarmPanelCard(card: this);
}
2020-04-26 21:44:21 +03:00
2020-05-04 17:47:47 +03:00
AlarmPanelCardData(rawData) : super(rawData) {
2020-04-26 21:44:21 +03:00
//Parsing card data
name = rawData['name'];
states = rawData['states'];
//Parsing entity
var entitiId = rawData["entity"];
if (entitiId != null && entitiId is String) {
if (HomeAssistant().entities.isExist(entitiId)) {
entities.add(EntityWrapper(
entity: HomeAssistant().entities.get(entitiId),
2020-04-29 00:03:00 +03:00
stateColor: true,
2020-04-26 21:44:21 +03:00
overrideName: name
));
} else {
entities.add(EntityWrapper(entity: Entity.missed(entitiId)));
}
}
}
}
class ButtonCardData extends CardData {
String name;
String icon;
bool showName;
bool showIcon;
2020-04-27 23:56:33 +03:00
double iconHeightPx = 0;
double iconHeightRem = 0;
2020-04-26 21:44:21 +03:00
2020-04-27 01:46:37 +03:00
@override
Widget buildCardWidget() {
return EntityButtonCard(card: this);
}
2020-05-04 17:47:47 +03:00
ButtonCardData(rawData) : super(rawData) {
2020-04-26 21:44:21 +03:00
//Parsing card data
name = rawData['name'];
2020-05-08 15:37:53 +03:00
icon = '${rawData['icon']}';
2020-04-27 01:46:37 +03:00
showName = rawData['show_name'] ?? true;
showIcon = rawData['show_icon'] ?? true;
2020-04-29 00:03:00 +03:00
stateColor = rawData['state_color'] ?? true;
2020-04-29 10:00:28 +03:00
var rawHeight = rawData['icon_height'];
if (rawHeight != null && rawHeight is String) {
2020-04-27 23:56:33 +03:00
if (rawHeight.contains('px')) {
iconHeightPx = double.tryParse(rawHeight.replaceFirst('px', '')) ?? 0;
} else if (rawHeight.contains('rem')) {
iconHeightRem = double.tryParse(rawHeight.replaceFirst('rem', '')) ?? 0;
} else if (rawHeight.contains('em')) {
iconHeightRem = double.tryParse(rawHeight.replaceFirst('em', '')) ?? 0;
}
}
2020-04-26 21:44:21 +03:00
//Parsing entity
var entitiId = rawData["entity"];
if (entitiId != null && entitiId is String) {
if (HomeAssistant().entities.isExist(entitiId)) {
entities.add(EntityWrapper(
entity: HomeAssistant().entities.get(entitiId),
overrideName: name,
overrideIcon: icon,
2020-04-29 00:03:00 +03:00
stateColor: stateColor,
2020-04-26 21:44:21 +03:00
uiAction: EntityUIAction(
rawEntityData: rawData
)
));
} else {
entities.add(EntityWrapper(entity: Entity.missed(entitiId)));
}
} else if (entitiId == null) {
entities.add(
EntityWrapper(
entity: Entity.ghost(
name,
icon,
),
2020-04-29 00:03:00 +03:00
stateColor: stateColor,
2020-04-26 21:44:21 +03:00
uiAction: EntityUIAction(
rawEntityData: rawData
)
)
);
}
}
}
class GaugeCardData extends CardData {
String name;
String unit;
double min;
double max;
2020-04-26 21:44:21 +03:00
Map severity;
2020-04-27 01:46:37 +03:00
@override
Widget buildCardWidget() {
return GaugeCard(card: this);
}
2020-04-26 21:44:21 +03:00
2020-05-04 17:47:47 +03:00
GaugeCardData(rawData) : super(rawData) {
2020-04-26 21:44:21 +03:00
//Parsing card data
name = rawData['name'];
unit = rawData['unit'];
if (rawData['min'] is int) {
min = rawData['min'].toDouble();
} else if (rawData['min'] is double) {
min = rawData['min'];
} else {
min = 0;
}
if (rawData['max'] is int) {
max = rawData['max'].toDouble();
} else if (rawData['max'] is double) {
max = rawData['max'];
} else {
max = 100;
}
2020-04-26 21:44:21 +03:00
severity = rawData['severity'];
//Parsing entity
2020-05-07 18:48:20 +03:00
var entitiId = rawData["entity"] is List ? rawData["entity"][0] : rawData["entity"];
2020-04-26 21:44:21 +03:00
if (entitiId != null && entitiId is String) {
if (HomeAssistant().entities.isExist(entitiId)) {
entities.add(EntityWrapper(
entity: HomeAssistant().entities.get(entitiId),
overrideName: name
));
} else {
entities.add(EntityWrapper(entity: Entity.missed(entitiId)));
}
2020-04-27 01:46:37 +03:00
} else {
2020-05-07 18:48:20 +03:00
entities.add(EntityWrapper(entity: Entity.missed('$entitiId')));
2020-04-26 21:44:21 +03:00
}
}
}
class GlanceCardData extends CardData {
String title;
bool showName;
bool showIcon;
bool showState;
bool stateColor;
int columnsCount;
2020-04-27 01:46:37 +03:00
@override
Widget buildCardWidget() {
return GlanceCard(card: this);
}
2020-04-26 21:44:21 +03:00
2020-05-04 17:47:47 +03:00
GlanceCardData(rawData) : super(rawData) {
2020-04-26 21:44:21 +03:00
//Parsing card data
title = rawData["title"];
showName = rawData['show_name'] ?? true;
showIcon = rawData['show_icon'] ?? true;
showState = rawData['show_state'] ?? true;
stateColor = rawData['state_color'] ?? true;
columnsCount = rawData['columns'] ?? 4;
//Parsing entities
var rawEntities = rawData["entities"] ?? [];
rawEntities.forEach((rawEntity) {
if (rawEntity is String) {
if (HomeAssistant().entities.isExist(rawEntity)) {
entities.add(EntityWrapper(entity: HomeAssistant().entities.get(rawEntity)));
} else {
entities.add(EntityWrapper(entity: Entity.missed(rawEntity)));
}
} else {
if (HomeAssistant().entities.isExist(rawEntity["entity"])) {
Entity e = HomeAssistant().entities.get(rawEntity["entity"]);
entities.add(
EntityWrapper(
entity: e,
2020-04-29 00:03:00 +03:00
stateColor: stateColor,
2020-04-26 21:44:21 +03:00
overrideName: rawEntity["name"],
overrideIcon: rawEntity["icon"],
stateFilter: rawEntity['state_filter'] ?? [],
uiAction: EntityUIAction(rawEntityData: rawEntity)
)
);
} else {
entities.add(EntityWrapper(entity: Entity.missed(rawEntity["entity"])));
}
}
});
}
}
class HorizontalStackCardData extends CardData {
List<CardData> childCards;
2020-04-27 01:46:37 +03:00
@override
Widget buildCardWidget() {
return HorizontalStackCard(card: this);
}
2020-04-26 21:44:21 +03:00
2020-05-04 17:47:47 +03:00
HorizontalStackCardData(rawData) : super(rawData) {
2020-05-08 16:20:01 +03:00
if (rawData.containsKey('cards') && rawData['cards'] is List) {
2020-04-27 01:46:37 +03:00
childCards = rawData['cards'].map<CardData>((childCard) {
2020-04-27 23:56:33 +03:00
return CardData.parse(childCard);
2020-04-26 21:44:21 +03:00
}).toList();
} else {
childCards = [];
}
}
}
class VerticalStackCardData extends CardData {
List<CardData> childCards;
2020-04-27 01:46:37 +03:00
@override
Widget buildCardWidget() {
return VerticalStackCard(card: this);
}
2020-04-26 21:44:21 +03:00
2020-05-04 17:47:47 +03:00
VerticalStackCardData(rawData) : super(rawData) {
2020-05-08 16:20:01 +03:00
if (rawData.containsKey('cards') && rawData['cards'] is List) {
2020-04-27 01:46:37 +03:00
childCards = rawData['cards'].map<CardData>((childCard) {
2020-04-26 21:44:21 +03:00
return CardData.parse(childCard);
}).toList();
} else {
childCards = [];
}
}
2020-04-27 01:46:37 +03:00
}
class MarkdownCardData extends CardData {
String title;
String content;
@override
Widget buildCardWidget() {
return MarkdownCard(card: this);
}
2020-05-04 17:47:47 +03:00
MarkdownCardData(rawData) : super(rawData) {
2020-04-27 01:46:37 +03:00
//Parsing card data
title = rawData['title'];
content = rawData['content'];
}
}
class MediaControlCardData extends CardData {
@override
Widget buildCardWidget() {
return MediaControlsCard(card: this);
}
2020-05-04 17:47:47 +03:00
MediaControlCardData(rawData) : super(rawData) {
2020-04-27 01:46:37 +03:00
var entitiId = rawData["entity"];
if (entitiId != null && entitiId is String) {
if (HomeAssistant().entities.isExist(entitiId)) {
entities.add(EntityWrapper(
entity: HomeAssistant().entities.get(entitiId),
));
} else {
entities.add(EntityWrapper(entity: Entity.missed(entitiId)));
}
}
}
2020-04-29 10:00:28 +03:00
}
class ErrorCardData extends CardData {
String cardConfig;
@override
Widget buildCardWidget() {
return ErrorCard(card: this);
}
2020-05-04 17:47:47 +03:00
ErrorCardData(rawData) : super(rawData) {
cardConfig = '$rawData';
}
}