From 3190b45db301ad5f6ce4dc2c68fdd0dbcb2a90e7 Mon Sep 17 00:00:00 2001 From: Yegor Vialov Date: Wed, 14 Nov 2018 13:38:02 +0200 Subject: [PATCH] Resolves #176 History can be requested only once per 30 seconds --- lib/entity_class/entity.class.dart | 4 --- .../history_chart/entity_history.dart | 30 ++++++++++++------- 2 files changed, 19 insertions(+), 15 deletions(-) diff --git a/lib/entity_class/entity.class.dart b/lib/entity_class/entity.class.dart index 79b4680..845c71e 100644 --- a/lib/entity_class/entity.class.dart +++ b/lib/entity_class/entity.class.dart @@ -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"]); } diff --git a/lib/entity_widgets/history_chart/entity_history.dart b/lib/entity_widgets/history_chart/entity_history.dart index dfafeeb..0a6bfb1 100644 --- a/lib/entity_widgets/history_chart/entity_history.dart +++ b/lib/entity_widgets/history_chart/entity_history.dart @@ -31,6 +31,7 @@ class _EntityHistoryWidgetState extends State { List _history; bool _needToUpdateHistory; + DateTime _historyLastUpdated; @override void initState() { @@ -39,18 +40,25 @@ class _EntityHistoryWidgetState extends State { } 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