New card ui. Input_number mode: slider support

This commit is contained in:
estevez 2018-09-29 11:52:17 +03:00
parent af3a5bc611
commit d0d1ab2740
3 changed files with 104 additions and 20 deletions

View File

@ -11,6 +11,14 @@ class Entity {
String get domain => _domain; String get domain => _domain;
String get entityId => _entityId; String get entityId => _entityId;
String get state => _state; String get state => _state;
set state(value) => _state = value;
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;
bool get isSlider => _attributes["mode"] == "slider";
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); bool get isView => (_domain == "group") && (_attributes != null ? _attributes["view"] ?? false : false);
bool get isGroup => _domain == "group"; bool get isGroup => _domain == "group";
@ -24,6 +32,10 @@ class Entity {
update(rawData); update(rawData);
} }
int getValueDivisions() {
return ((maxValue - minValue)/valueStep).round().round();
}
void update(Map rawData) { void update(Map rawData) {
_attributes = rawData["attributes"] ?? {}; _attributes = rawData["attributes"] ?? {};
_domain = rawData["entity_id"].split(".")[0]; _domain = rawData["entity_id"].split(".")[0];

View File

@ -233,7 +233,7 @@ class HomeAssistant {
_statesCompleter.complete(); _statesCompleter.complete();
} }
Future callService(String domain, String service, String entity_id) { Future callService(String domain, String service, String entityId, Map<String, String> additionalParams) {
var sendCompleter = Completer(); var sendCompleter = Completer();
//TODO: Send service call timeout timer. Should be removed after #21 fix //TODO: Send service call timeout timer. Should be removed after #21 fix
Timer _sendTimer = Timer(Duration(seconds: 7), () { Timer _sendTimer = Timer(Duration(seconds: 7), () {
@ -241,7 +241,14 @@ class HomeAssistant {
}); });
_reConnectSocket().then((r) { _reConnectSocket().then((r) {
_incrementMessageId(); _incrementMessageId();
_sendMessageRaw('{"id": $_currentMessageId, "type": "call_service", "domain": "$domain", "service": "$service", "service_data": {"entity_id": "$entity_id"}}'); String message = '{"id": $_currentMessageId, "type": "call_service", "domain": "$domain", "service": "$service", "service_data": {"entity_id": "$entityId"';
if (additionalParams != null) {
additionalParams.forEach((name, value){
message += ', "$name" : "$value"';
});
}
message += '}}';
_sendMessageRaw(message);
_sendTimer.cancel(); _sendTimer.cancel();
sendCompleter.complete(); sendCompleter.complete();
}).catchError((e){ }).catchError((e){

View File

@ -178,11 +178,11 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver {
}); });
} }
void _callService(String domain, String service, String entityId) { void _callService(String domain, String service, String entityId, Map<String, String> additionalParams) {
setState(() { setState(() {
_isLoading = true; _isLoading = true;
}); });
_homeAssistant.callService(domain, service, entityId).then((r) { _homeAssistant.callService(domain, service, entityId, additionalParams).then((r) {
setState(() { setState(() {
_isLoading = false; _isLoading = false;
}); });
@ -386,7 +386,33 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver {
ids.forEach((id) { ids.forEach((id) {
var data = _entities.get(id); var data = _entities.get(id);
if (data != null) { if (data != null) {
entities.add(new ListTile( entities.add(
Padding(
padding: EdgeInsets.fromLTRB(0.0, 10.0, 0.0, 10.0),
child: SizedBox(
height: 34.0,
child: Row(
children: <Widget>[
Padding(
padding: EdgeInsets.fromLTRB(8.0, 0.0, 12.0, 0.0),
child: MaterialDesignIcons.createIconWidgetFromEntityData(data, 28.0, _stateIconColors[data.state] ?? Colors.blueGrey),
),
Expanded(
child: Text(
"${data.displayName}",
overflow: TextOverflow.fade,
softWrap: false,
style: TextStyle(
fontSize: 16.0
),
),
),
_buildEntityActionWidget(data)
],
),
),
)
/*new ListTile(
leading: MaterialDesignIcons.createIconWidgetFromEntityData(data, 28.0, _stateIconColors[data.state] ?? Colors.blueGrey), leading: MaterialDesignIcons.createIconWidgetFromEntityData(data, 28.0, _stateIconColors[data.state] ?? Colors.blueGrey),
//subtitle: Text("${data['entity_id']}"), //subtitle: Text("${data['entity_id']}"),
trailing: _buildEntityActionWidget(data), trailing: _buildEntityActionWidget(data),
@ -395,7 +421,7 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver {
overflow: TextOverflow.fade, overflow: TextOverflow.fade,
softWrap: false, softWrap: false,
), ),
)); )*/);
} }
}); });
return entities; return entities;
@ -412,7 +438,7 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver {
value: entity.isOn, value: entity.isOn,
onChanged: ((state) { onChanged: ((state) {
_callService( _callService(
entity.domain, state ? "turn_on" : "turn_off", entityId entity.domain, state ? "turn_on" : "turn_off", entityId, null
); );
//TODO remove after checking if state will chenge without setState but after socket event //TODO remove after checking if state will chenge without setState but after socket event
/*setState(() { /*setState(() {
@ -425,25 +451,64 @@ class _MainPageState extends State<MainPage> with WidgetsBindingObserver {
case "script": case "script":
case "scene": { case "scene": {
result = SizedBox( result = FlatButton(
width: 60.0, onPressed: (() {
child: FlatButton( _callService(entity.domain, "turn_on", entityId, null);
onPressed: (() { }),
_callService(entity.domain, "turn_on", entityId); child: Text(
}), "EXECUTE",
child: Text( textAlign: TextAlign.right,
"Run", style: new TextStyle(fontSize: 16.0, color: Colors.blue),
textAlign: TextAlign.right, ),
style: new TextStyle(fontSize: 16.0, color: Colors.blue),
),
)
); );
break; break;
} }
case "input_number": {
if (entity.isSlider) {
result = Container(
width: 200.0,
child: Row(
children: <Widget>[
Expanded(
child: Slider(
min: entity.minValue*10,
max: entity.maxValue*10,
value: entity.doubleState*10,
divisions: entity.getValueDivisions(),
onChanged: (value) {
setState(() {
entity.state = (value.roundToDouble() / 10).toString();
});
},
onChangeEnd: (value) {
_callService(entity.domain, "set_value", entityId,
{"value": "${entity.state}"});
},
),
),
Padding(
padding: EdgeInsets.only(right: 16.0),
child: Text(
"${entity.state}${entity.unitOfMeasurement}",
textAlign: TextAlign.right,
style: new TextStyle(
fontSize: 16.0,
)
),
)
],
),
);
} else {
//TODO draw box instead of slider
}
break;
}
default: { default: {
result = Padding( result = Padding(
padding: EdgeInsets.fromLTRB(0.0, 0.0, 16.0, 0.0), padding: EdgeInsets.fromLTRB(0.0, 0.0, 14.0, 0.0),
child: Text( child: Text(
"${entity.state}${entity.unitOfMeasurement}", "${entity.state}${entity.unitOfMeasurement}",
textAlign: TextAlign.right, textAlign: TextAlign.right,