Resolves #176 History can be requested only once per 30 seconds

This commit is contained in:
Yegor Vialov 2018-11-14 13:38:02 +02:00
parent f5434e26e5
commit 3190b45db3
2 changed files with 19 additions and 15 deletions

View File

@ -56,10 +56,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"];
if (domain == "sun") {
state = "iuwfhiwushf iwuwfhiuwefh dsjhfkjsdfnksdj nfksdjfn ksdjfn kdsjfndskj sdk fhksbsk jvfk jvsfkj sfkjvsfkvdsjk bvsfk svfjk";
attributes["friendly_name"] = "Black hole sun, wan't you come, wan't you come";
}
assumedState = state; assumedState = state;
_lastUpdated = DateTime.tryParse(rawData["last_updated"]); _lastUpdated = DateTime.tryParse(rawData["last_updated"]);
} }

View File

@ -31,6 +31,7 @@ class _EntityHistoryWidgetState extends State<EntityHistoryWidget> {
List _history; List _history;
bool _needToUpdateHistory; bool _needToUpdateHistory;
DateTime _historyLastUpdated;
@override @override
void initState() { void initState() {
@ -39,18 +40,25 @@ class _EntityHistoryWidgetState extends State<EntityHistoryWidget> {
} }
void _loadHistory(HomeAssistant ha, String entityId) { void _loadHistory(HomeAssistant ha, String entityId) {
ha.getHistory(entityId).then((history){ DateTime now = DateTime.now();
setState(() { if (_historyLastUpdated != null) {
_history = history.isNotEmpty ? history[0] : []; TheLogger.debug("History was updated ${now.difference(_historyLastUpdated).inSeconds} seconds ago");
_needToUpdateHistory = false; }
if (_historyLastUpdated == null || now.difference(_historyLastUpdated).inSeconds > 30) {
_historyLastUpdated = now;
ha.getHistory(entityId).then((history){
setState(() {
_history = history.isNotEmpty ? history[0] : [];
_needToUpdateHistory = false;
});
}).catchError((e) {
TheLogger.error("Error loading $entityId history: $e");
setState(() {
_history = [];
_needToUpdateHistory = false;
});
}); });
}).catchError((e) { }
TheLogger.error("Error loading $entityId history: $e");
setState(() {
_history = [];
_needToUpdateHistory = false;
});
});
} }
@override @override