Resolves #107 Show entity attributes

This commit is contained in:
Yegor Vialov 2018-10-07 15:03:51 +03:00
parent ce69f044fb
commit a4ac40b366
7 changed files with 153 additions and 77 deletions

View File

@ -1,14 +1,14 @@
part of '../main.dart';
class _DateTimeEntityWidgetState extends _EntityWidgetState {
bool get hasDate => widget.entity._attributes["has_date"] ?? false;
bool get hasTime => widget.entity._attributes["has_time"] ?? false;
int get year => widget.entity._attributes["year"] ?? 1970;
int get month => widget.entity._attributes["month"] ?? 1;
int get day => widget.entity._attributes["day"] ?? 1;
int get hour => widget.entity._attributes["hour"] ?? 0;
int get minute => widget.entity._attributes["minute"] ?? 0;
int get second => widget.entity._attributes["second"] ?? 0;
bool get hasDate => widget.entity.attributes["has_date"] ?? false;
bool get hasTime => widget.entity.attributes["has_time"] ?? false;
int get year => widget.entity.attributes["year"] ?? 1970;
int get month => widget.entity.attributes["month"] ?? 1;
int get day => widget.entity.attributes["day"] ?? 1;
int get hour => widget.entity.attributes["hour"] ?? 0;
int get minute => widget.entity.attributes["minute"] ?? 0;
int get second => widget.entity.attributes["second"] ?? 0;
String get formattedState => _getFormattedState();
DateTime get dateTimeState => _getDateTimeState();

View File

@ -22,8 +22,9 @@ class Entity {
static const NAME_FONT_SIZE = 16.0;
static const SMALL_FONT_SIZE = 14.0;
static const INPUT_WIDTH = 160.0;
static const ROW_PADDING = 10.0;
Map _attributes;
Map attributes;
String _domain;
String _entityId;
String _state;
@ -33,23 +34,23 @@ class Entity {
List<Entity> childEntities = [];
String get displayName =>
_attributes["friendly_name"] ?? (_attributes["name"] ?? "_");
attributes["friendly_name"] ?? (attributes["name"] ?? "_");
String get domain => _domain;
String get entityId => _entityId;
String get state => _state;
set state(value) => _state = value;
String get deviceClass => _attributes["device_class"] ?? null;
String get deviceClass => attributes["device_class"] ?? null;
bool get isView =>
(_domain == "group") &&
(_attributes != null ? _attributes["view"] ?? false : false);
(attributes != null ? attributes["view"] ?? false : false);
bool get isGroup => _domain == "group";
bool get isBadge => Entity.badgeDomains.contains(_domain);
String get icon => _attributes["icon"] ?? "";
String get icon => attributes["icon"] ?? "";
bool get isOn => state == "on";
String get entityPicture => _attributes["entity_picture"];
String get unitOfMeasurement => _attributes["unit_of_measurement"] ?? "";
List get childEntityIds => _attributes["entity_id"] ?? [];
String get entityPicture => attributes["entity_picture"];
String get unitOfMeasurement => attributes["unit_of_measurement"] ?? "";
List get childEntityIds => attributes["entity_id"] ?? [];
String get lastUpdated => _getLastUpdatedFormatted();
Entity(Map rawData) {
@ -57,7 +58,7 @@ class Entity {
}
void update(Map rawData) {
_attributes = rawData["attributes"] ?? {};
attributes = rawData["attributes"] ?? {};
_domain = rawData["entity_id"].split(".")[0];
_entityId = rawData["entity_id"];
_state = rawData["state"];
@ -72,6 +73,13 @@ class Entity {
);
}
String getAttribute(String attributeName) {
if (attributes != null) {
return attributes["$attributeName"];
}
return null;
}
String _getLastUpdatedFormatted() {
if (_lastUpdated == null) {
return "-";
@ -119,6 +127,9 @@ class EntityWidget extends StatefulWidget {
@override
_EntityWidgetState createState() {
switch (entity.domain) {
case 'sun': {
return _SunEntityWidgetState();
}
case "automation":
case "input_boolean":
case "switch":
@ -156,6 +167,8 @@ class EntityWidget extends StatefulWidget {
class _EntityWidgetState extends State<EntityWidget> {
List<String> attributesToShow = ["all"];
@override
Widget build(BuildContext context) {
if (widget.widgetType == EntityWidgetType.regular) {
@ -174,7 +187,8 @@ class _EntityWidgetState extends State<EntityWidget> {
return ListView(
children: <Widget>[
_buildMainWidget(context),
_buildSecondRowWidget()
_buildSecondRowWidget(),
_buildAttributesWidget()
],
);
}
@ -200,6 +214,56 @@ class _EntityWidgetState extends State<EntityWidget> {
);
}
Widget _buildAttributesWidget() {
List<Widget> attrs = [];
if (attributesToShow.contains("all")) {
widget.entity.attributes.forEach((name, value){
attrs.add(
_buildAttributeWidget("$name", "$value")
);
});
} else {
attributesToShow.forEach((String attr) {
String attrValue = widget.entity.getAttribute("$attr");
if (attrValue != null) {
attrs.add(
_buildAttributeWidget("$attr", "$attrValue")
);
}
});
}
return Column(
children: attrs,
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
);
}
Widget _buildAttributeWidget(String name, String value) {
return Row(
children: <Widget>[
Expanded(
child: Padding(
padding: EdgeInsets.fromLTRB(Entity.LEFT_WIDGET_PADDING, Entity.ROW_PADDING, 0.0, 0.0),
child: Text(
"$name",
textAlign: TextAlign.left,
),
),
),
Expanded(
child: Padding(
padding: EdgeInsets.fromLTRB(0.0, Entity.ROW_PADDING, Entity.RIGHT_WIDGET_PADDING, 0.0),
child: Text(
"$value",
textAlign: TextAlign.right,
),
),
)
],
);
}
void openEntityPage() {
eventBus.fire(new ShowEntityPageEvent(widget.entity));
}
@ -208,9 +272,9 @@ class _EntityWidgetState extends State<EntityWidget> {
return;
}
Widget buildAdditionalWidget() {
/*Widget buildAdditionalWidget() {
return _buildSecondRowWidget();
}
}*/
Widget _buildIconWidget() {
return Padding(
@ -320,56 +384,59 @@ class _EntityWidgetState extends State<EntityWidget> {
)
);
}
return Column(
children: <Widget>[
Container(
margin: EdgeInsets.fromLTRB(0.0, 10.0, 0.0, 10.0),
width: 50.0,
height: 50.0,
decoration: new BoxDecoration(
// Circle shape
shape: BoxShape.circle,
color: Colors.white,
// The border you want
border: new Border.all(
width: 2.0,
color: iconColor,
return GestureDetector(
child: Column(
children: <Widget>[
Container(
margin: EdgeInsets.fromLTRB(0.0, 10.0, 0.0, 10.0),
width: 50.0,
height: 50.0,
decoration: new BoxDecoration(
// Circle shape
shape: BoxShape.circle,
color: Colors.white,
// The border you want
border: new Border.all(
width: 2.0,
color: iconColor,
),
),
child: Stack(
overflow: Overflow.visible,
children: <Widget>[
Positioned(
width: 46.0,
height: 46.0,
top: 0.0,
left: 0.0,
child: badgeIcon,
),
Positioned(
//width: 50.0,
bottom: -9.0,
left: -10.0,
right: -10.0,
child: Center(
child: onBadgeText,
)
)
],
),
),
child: Stack(
overflow: Overflow.visible,
children: <Widget>[
Positioned(
width: 46.0,
height: 46.0,
top: 0.0,
left: 0.0,
child: badgeIcon,
),
Positioned(
//width: 50.0,
bottom: -9.0,
left: -10.0,
right: -10.0,
child: Center(
child: onBadgeText,
)
)
],
Container(
width: 60.0,
child: Text(
"${widget.entity.displayName}",
textAlign: TextAlign.center,
style: TextStyle(fontSize: 12.0),
softWrap: true,
maxLines: 3,
overflow: TextOverflow.ellipsis,
),
),
),
Container(
width: 60.0,
child: Text(
"${widget.entity.displayName}",
textAlign: TextAlign.center,
style: TextStyle(fontSize: 12.0),
softWrap: true,
maxLines: 3,
overflow: TextOverflow.ellipsis,
),
),
],
],
),
onTap: openEntityPage,
);
}
}

View File

@ -12,8 +12,8 @@ class _SelectEntityWidgetState extends _EntityWidgetState {
@override
Widget _buildActionWidget(BuildContext context) {
_listOptions.clear();
if (widget.entity._attributes["options"] != null) {
widget.entity._attributes["options"].forEach((value){
if (widget.entity.attributes["options"] != null) {
widget.entity.attributes["options"].forEach((value){
_listOptions.add(value.toString());
});
}

View File

@ -3,9 +3,9 @@ part of '../main.dart';
class _SliderEntityWidgetState extends _EntityWidgetState {
int _multiplier = 1;
double get minValue => widget.entity._attributes["min"] ?? 0.0;
double get maxValue => widget.entity._attributes["max"] ?? 100.0;
double get valueStep => widget.entity._attributes["step"] ?? 1.0;
double get minValue => widget.entity.attributes["min"] ?? 0.0;
double get maxValue => widget.entity.attributes["max"] ?? 100.0;
double get valueStep => widget.entity.attributes["step"] ?? 1.0;
double get doubleState => double.tryParse(widget.entity.state) ?? 0.0;
@override

View File

@ -0,0 +1,8 @@
part of '../main.dart';
class _SunEntityWidgetState extends _EntityWidgetState {
@override
List<String> attributesToShow = ["all"];
}

View File

@ -5,11 +5,11 @@ class _TextEntityWidgetState extends _EntityWidgetState {
FocusNode _focusNode = FocusNode();
bool validValue = false;
int get valueMinLength => widget.entity._attributes["min"] ?? -1;
int get valueMaxLength => widget.entity._attributes["max"] ?? -1;
String get valuePattern => widget.entity._attributes["pattern"] ?? null;
bool get isTextField => widget.entity._attributes["mode"] == "text";
bool get isPasswordField => widget.entity._attributes["mode"] == "password";
int get valueMinLength => widget.entity.attributes["min"] ?? -1;
int get valueMaxLength => widget.entity.attributes["max"] ?? -1;
String get valuePattern => widget.entity.attributes["pattern"] ?? null;
bool get isTextField => widget.entity.attributes["mode"] == "text";
bool get isPasswordField => widget.entity.attributes["mode"] == "password";
@override
void initState() {

View File

@ -19,6 +19,7 @@ part 'entity_class/select_entity.class.dart';
part 'entity_class/slider_entity.class.dart';
part 'entity_class/switch_entity.class.dart';
part 'entity_class/text_entity.class.dart';
part 'entity_class/sun_entity.class.dart';
part 'settings.page.dart';
part 'home_assistant.class.dart';