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

502 lines
14 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-27 01:46:37 +03:00
EntityWrapper get entity => entities.isNotEmpty ? entities[0] : null;
2020-04-27 23:56:33 +03:00
factory CardData.parse(Map<String, dynamic> rawData) {
2020-04-26 21:44:21 +03:00
switch (rawData['type']) {
case CardType.ENTITIES:
2020-04-27 23:56:33 +03:00
return EntitiesCardData(rawData);
2020-04-26 21:44:21 +03:00
break;
case CardType.ALARM_PANEL:
2020-04-27 23:56:33 +03:00
return AlarmPanelCardData(rawData);
2020-04-26 21:44:21 +03:00
break;
case CardType.BUTTON:
2020-04-27 23:56:33 +03:00
return ButtonCardData(rawData);
2020-04-26 21:44:21 +03:00
break;
case CardType.ENTITY_BUTTON:
2020-04-27 23:56:33 +03:00
return ButtonCardData(rawData);
2020-04-26 21:44:21 +03:00
break;
case CardType.CONDITIONAL:
2020-04-27 23:56:33 +03:00
return CardData.parse(rawData['card']);
2020-04-26 21:44:21 +03:00
break;
case CardType.ENTITY_FILTER:
Map<String, dynamic> cardData = Map.from(rawData);
cardData.remove('type');
if (rawData.containsKey('card')) {
cardData.addAll(rawData['card']);
}
cardData['type'] ??= CardType.ENTITIES;
2020-04-27 23:56:33 +03:00
return CardData.parse(cardData);
2020-04-26 21:44:21 +03:00
break;
case CardType.GAUGE:
2020-04-27 23:56:33 +03:00
return GaugeCardData(rawData);
2020-04-26 21:44:21 +03:00
break;
case CardType.GLANCE:
2020-04-27 23:56:33 +03:00
return GlanceCardData(rawData);
2020-04-26 21:44:21 +03:00
break;
case CardType.HORIZONTAL_STACK:
2020-04-27 23:56:33 +03:00
return HorizontalStackCardData(rawData);
2020-04-26 21:44:21 +03:00
break;
case CardType.VERTICAL_STACK:
2020-04-27 23:56:33 +03:00
return VerticalStackCardData(rawData);
2020-04-26 21:44:21 +03:00
break;
2020-04-27 01:46:37 +03:00
case CardType.MARKDOWN:
2020-04-27 23:56:33 +03:00
return MarkdownCardData(rawData);
2020-04-27 01:46:37 +03:00
break;
case CardType.MEDIA_CONTROL:
2020-04-27 23:56:33 +03:00
return MediaControlCardData(rawData);
2020-04-27 01:46:37 +03:00
break;
2020-04-26 21:44:21 +03:00
default:
2020-04-27 01:46:37 +03:00
if (rawData.containsKey('entities')) {
2020-04-27 23:56:33 +03:00
return EntitiesCardData(rawData);
2020-04-27 01:46:37 +03:00
} else if (rawData.containsKey('entity')) {
rawData['entities'] = [rawData['entity']];
2020-04-27 23:56:33 +03:00
return EntitiesCardData(rawData);
2020-04-27 01:46:37 +03:00
}
2020-04-27 23:56:33 +03:00
return CardData(rawData);
2020-04-26 21:44:21 +03:00
}
}
2020-04-27 23:56:33 +03:00
CardData(Map<String, dynamic> rawData) {
2020-04-26 21:44:21 +03:00
type = rawData['type'] ?? CardType.ENTITIES;
2020-04-27 01:46:37 +03:00
conditions = rawData['conditions'] ?? [];
showEmpty = rawData['show_empty'] ?? true;
2020-04-26 21:44:21 +03:00
stateFilter = rawData['state_filter'] ?? [];
}
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 (HomeAssistant().autoUi && entityWrapper.entity.isHidden) {
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;
}
}
} catch (e) {
Logger.e('Error filtering ${entityWrapper.entity.entityId} by $allowedState');
Logger.e('$e');
}
}
}
return showByFilter;
}).toList();
}
}
class EntitiesCardData extends CardData {
String title;
String icon;
bool showHeaderToggle;
bool stateColor;
2020-04-27 01:46:37 +03:00
@override
Widget buildCardWidget() {
return EntitiesCard(card: this);
}
2020-04-26 21:44:21 +03:00
2020-04-27 23:56:33 +03:00
EntitiesCardData(Map<String, dynamic> rawData) : super(rawData) {
2020-04-26 21:44:21 +03:00
//Parsing card data
title = rawData["title"];
icon = rawData['icon'];
showHeaderToggle = rawData['show_header_toggle'] ?? false;
stateColor = rawData['state_color'] ?? true;
//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 (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(
icon: rawEntity["icon"],
name: rawEntity["name"],
service: rawEntity["service"],
actionName: rawEntity["action_name"]
),
uiAction: EntityUIAction(rawEntityData: uiActionData)
)
);
} 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"]
),
uiAction: EntityUIAction(rawEntityData: uiActionData)
)
);
} else if (HomeAssistant().entities.isExist(rawEntity["entity"])) {
Entity e = HomeAssistant().entities.get(rawEntity["entity"]);
entities.add(
EntityWrapper(
entity: e,
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-04-27 23:56:33 +03:00
AlarmPanelCardData(Map<String, dynamic> 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),
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-04-27 23:56:33 +03:00
ButtonCardData(Map<String, dynamic> rawData) : super(rawData) {
2020-04-26 21:44:21 +03:00
//Parsing card data
name = rawData['name'];
icon = rawData['icon'];
2020-04-27 01:46:37 +03:00
showName = rawData['show_name'] ?? true;
showIcon = rawData['show_icon'] ?? true;
2020-04-27 23:56:33 +03:00
if (rawData.containsKey('icon_height')) {
String rawHeight = rawData['icon_height'];
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,
uiAction: EntityUIAction(
rawEntityData: rawData
)
));
} else {
entities.add(EntityWrapper(entity: Entity.missed(entitiId)));
}
} else if (entitiId == null) {
entities.add(
EntityWrapper(
entity: Entity.ghost(
name,
icon,
),
uiAction: EntityUIAction(
rawEntityData: rawData
)
)
);
}
}
}
class GaugeCardData extends CardData {
String name;
String unit;
int min;
int max;
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-04-27 23:56:33 +03:00
GaugeCardData(Map<String, dynamic> rawData) : super(rawData) {
2020-04-26 21:44:21 +03:00
//Parsing card data
name = rawData['name'];
unit = rawData['unit'];
min = rawData['min'] ?? 0;
max = rawData['max'] ?? 100;
severity = rawData['severity'];
//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
));
} else {
entities.add(EntityWrapper(entity: Entity.missed(entitiId)));
}
2020-04-27 01:46:37 +03:00
} else {
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-04-27 23:56:33 +03:00
GlanceCardData(Map<String, dynamic> 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,
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-04-27 23:56:33 +03:00
HorizontalStackCardData(Map<String, dynamic> rawData) : super(rawData) {
2020-04-26 21:44:21 +03:00
if (rawData.containsKey('cards')) {
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-04-27 23:56:33 +03:00
VerticalStackCardData(Map<String, dynamic> rawData) : super(rawData) {
2020-04-26 21:44:21 +03:00
if (rawData.containsKey('cards')) {
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-04-27 23:56:33 +03:00
MarkdownCardData(Map<String, dynamic> 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-04-27 23:56:33 +03:00
MediaControlCardData(Map<String, dynamic> 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)));
}
}
}
2018-10-27 17:28:47 +03:00
}