Batch Google Report State (#49511)

* Batch Google Report State

* Fix batching
This commit is contained in:
Paulus Schoutsen 2021-04-23 10:56:23 -07:00 committed by GitHub
parent c6edc7ae4f
commit a6d87b7fae
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 85 additions and 12 deletions

View file

@ -1,4 +1,5 @@
"""Test Google report state."""
from datetime import timedelta
from unittest.mock import AsyncMock, patch
from homeassistant.components.google_assistant import error, report_state
@ -41,10 +42,25 @@ async def test_report_state(hass, caplog, legacy_patchable_time):
hass.states.async_set("light.kitchen", "on")
await hass.async_block_till_done()
assert len(mock_report.mock_calls) == 1
assert mock_report.mock_calls[0][1][0] == {
"devices": {"states": {"light.kitchen": {"on": True, "online": True}}}
}
hass.states.async_set("light.kitchen_2", "on")
await hass.async_block_till_done()
assert len(mock_report.mock_calls) == 0
async_fire_time_changed(
hass, utcnow() + timedelta(seconds=report_state.REPORT_STATE_WINDOW)
)
await hass.async_block_till_done()
assert len(mock_report.mock_calls) == 1
assert mock_report.mock_calls[0][1][0] == {
"devices": {
"states": {
"light.kitchen": {"on": True, "online": True},
"light.kitchen_2": {"on": True, "online": True},
},
}
}
# Test that if serialize returns same value, we don't send
with patch(
@ -57,6 +73,9 @@ async def test_report_state(hass, caplog, legacy_patchable_time):
# Changed, but serialize is same, so filtered out by extra check
hass.states.async_set("light.double_report", "off")
async_fire_time_changed(
hass, utcnow() + timedelta(seconds=report_state.REPORT_STATE_WINDOW)
)
await hass.async_block_till_done()
assert len(mock_report.mock_calls) == 1
@ -69,6 +88,9 @@ async def test_report_state(hass, caplog, legacy_patchable_time):
BASIC_CONFIG, "async_report_state_all", AsyncMock()
) as mock_report:
hass.states.async_set("switch.ac", "on", {"something": "else"})
async_fire_time_changed(
hass, utcnow() + timedelta(seconds=report_state.REPORT_STATE_WINDOW)
)
await hass.async_block_till_done()
assert len(mock_report.mock_calls) == 0
@ -81,9 +103,12 @@ async def test_report_state(hass, caplog, legacy_patchable_time):
side_effect=error.SmartHomeError("mock-error", "mock-msg"),
):
hass.states.async_set("light.kitchen", "off")
async_fire_time_changed(
hass, utcnow() + timedelta(seconds=report_state.REPORT_STATE_WINDOW)
)
await hass.async_block_till_done()
assert "Not reporting state for light.kitchen: mock-error"
assert "Not reporting state for light.kitchen: mock-error" in caplog.text
assert len(mock_report.mock_calls) == 0
unsub()
@ -92,6 +117,9 @@ async def test_report_state(hass, caplog, legacy_patchable_time):
BASIC_CONFIG, "async_report_state_all", AsyncMock()
) as mock_report:
hass.states.async_set("light.kitchen", "on")
async_fire_time_changed(
hass, utcnow() + timedelta(seconds=report_state.REPORT_STATE_WINDOW)
)
await hass.async_block_till_done()
assert len(mock_report.mock_calls) == 0