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

View File

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