Remove assumedState from Entity class

This commit is contained in:
Yegor Vialov
2018-11-15 12:49:46 +02:00
parent 62b4e99810
commit b8f6fda8d3
2 changed files with 16 additions and 8 deletions

View File

@ -16,7 +16,6 @@ class Entity {
String domain; String domain;
String entityId; String entityId;
String state; String state;
String assumedState;
DateTime _lastUpdated; DateTime _lastUpdated;
List<Entity> childEntities = []; List<Entity> childEntities = [];
@ -52,7 +51,6 @@ class Entity {
domain = rawData["entity_id"].split(".")[0]; domain = rawData["entity_id"].split(".")[0];
entityId = rawData["entity_id"]; entityId = rawData["entity_id"];
state = rawData["state"]; state = rawData["state"];
assumedState = state;
_lastUpdated = DateTime.tryParse(rawData["last_updated"]); _lastUpdated = DateTime.tryParse(rawData["last_updated"]);
} }

View File

@ -7,6 +7,9 @@ class SwitchStateWidget extends StatefulWidget {
class _SwitchStateWidgetState extends State<SwitchStateWidget> { class _SwitchStateWidgetState extends State<SwitchStateWidget> {
String newState;
bool updatedHere = false;
@override @override
void initState() { void initState() {
super.initState(); super.initState();
@ -14,11 +17,14 @@ class _SwitchStateWidgetState extends State<SwitchStateWidget> {
void _setNewState(newValue, Entity entity) { void _setNewState(newValue, Entity entity) {
setState(() { setState(() {
entity.assumedState = newValue ? EntityState.on : EntityState.off; newState = newValue ? EntityState.on : EntityState.off;
updatedHere = true;
}); });
Timer(Duration(seconds: 2), (){ Timer(Duration(seconds: 2), (){
setState(() { setState(() {
entity.assumedState = entity.state; newState = entity.state;
updatedHere = true;
TheLogger.debug("Timer@!!");
}); });
}); });
eventBus.fire(new ServiceCallEvent( eventBus.fire(new ServiceCallEvent(
@ -29,14 +35,18 @@ class _SwitchStateWidgetState extends State<SwitchStateWidget> {
Widget build(BuildContext context) { Widget build(BuildContext context) {
final entityModel = EntityModel.of(context); final entityModel = EntityModel.of(context);
final entity = entityModel.entity; final entity = entityModel.entity;
Widget result; if (!updatedHere) {
newState = entity.state;
} else {
updatedHere = false;
}
if (entity.state == EntityState.unavailable || entity.state == EntityState.unknown) { if (entity.state == EntityState.unavailable || entity.state == EntityState.unknown) {
return SimpleEntityState(); return SimpleEntityState();
} else if ((entity.attributes["assumed_state"] == null) || (entity.attributes["assumed_state"] == false)) { } else if ((entity.attributes["assumed_state"] == null) || (entity.attributes["assumed_state"] == false)) {
return SizedBox( return SizedBox(
height: 32.0, height: 32.0,
child: Switch( child: Switch(
value: entity.assumedState == EntityState.on, value: newState == EntityState.on,
onChanged: ((switchState) { onChanged: ((switchState) {
_setNewState(switchState, entity); _setNewState(switchState, entity);
}), }),
@ -51,13 +61,13 @@ class _SwitchStateWidgetState extends State<SwitchStateWidget> {
IconButton( IconButton(
onPressed: () => _setNewState(false, entity), onPressed: () => _setNewState(false, entity),
icon: Icon(MaterialDesignIcons.createIconDataFromIconName("mdi:flash-off")), icon: Icon(MaterialDesignIcons.createIconDataFromIconName("mdi:flash-off")),
color: entity.assumedState == EntityState.on ? Colors.black : Colors.blue, color: newState == EntityState.on ? Colors.black : Colors.blue,
iconSize: Sizes.iconSize, iconSize: Sizes.iconSize,
), ),
IconButton( IconButton(
onPressed: () => _setNewState(true, entity), onPressed: () => _setNewState(true, entity),
icon: Icon(MaterialDesignIcons.createIconDataFromIconName("mdi:flash")), icon: Icon(MaterialDesignIcons.createIconDataFromIconName("mdi:flash")),
color: entity.assumedState == EntityState.on ? Colors.blue : Colors.black, color: newState == EntityState.on ? Colors.blue : Colors.black,
iconSize: Sizes.iconSize iconSize: Sizes.iconSize
) )
], ],