Prebake common history queries (#37496)

* Prebake common history queries

The python overhead of to construct
the queries exceeded the database overhead.  We now
prebake the queries that get frequently polled.

This reduces the time it takes to update history_stats
sensors and can make quite a difference if there
are a lot of them.

When using the mini-graph-card card, all the entities
on the card being graphed are queried every few seconds
for new states. Previously this would tie up the database if there
are lot of these graphs in the UI.

* Update homeassistant/components/history/__init__.py

Co-authored-by: Paulus Schoutsen <paulus@home-assistant.io>

* Update homeassistant/components/history/__init__.py

Co-authored-by: Paulus Schoutsen <paulus@home-assistant.io>

* cache entity_filter in the lambda

* switch to yield

* Revert "switch to yield"

This reverts commit f8386f494002178729b67b54dd299affd406f2f2.

* get_states always returns a list

* query wasnt actually reusable so revert part of the breakout

Co-authored-by: Paulus Schoutsen <paulus@home-assistant.io>
This commit is contained in:
J. Nick KostonandPaulus Schoutsen authored and GitHub committed 2020-07-05 11:03:23 -05:00
1 parent 8bc775dacc
commit b64ae55c66
2 files changed
+120 -52

No files matched your search

+6 -6
View File
@@ -61,7 +61,7 @@ class TestComponentHistory(unittest.TestCase):
def test_get_states(self):
"""Test getting states at a specific point in time."""
self.init_recorder()
self.test_setup()
states = []
now = dt_util.utcnow()
@@ -115,7 +115,7 @@ class TestComponentHistory(unittest.TestCase):
def test_state_changes_during_period(self):
"""Test state change during period."""
self.init_recorder()
self.test_setup()
entity_id = "media_player.test"
def set_state(state):
@@ -156,7 +156,7 @@ class TestComponentHistory(unittest.TestCase):
def test_get_last_state_changes(self):
"""Test number of state changes."""
self.init_recorder()
self.test_setup()
entity_id = "sensor.test"
def set_state(state):
@@ -195,7 +195,7 @@ class TestComponentHistory(unittest.TestCase):
The filter integration uses copy() on states
from history.
"""
self.init_recorder()
self.test_setup()
entity_id = "sensor.test"
def set_state(state):
@@ -608,7 +608,7 @@ class TestComponentHistory(unittest.TestCase):
def test_get_significant_states_only(self):
"""Test significant states when significant_states_only is set."""
self.init_recorder()
self.test_setup()
entity_id = "sensor.test"
def set_state(state, **kwargs):
@@ -683,7 +683,7 @@ class TestComponentHistory(unittest.TestCase):
We inject a bunch of state updates from media player, zone and
thermostat.
"""
self.init_recorder()
self.test_setup()
mp = "media_player.test"
mp2 = "media_player.test2"
mp3 = "media_player.test3"