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];
entityId = rawData["entity_id"];
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;
_lastUpdated = DateTime.tryParse(rawData["last_updated"]);
}

View File

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