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

487 lines
13 KiB
Dart
Raw Normal View History

2018-09-26 22:16:50 +03:00
part of 'main.dart';
class Entity {
2018-09-29 16:19:01 +03:00
static const STATE_ICONS_COLORS = {
"on": Colors.amber,
"off": Color.fromRGBO(68, 115, 158, 1.0),
"unavailable": Colors.black12,
"unknown": Colors.black12,
"playing": Colors.amber
};
2018-09-30 01:07:02 +03:00
static const RIGHT_WIDGET_PADDING = 14.0;
2018-09-29 16:19:01 +03:00
static const LEFT_WIDGET_PADDING = 8.0;
static const EXTENDED_WIDGET_HEIGHT = 50.0;
static const WIDGET_HEIGHT = 34.0;
2018-09-30 01:07:02 +03:00
static const ICON_SIZE = 28.0;
static const STATE_FONT_SIZE = 16.0;
static const NAME_FONT_SIZE = 16.0;
2018-09-30 10:15:32 +03:00
static const SMALL_FONT_SIZE = 14.0;
2018-09-30 20:57:07 +03:00
static const INPUT_WIDTH = 160.0;
2018-09-29 16:19:01 +03:00
Map _attributes;
String _domain;
String _entityId;
String _state;
2018-09-29 16:19:01 +03:00
DateTime _lastUpdated;
2018-09-26 22:16:50 +03:00
2018-09-30 20:24:13 +03:00
String get displayName =>
_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;
2018-09-30 20:24:13 +03:00
bool get isView =>
(_domain == "group") &&
(_attributes != null ? _attributes["view"] ?? false : false);
bool get isGroup => _domain == "group";
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 childEntities => _attributes["entity_id"] ?? [];
2018-09-29 16:19:01 +03:00
String get lastUpdated => _getLastUpdatedFormatted();
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"];
2018-09-29 16:19:01 +03:00
_lastUpdated = DateTime.tryParse(rawData["last_updated"]);
}
String _getLastUpdatedFormatted() {
if (_lastUpdated == null) {
return "-";
} else {
2018-09-30 10:15:32 +03:00
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";
2018-09-29 16:19:01 +03:00
}
}
void openEntityPage() {
eventBus.fire(new ShowEntityPageEvent(this));
2018-09-26 22:16:50 +03:00
}
2018-09-30 12:51:55 +03:00
void sendNewState(newState) {
return;
}
2018-09-30 23:12:27 +03:00
Widget buildWidget(bool inCard, BuildContext context) {
return SizedBox(
2018-09-29 16:19:01 +03:00
height: Entity.WIDGET_HEIGHT,
child: Row(
children: <Widget>[
2018-09-29 16:19:01 +03:00
GestureDetector(
child: _buildIconWidget(),
2018-09-30 10:00:19 +03:00
onTap: inCard ? openEntityPage : null,
),
Expanded(
2018-09-29 16:19:01 +03:00
child: GestureDetector(
child: _buildNameWidget(),
2018-09-30 10:00:19 +03:00
onTap: inCard ? openEntityPage : null,
),
),
2018-09-30 23:12:27 +03:00
_buildActionWidget(inCard, context)
],
),
);
}
2018-09-30 10:15:32 +03:00
Widget buildAdditionalWidget() {
return _buildLastUpdatedWidget();
}
2018-09-29 16:19:01 +03:00
Widget _buildIconWidget() {
return Padding(
padding: EdgeInsets.fromLTRB(Entity.LEFT_WIDGET_PADDING, 0.0, 12.0, 0.0),
2018-09-30 20:24:13 +03:00
child: MaterialDesignIcons.createIconWidgetFromEntityData(
this,
Entity.ICON_SIZE,
Entity.STATE_ICONS_COLORS[_state] ?? Colors.blueGrey),
2018-09-29 16:19:01 +03:00
);
}
Widget _buildLastUpdatedWidget() {
2018-09-30 10:15:32 +03:00
return Padding(
2018-09-30 20:24:13 +03:00
padding: EdgeInsets.fromLTRB(
Entity.LEFT_WIDGET_PADDING, Entity.SMALL_FONT_SIZE, 0.0, 0.0),
2018-09-30 10:15:32 +03:00
child: Text(
'${this.lastUpdated}',
textAlign: TextAlign.left,
2018-09-30 20:24:13 +03:00
style:
TextStyle(fontSize: Entity.SMALL_FONT_SIZE, color: Colors.black26),
2018-09-29 16:19:01 +03:00
),
);
}
Widget _buildNameWidget() {
2018-09-29 18:02:29 +03:00
return Padding(
padding: EdgeInsets.only(right: 10.0),
child: Text(
"${this.displayName}",
overflow: TextOverflow.ellipsis,
softWrap: false,
2018-09-30 20:24:13 +03:00
style: TextStyle(fontSize: Entity.NAME_FONT_SIZE),
2018-09-29 16:19:01 +03:00
),
);
}
2018-09-30 23:12:27 +03:00
Widget _buildActionWidget(bool inCard, BuildContext context) {
return Padding(
2018-09-30 20:24:13 +03:00
padding:
EdgeInsets.fromLTRB(0.0, 0.0, Entity.RIGHT_WIDGET_PADDING, 0.0),
2018-09-29 16:19:01 +03:00
child: GestureDetector(
child: Text(
"$_state${this.unitOfMeasurement}",
textAlign: TextAlign.right,
style: new TextStyle(
2018-09-30 01:07:02 +03:00
fontSize: Entity.STATE_FONT_SIZE,
2018-09-30 20:24:13 +03:00
)),
2018-09-29 16:19:01 +03:00
onTap: openEntityPage,
2018-09-30 23:12:27 +03:00
)
);
}
}
class SwitchEntity extends Entity {
SwitchEntity(Map rawData) : super(rawData);
2018-09-30 12:51:55 +03:00
@override
void sendNewState(newValue) {
2018-09-30 20:24:13 +03:00
eventBus.fire(new ServiceCallEvent(
_domain, (newValue as bool) ? "turn_on" : "turn_off", entityId, null));
2018-09-30 12:51:55 +03:00
}
@override
2018-09-30 23:12:27 +03:00
Widget _buildActionWidget(bool inCard, BuildContext context) {
return Switch(
value: this.isOn,
onChanged: ((switchState) {
2018-09-30 12:51:55 +03:00
sendNewState(switchState);
}),
);
}
}
class ButtonEntity extends Entity {
ButtonEntity(Map rawData) : super(rawData);
2018-09-30 12:51:55 +03:00
@override
void sendNewState(newValue) {
eventBus.fire(new ServiceCallEvent(_domain, "turn_on", _entityId, null));
}
@override
2018-09-30 23:12:27 +03:00
Widget _buildActionWidget(bool inCard, BuildContext context) {
return FlatButton(
onPressed: (() {
2018-09-30 12:51:55 +03:00
sendNewState(null);
}),
child: Text(
"EXECUTE",
textAlign: TextAlign.right,
2018-09-30 20:24:13 +03:00
style:
new TextStyle(fontSize: Entity.STATE_FONT_SIZE, color: Colors.blue),
),
);
}
2018-09-30 20:24:13 +03:00
}
2018-09-30 23:12:27 +03:00
//
// SLIDER
//
2018-09-30 20:24:13 +03:00
class SliderEntity extends Entity {
int _multiplier = 1;
double get minValue => _attributes["min"] ?? 0.0;
double get maxValue => _attributes["max"] ?? 100.0;
double get valueStep => _attributes["step"] ?? 1.0;
double get doubleState => double.tryParse(_state) ?? 0.0;
SliderEntity(Map rawData) : super(rawData) {
if (valueStep < 1) {
_multiplier = 10;
} else if (valueStep < 0.1) {
_multiplier = 100;
}
}
2018-09-30 20:24:13 +03:00
@override
void sendNewState(newValue) {
eventBus.fire(new ServiceCallEvent(_domain, "set_value", _entityId,
{"value": "${newValue.toString()}"}));
}
@override
2018-09-30 23:12:27 +03:00
Widget _buildActionWidget(bool inCard, BuildContext context) {
2018-09-30 20:24:13 +03:00
return Container(
width: 200.0,
child: Row(
children: <Widget>[
Expanded(
child: Slider(
min: this.minValue * _multiplier,
max: this.maxValue * _multiplier,
value: (this.doubleState <= this.maxValue) &&
(this.doubleState >= this.minValue)
? this.doubleState * _multiplier
: this.minValue * _multiplier,
onChanged: (value) {
eventBus.fire(new StateChangedEvent(_entityId,
(value.roundToDouble() / _multiplier).toString(), true));
},
onChangeEnd: (value) {
sendNewState(value.roundToDouble() / _multiplier);
},
),
),
Padding(
padding: EdgeInsets.only(right: Entity.RIGHT_WIDGET_PADDING),
child: Text("$_state${this.unitOfMeasurement}",
textAlign: TextAlign.right,
style: new TextStyle(
fontSize: Entity.STATE_FONT_SIZE,
)),
)
],
),
);
}
}
2018-09-30 23:12:27 +03:00
//
// DATETIME
//
class DateTimeEntity extends Entity {
bool get hasDate => _attributes["has_date"] ?? false;
bool get hasTime => _attributes["has_time"] ?? false;
int get year => _attributes["year"] ?? 1970;
int get month => _attributes["month"] ?? 1;
int get day => _attributes["day"] ?? 1;
int get hour => _attributes["hour"] ?? 0;
int get minute => _attributes["minute"] ?? 0;
int get second => _attributes["second"] ?? 0;
String get formattedState => _getFormattedState();
DateTime get dateTimeState => _getDateTimeState();
DateTimeEntity(Map rawData) : super(rawData);
DateTime _getDateTimeState() {
return DateTime(this.year, this.month, this.day, this.hour, this.minute, this.second);
}
String _getFormattedState() {
String formattedState = "";
if (this.hasDate) {
formattedState += formatDate(dateTimeState, [M, ' ', d, ', ', yyyy]);
}
if (this.hasTime) {
formattedState += " "+formatDate(dateTimeState, [HH, ':', nn]);
}
return formattedState;
}
@override
void sendNewState(newValue) {
eventBus.fire(new ServiceCallEvent(_domain, "set_datetime", _entityId,
newValue));
}
@override
Widget _buildActionWidget(bool inCard, BuildContext context) {
return Padding(
padding:
EdgeInsets.fromLTRB(0.0, 0.0, Entity.RIGHT_WIDGET_PADDING, 0.0),
child: GestureDetector(
child: Text(
"$formattedState",
textAlign: TextAlign.right,
style: new TextStyle(
fontSize: Entity.STATE_FONT_SIZE,
)),
onTap: () => _handleStateTap(context),
)
);
}
void _handleStateTap(BuildContext context) {
if (hasDate) {
_showDatePicker(context).then((date) {
if (date != null) {
if (hasTime) {
_showTimePicker(context).then((time){
sendNewState({"date": "${formatDate(date, [yyyy, '-', mm, '-', dd])}", "time": "${formatDate(DateTime(1970, 1, 1, time.hour, time.minute), [HH, ':', nn])}"});
});
} else {
sendNewState({"date": "${formatDate(date, [yyyy, '-', mm, '-', dd])}"});
}
}
});
} else if (hasTime) {
_showTimePicker(context).then((time){
if (time != null) {
sendNewState({"time": "${formatDate(DateTime(1970, 1, 1, time.hour, time.minute), [HH, ':', nn])}"});
}
});
} else {
TheLogger.log("Warning", "$entityId has no date and no time");
}
}
Future _showDatePicker(BuildContext context) {
return showDatePicker(
context: context,
initialDate: dateTimeState,
firstDate: DateTime(1970),
lastDate: DateTime(2037) //Unix timestamp will finish at Jan 19, 2038
);
}
Future _showTimePicker(BuildContext context) {
return showTimePicker(
context: context,
initialTime: TimeOfDay.fromDateTime(dateTimeState)
);
}
}
2018-09-30 20:57:07 +03:00
class SelectEntity extends Entity {
List<String> _listOptions = [];
String get initialValue => _attributes["initial"] ?? null;
SelectEntity(Map rawData) : super(rawData) {
if (_attributes["options"] != null) {
_attributes["options"].forEach((value){
_listOptions.add(value.toString());
});
}
}
@override
void sendNewState(newValue) {
eventBus.fire(new ServiceCallEvent(_domain, "select_option", _entityId,
{"option": "$newValue"}));
}
@override
2018-09-30 23:12:27 +03:00
Widget _buildActionWidget(bool inCard, BuildContext context) {
2018-09-30 20:57:07 +03:00
return Container(
width: Entity.INPUT_WIDTH,
child: DropdownButton<String>(
value: _state,
items: this._listOptions.map((String value) {
return new DropdownMenuItem<String>(
value: value,
child: new Text(value),
);
}).toList(),
onChanged: (_) {
sendNewState(_);
},
),
);
}
}
class TextEntity extends Entity {
2018-09-30 10:00:19 +03:00
String tmpState;
FocusNode _focusNode;
2018-09-30 12:51:55 +03:00
bool validValue = false;
2018-09-30 10:00:19 +03:00
2018-09-30 20:24:13 +03:00
int get valueMinLength => _attributes["min"] ?? -1;
int get valueMaxLength => _attributes["max"] ?? -1;
String get valuePattern => _attributes["pattern"] ?? null;
bool get isTextField => _attributes["mode"] == "text";
bool get isPasswordField => _attributes["mode"] == "password";
2018-09-30 20:57:07 +03:00
TextEntity(Map rawData) : super(rawData) {
2018-09-30 10:00:19 +03:00
_focusNode = FocusNode();
2018-09-30 12:51:55 +03:00
//TODO possible memory leak generator
2018-09-30 10:00:19 +03:00
_focusNode.addListener(_focusListener);
2018-09-30 12:51:55 +03:00
//tmpState = state;
}
@override
void sendNewState(newValue) {
if (validate(newValue)) {
eventBus.fire(new ServiceCallEvent(_domain, "set_value", _entityId,
2018-09-30 20:57:07 +03:00
{"value": "{newValue"}));
2018-09-30 12:51:55 +03:00
}
}
@override
void update(Map rawData) {
super.update(rawData);
tmpState = _state;
}
bool validate(newValue) {
if (newValue is String) {
//TODO add pattern support
2018-09-30 20:24:13 +03:00
validValue = (newValue.length >= this.valueMinLength) &&
(this.valueMaxLength == -1 ||
(newValue.length <= this.valueMaxLength));
2018-09-30 12:51:55 +03:00
} else {
validValue = true;
}
return validValue;
2018-09-30 10:00:19 +03:00
}
2018-09-30 10:00:19 +03:00
void _focusListener() {
if (!_focusNode.hasFocus && (tmpState != state)) {
2018-09-30 12:51:55 +03:00
sendNewState(tmpState);
tmpState = state;
2018-09-30 10:00:19 +03:00
}
}
2018-09-29 16:19:01 +03:00
@override
2018-09-30 23:12:27 +03:00
Widget _buildActionWidget(bool inCard, BuildContext context) {
2018-09-30 20:24:13 +03:00
if (this.isTextField || this.isPasswordField) {
2018-09-30 01:07:02 +03:00
return Container(
2018-09-30 20:57:07 +03:00
width: Entity.INPUT_WIDTH,
2018-09-30 01:07:02 +03:00
child: TextField(
2018-09-30 20:24:13 +03:00
focusNode: inCard ? _focusNode : null,
obscureText: this.isPasswordField,
controller: new TextEditingController.fromValue(
new TextEditingValue(
text: tmpState,
selection:
new TextSelection.collapsed(offset: tmpState.length))),
onChanged: (value) {
tmpState = value;
}),
2018-09-30 01:07:02 +03:00
);
} else {
2018-09-30 20:24:13 +03:00
TheLogger.log("Warning", "Unsupported input mode for $entityId");
2018-09-30 23:12:27 +03:00
return super._buildActionWidget(inCard, context);
}
}
2018-09-30 20:24:13 +03:00
}