2018-10-27 14:27:41 +03:00
|
|
|
part of '../../main.dart';
|
|
|
|
|
|
|
|
class SwitchStateWidget extends StatefulWidget {
|
2018-11-24 11:33:59 +02:00
|
|
|
|
|
|
|
final String domainForService;
|
|
|
|
|
|
|
|
const SwitchStateWidget({Key key, this.domainForService}) : super(key: key);
|
|
|
|
|
2018-10-27 14:27:41 +03:00
|
|
|
@override
|
|
|
|
_SwitchStateWidgetState createState() => _SwitchStateWidgetState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _SwitchStateWidgetState extends State<SwitchStateWidget> {
|
|
|
|
|
2018-11-15 12:49:46 +02:00
|
|
|
String newState;
|
|
|
|
bool updatedHere = false;
|
|
|
|
|
2018-10-27 14:27:41 +03:00
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
super.initState();
|
|
|
|
}
|
|
|
|
|
|
|
|
void _setNewState(newValue, Entity entity) {
|
|
|
|
setState(() {
|
2018-11-15 12:49:46 +02:00
|
|
|
newState = newValue ? EntityState.on : EntityState.off;
|
|
|
|
updatedHere = true;
|
2018-10-27 14:27:41 +03:00
|
|
|
});
|
|
|
|
Timer(Duration(seconds: 2), (){
|
|
|
|
setState(() {
|
2018-11-15 12:49:46 +02:00
|
|
|
newState = entity.state;
|
|
|
|
updatedHere = true;
|
2018-11-18 13:19:00 +02:00
|
|
|
//TheLogger.debug("Timer@!!");
|
2018-10-27 14:27:41 +03:00
|
|
|
});
|
|
|
|
});
|
2018-11-24 11:33:59 +02:00
|
|
|
String domain;
|
|
|
|
if (widget.domainForService != null) {
|
|
|
|
domain = widget.domainForService;
|
|
|
|
} else {
|
|
|
|
domain = entity.domain;
|
|
|
|
}
|
2018-10-27 14:27:41 +03:00
|
|
|
eventBus.fire(new ServiceCallEvent(
|
2018-11-24 11:33:59 +02:00
|
|
|
domain, (newValue as bool) ? "turn_on" : "turn_off", entity.entityId, null));
|
2018-10-27 14:27:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
final entityModel = EntityModel.of(context);
|
2018-11-16 22:32:43 +02:00
|
|
|
final entity = entityModel.entityWrapper.entity;
|
2018-11-15 12:49:46 +02:00
|
|
|
if (!updatedHere) {
|
|
|
|
newState = entity.state;
|
|
|
|
} else {
|
|
|
|
updatedHere = false;
|
|
|
|
}
|
2018-11-14 15:14:46 +02:00
|
|
|
if (entity.state == EntityState.unavailable || entity.state == EntityState.unknown) {
|
2018-11-04 23:13:25 +02:00
|
|
|
return SimpleEntityState();
|
|
|
|
} else if ((entity.attributes["assumed_state"] == null) || (entity.attributes["assumed_state"] == false)) {
|
|
|
|
return SizedBox(
|
|
|
|
height: 32.0,
|
|
|
|
child: Switch(
|
2018-11-15 12:49:46 +02:00
|
|
|
value: newState == EntityState.on,
|
2018-11-04 23:13:25 +02:00
|
|
|
onChanged: ((switchState) {
|
|
|
|
_setNewState(switchState, entity);
|
|
|
|
}),
|
|
|
|
)
|
2018-10-27 14:27:41 +03:00
|
|
|
);
|
|
|
|
} else {
|
2018-11-04 23:13:25 +02:00
|
|
|
return SizedBox(
|
|
|
|
height: 32.0,
|
|
|
|
child: Row(
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
|
|
children: <Widget>[
|
|
|
|
IconButton(
|
|
|
|
onPressed: () => _setNewState(false, entity),
|
|
|
|
icon: Icon(MaterialDesignIcons.createIconDataFromIconName("mdi:flash-off")),
|
2018-11-15 12:49:46 +02:00
|
|
|
color: newState == EntityState.on ? Colors.black : Colors.blue,
|
2018-11-12 20:28:10 +02:00
|
|
|
iconSize: Sizes.iconSize,
|
2018-11-04 23:13:25 +02:00
|
|
|
),
|
|
|
|
IconButton(
|
|
|
|
onPressed: () => _setNewState(true, entity),
|
|
|
|
icon: Icon(MaterialDesignIcons.createIconDataFromIconName("mdi:flash")),
|
2018-11-15 12:49:46 +02:00
|
|
|
color: newState == EntityState.on ? Colors.blue : Colors.black,
|
2018-11-12 20:28:10 +02:00
|
|
|
iconSize: Sizes.iconSize
|
2018-11-04 23:13:25 +02:00
|
|
|
)
|
|
|
|
],
|
|
|
|
),
|
2018-10-27 14:27:41 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|