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/entities/climate/widgets/climate_controls.dart

411 lines
13 KiB
Dart
Raw Normal View History

2019-08-24 21:22:32 +03:00
part of '../../../main.dart';
2018-10-27 14:27:41 +03:00
class ClimateControlWidget extends StatefulWidget {
ClimateControlWidget({Key key}) : super(key: key);
@override
_ClimateControlWidgetState createState() => _ClimateControlWidgetState();
}
class _ClimateControlWidgetState extends State<ClimateControlWidget> {
bool _temperaturePending = false;
2018-10-27 14:27:41 +03:00
bool _changedHere = false;
Timer _tempThrottleTimer;
Timer _targetTempThrottleTimer;
2018-10-27 14:27:41 +03:00
double _tmpTemperature = 0.0;
double _tmpTargetLow = 0.0;
double _tmpTargetHigh = 0.0;
double _tmpTargetHumidity = 0.0;
2019-08-16 13:29:41 +03:00
String _tmpHVACMode;
2018-10-27 14:27:41 +03:00
String _tmpFanMode;
String _tmpSwingMode;
2019-08-16 13:29:41 +03:00
String _tmpPresetMode;
//bool _tmpIsOff = false;
2018-10-27 14:27:41 +03:00
bool _tmpAuxHeat = false;
void _resetVars(ClimateEntity entity) {
if (!_temperaturePending) {
_tmpTemperature = entity.temperature;
_tmpTargetHigh = entity.targetHigh;
_tmpTargetLow = entity.targetLow;
}
2019-08-16 13:29:41 +03:00
_tmpHVACMode = entity.state;
2018-10-27 14:27:41 +03:00
_tmpFanMode = entity.fanMode;
_tmpSwingMode = entity.swingMode;
2019-08-16 13:29:41 +03:00
_tmpPresetMode = entity.presetMode;
//_tmpIsOff = entity.isOff;
2018-10-27 14:27:41 +03:00
_tmpAuxHeat = entity.auxHeat;
_tmpTargetHumidity = entity.targetHumidity;
_changedHere = false;
}
void _temperatureUp(ClimateEntity entity) {
_tmpTemperature = ((_tmpTemperature + entity.temperatureStep) <= entity.maxTemp) ? _tmpTemperature + entity.temperatureStep : entity.maxTemp;
2018-10-27 14:27:41 +03:00
_setTemperature(entity);
}
void _temperatureDown(ClimateEntity entity) {
_tmpTemperature = ((_tmpTemperature - entity.temperatureStep) >= entity.minTemp) ? _tmpTemperature - entity.temperatureStep : entity.minTemp;
2018-10-27 14:27:41 +03:00
_setTemperature(entity);
}
void _targetLowUp(ClimateEntity entity) {
_tmpTargetLow = ((_tmpTargetLow + entity.temperatureStep) <= entity.maxTemp) ? _tmpTargetLow + entity.temperatureStep : entity.maxTemp;
2018-10-27 14:27:41 +03:00
_setTargetTemp(entity);
}
void _targetLowDown(ClimateEntity entity) {
_tmpTargetLow = ((_tmpTargetLow - entity.temperatureStep) >= entity.minTemp) ? _tmpTargetLow - entity.temperatureStep : entity.minTemp;
2018-10-27 14:27:41 +03:00
_setTargetTemp(entity);
}
void _targetHighUp(ClimateEntity entity) {
_tmpTargetHigh = ((_tmpTargetHigh + entity.temperatureStep) <= entity.maxTemp) ? _tmpTargetHigh + entity.temperatureStep : entity.maxTemp;
2018-10-27 14:27:41 +03:00
_setTargetTemp(entity);
}
void _targetHighDown(ClimateEntity entity) {
_tmpTargetHigh = ((_tmpTargetHigh - entity.temperatureStep) >= entity.minTemp) ? _tmpTargetHigh - entity.temperatureStep : entity.minTemp;
2018-10-27 14:27:41 +03:00
_setTargetTemp(entity);
}
void _setTemperature(ClimateEntity entity) {
_tempThrottleTimer?.cancel();
2018-10-27 14:27:41 +03:00
setState(() {
_changedHere = true;
_temperaturePending = true;
_tmpTemperature = double.parse(_tmpTemperature.toStringAsFixed(1));
});
_tempThrottleTimer = Timer(Duration(seconds: 2), () {
setState(() {
_changedHere = true;
_temperaturePending = false;
2019-10-28 19:59:47 +02:00
ConnectionManager().callService(
2019-11-08 21:37:41 +02:00
domain: entity.domain,
service: "set_temperature",
entityId: entity.entityId,
data: {"temperature": "${_tmpTemperature.toStringAsFixed(1)}"}
2019-10-28 19:59:47 +02:00
);
});
2018-10-27 14:27:41 +03:00
});
}
void _setTargetTemp(ClimateEntity entity) {
_targetTempThrottleTimer?.cancel();
2018-10-27 14:27:41 +03:00
setState(() {
_changedHere = true;
_temperaturePending = true;
2018-10-27 14:27:41 +03:00
_tmpTargetLow = double.parse(_tmpTargetLow.toStringAsFixed(1));
_tmpTargetHigh = double.parse(_tmpTargetHigh.toStringAsFixed(1));
});
_targetTempThrottleTimer = Timer(Duration(seconds: 2), () {
setState(() {
_changedHere = true;
_temperaturePending = false;
2019-10-28 19:59:47 +02:00
ConnectionManager().callService(
2019-11-08 21:37:41 +02:00
domain: entity.domain,
service: "set_temperature",
entityId: entity.entityId,
data: {"target_temp_high": "${_tmpTargetHigh.toStringAsFixed(1)}", "target_temp_low": "${_tmpTargetLow.toStringAsFixed(1)}"}
2019-10-28 19:59:47 +02:00
);
});
2018-10-27 14:27:41 +03:00
});
}
void _setTargetHumidity(ClimateEntity entity, double value) {
setState(() {
_tmpTargetHumidity = value.roundToDouble();
_changedHere = true;
2019-11-08 21:37:41 +02:00
ConnectionManager().callService(
domain: entity.domain,
service: "set_humidity",
entityId: entity.entityId,
data: {"humidity": "$_tmpTargetHumidity"}
);
2018-10-27 14:27:41 +03:00
});
}
2019-08-16 13:29:41 +03:00
void _setHVACMode(ClimateEntity entity, value) {
2018-10-27 14:27:41 +03:00
setState(() {
2019-08-16 13:29:41 +03:00
_tmpHVACMode = value;
2018-10-27 14:27:41 +03:00
_changedHere = true;
2019-11-08 21:37:41 +02:00
ConnectionManager().callService(
domain: entity.domain,
service: "set_hvac_mode",
entityId: entity.entityId,
data: {"hvac_mode": "$_tmpHVACMode"}
);
2018-10-27 14:27:41 +03:00
});
}
void _setSwingMode(ClimateEntity entity, value) {
setState(() {
_tmpSwingMode = value;
_changedHere = true;
2019-11-08 21:37:41 +02:00
ConnectionManager().callService(
domain: entity.domain,
service: "set_swing_mode",
entityId: entity.entityId,
data: {"swing_mode": "$_tmpSwingMode"}
);
2018-10-27 14:27:41 +03:00
});
}
void _setFanMode(ClimateEntity entity, value) {
setState(() {
_tmpFanMode = value;
_changedHere = true;
2019-11-08 21:37:41 +02:00
ConnectionManager().callService(domain: entity.domain, service: "set_fan_mode", entityId: entity.entityId, data: {"fan_mode": "$_tmpFanMode"});
2018-10-27 14:27:41 +03:00
});
}
2019-08-16 13:29:41 +03:00
void _setPresetMode(ClimateEntity entity, value) {
2018-10-27 14:27:41 +03:00
setState(() {
2019-08-16 13:29:41 +03:00
_tmpPresetMode = value;
2018-10-27 14:27:41 +03:00
_changedHere = true;
2019-11-08 21:37:41 +02:00
ConnectionManager().callService(domain: entity.domain, service: "set_preset_mode", entityId: entity.entityId, data: {"preset_mode": "$_tmpPresetMode"});
2018-10-27 14:27:41 +03:00
});
}
2019-08-16 13:29:41 +03:00
/*void _setOnOf(ClimateEntity entity, value) {
2018-10-27 14:27:41 +03:00
setState(() {
_tmpIsOff = !value;
_changedHere = true;
eventBus.fire(new ServiceCallEvent(entity.domain, "${_tmpIsOff ? 'turn_off' : 'turn_on'}", entity.entityId, null));
_resetStateTimer(entity);
});
2019-08-16 13:29:41 +03:00
}*/
2018-10-27 14:27:41 +03:00
void _setAuxHeat(ClimateEntity entity, value) {
setState(() {
_tmpAuxHeat = value;
_changedHere = true;
2019-11-08 21:37:41 +02:00
ConnectionManager().callService(domain: entity.domain, service: "set_aux_heat", entityId: entity.entityId, data: {"aux_heat": "$_tmpAuxHeat"});
2018-10-27 14:27:41 +03:00
});
}
@override
Widget build(BuildContext context) {
final entityModel = EntityModel.of(context);
final ClimateEntity entity = entityModel.entityWrapper.entity;
Logger.d("[Climate widget build] changed here = $_changedHere");
2018-10-27 14:27:41 +03:00
if (_changedHere) {
//_showPending = (_tmpTemperature != entity.temperature || _tmpTargetHigh != entity.targetHigh || _tmpTargetLow != entity.targetLow);
2018-10-27 14:27:41 +03:00
_changedHere = false;
} else {
_resetVars(entity);
}
return Padding(
2018-11-12 20:28:10 +02:00
padding: EdgeInsets.fromLTRB(Sizes.leftWidgetPadding, Sizes.rowPadding, Sizes.rightWidgetPadding, 0.0),
2018-10-27 14:27:41 +03:00
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
2019-08-16 13:29:41 +03:00
//_buildOnOffControl(entity),
_buildTemperatureControls(entity, context),
_buildTargetTemperatureControls(entity, context),
_buildHumidityControls(entity, context),
_buildOperationControl(entity, context),
_buildFanControl(entity, context),
_buildSwingControl(entity, context),
_buildPresetModeControl(entity, context),
_buildAuxHeatControl(entity, context)
2018-10-27 14:27:41 +03:00
],
),
);
}
Widget _buildPresetModeControl(ClimateEntity entity, BuildContext context) {
2019-08-16 13:29:41 +03:00
if (entity.supportPresetMode) {
return ModeSelectorWidget(
options: entity.presetModes,
onChange: (mode) => _setPresetMode(entity, mode),
caption: "Preset",
value: _tmpPresetMode,
2018-10-27 14:27:41 +03:00
);
} else {
return Container(height: 0.0, width: 0.0,);
}
}
2019-08-16 13:29:41 +03:00
/*Widget _buildOnOffControl(ClimateEntity entity) {
2018-10-27 14:27:41 +03:00
if (entity.supportOnOff) {
return ModeSwitchWidget(
onChange: (value) => _setOnOf(entity, value),
caption: "On / Off",
value: !_tmpIsOff
);
} else {
return Container(height: 0.0, width: 0.0,);
}
2019-08-16 13:29:41 +03:00
}*/
2018-10-27 14:27:41 +03:00
Widget _buildAuxHeatControl(ClimateEntity entity, BuildContext context) {
2018-10-27 14:27:41 +03:00
if (entity.supportAuxHeat ) {
return ModeSwitchWidget(
caption: "Aux heat",
onChange: (value) => _setAuxHeat(entity, value),
value: _tmpAuxHeat
);
} else {
return Container(height: 0.0, width: 0.0,);
}
}
Widget _buildOperationControl(ClimateEntity entity, BuildContext context) {
2019-08-16 13:29:41 +03:00
if (entity.hvacModes != null) {
2018-10-27 14:27:41 +03:00
return ModeSelectorWidget(
2019-08-16 13:29:41 +03:00
onChange: (mode) => _setHVACMode(entity, mode),
options: entity.hvacModes,
2018-10-27 14:27:41 +03:00
caption: "Operation",
2019-08-16 13:29:41 +03:00
value: _tmpHVACMode,
2018-10-27 14:27:41 +03:00
);
} else {
return Container(height: 0.0, width: 0.0);
}
}
Widget _buildFanControl(ClimateEntity entity, BuildContext context) {
2018-10-27 14:27:41 +03:00
if (entity.supportFanMode) {
return ModeSelectorWidget(
2019-08-16 13:29:41 +03:00
options: entity.fanModes,
2018-10-27 14:27:41 +03:00
onChange: (mode) => _setFanMode(entity, mode),
caption: "Fan mode",
value: _tmpFanMode,
);
} else {
return Container(height: 0.0, width: 0.0);
}
}
Widget _buildSwingControl(ClimateEntity entity, BuildContext context) {
2018-10-27 14:27:41 +03:00
if (entity.supportSwingMode) {
return ModeSelectorWidget(
onChange: (mode) => _setSwingMode(entity, mode),
2019-08-16 13:29:41 +03:00
options: entity.swingModes,
2018-10-27 14:27:41 +03:00
value: _tmpSwingMode,
caption: "Swing mode"
);
} else {
return Container(height: 0.0, width: 0.0);
}
}
Widget _buildTemperatureControls(ClimateEntity entity, BuildContext context) {
2018-10-27 14:27:41 +03:00
if ((entity.supportTargetTemperature) && (entity.temperature != null)) {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text("Target temperature", style: Theme.of(context).textTheme.body1),
2018-10-27 14:27:41 +03:00
TemperatureControlWidget(
value: _tmpTemperature,
active: _temperaturePending,
onDec: () => _temperatureDown(entity),
onInc: () => _temperatureUp(entity),
2018-10-27 14:27:41 +03:00
)
],
);
} else {
return Container(width: 0.0, height: 0.0,);
}
}
Widget _buildTargetTemperatureControls(ClimateEntity entity, BuildContext context) {
2018-10-27 14:27:41 +03:00
List<Widget> controls = [];
2019-08-16 13:29:41 +03:00
if ((entity.supportTargetTemperatureRange) && (entity.targetLow != null)) {
2018-10-27 14:27:41 +03:00
controls.addAll(<Widget>[
TemperatureControlWidget(
value: _tmpTargetLow,
active: _temperaturePending,
onDec: () => _targetLowDown(entity),
onInc: () => _targetLowUp(entity),
2018-10-27 14:27:41 +03:00
),
Expanded(
child: Container(height: 10.0),
)
]);
}
2019-08-16 13:29:41 +03:00
if ((entity.supportTargetTemperatureRange) && (entity.targetHigh != null)) {
2018-10-27 14:27:41 +03:00
controls.add(
TemperatureControlWidget(
value: _tmpTargetHigh,
active: _temperaturePending,
onDec: () => _targetHighDown(entity),
onInc: () => _targetHighUp(entity),
2018-10-27 14:27:41 +03:00
)
);
}
if (controls.isNotEmpty) {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text("Target temperature range", style: Theme.of(context).textTheme.body1),
2018-10-27 14:27:41 +03:00
Row(
children: controls,
)
],
);
} else {
return Container(width: 0.0, height: 0.0);
}
}
Widget _buildHumidityControls(ClimateEntity entity, BuildContext context) {
2018-10-27 14:27:41 +03:00
List<Widget> result = [];
if (entity.supportTargetHumidity) {
result.addAll(<Widget>[
Text(
"$_tmpTargetHumidity%",
style: Theme.of(context).textTheme.display1,
2018-10-27 14:27:41 +03:00
),
Expanded(
child: Slider(
value: _tmpTargetHumidity,
max: entity.maxHumidity,
min: entity.minHumidity,
onChanged: ((double val) {
setState(() {
_changedHere = true;
_tmpTargetHumidity = val.roundToDouble();
});
}),
onChangeEnd: (double v) => _setTargetHumidity(entity, v),
),
)
]);
}
if (result.isNotEmpty) {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Padding(
padding: EdgeInsets.fromLTRB(
2018-11-12 20:28:10 +02:00
0.0, Sizes.rowPadding, 0.0, Sizes.rowPadding),
child: Text("Target humidity", style: Theme.of(context).textTheme.body1),
2018-10-27 14:27:41 +03:00
),
Row(
crossAxisAlignment: CrossAxisAlignment.center,
children: result,
),
Container(
2018-11-12 20:28:10 +02:00
height: Sizes.rowPadding,
2018-10-27 14:27:41 +03:00
)
],
);
} else {
return Container(
width: 0.0,
height: 0.0,
);
}
}
@override
void dispose() {
super.dispose();
}
}