mirror of
https://github.com/home-assistant/core.git
synced 2026-09-28 02:18:10 -04:00
Improve performance of google assistant supported checks (#99454)
* Improve performance of google assistant supported checks * tweak * tweak * split function * tweak
This commit is contained in:
@@ -447,32 +447,53 @@ async def test_config_local_sdk_warn_version(
|
||||
) in caplog.text
|
||||
|
||||
|
||||
def test_is_supported_cached() -> None:
|
||||
"""Test is_supported is cached."""
|
||||
def test_async_get_entities_cached(hass: HomeAssistant) -> None:
|
||||
"""Test async_get_entities is cached."""
|
||||
config = MockConfig()
|
||||
|
||||
def entity(features: int):
|
||||
return helpers.GoogleEntity(
|
||||
None,
|
||||
config,
|
||||
State("test.entity_id", "on", {"supported_features": features}),
|
||||
)
|
||||
hass.states.async_set("light.ceiling_lights", "off")
|
||||
hass.states.async_set("light.bed_light", "off")
|
||||
hass.states.async_set("not_supported.not_supported", "off")
|
||||
|
||||
google_entities = helpers.async_get_entities(hass, config)
|
||||
assert len(google_entities) == 2
|
||||
assert config.is_supported_cache == {
|
||||
"light.bed_light": (None, True),
|
||||
"light.ceiling_lights": (None, True),
|
||||
"not_supported.not_supported": (None, False),
|
||||
}
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.google_assistant.helpers.GoogleEntity.traits",
|
||||
return_value=[1],
|
||||
) as mock_traits:
|
||||
assert entity(1).is_supported() is True
|
||||
assert len(mock_traits.mock_calls) == 1
|
||||
return_value=RuntimeError("Should not be called"),
|
||||
):
|
||||
google_entities = helpers.async_get_entities(hass, config)
|
||||
|
||||
# Supported feature changes, so we calculate again
|
||||
assert entity(2).is_supported() is True
|
||||
assert len(mock_traits.mock_calls) == 2
|
||||
assert len(google_entities) == 2
|
||||
assert config.is_supported_cache == {
|
||||
"light.bed_light": (None, True),
|
||||
"light.ceiling_lights": (None, True),
|
||||
"not_supported.not_supported": (None, False),
|
||||
}
|
||||
|
||||
mock_traits.reset_mock()
|
||||
hass.states.async_set("light.new", "on")
|
||||
google_entities = helpers.async_get_entities(hass, config)
|
||||
|
||||
# Supported feature is same, so we do not calculate again
|
||||
mock_traits.side_effect = ValueError
|
||||
assert len(google_entities) == 3
|
||||
assert config.is_supported_cache == {
|
||||
"light.bed_light": (None, True),
|
||||
"light.new": (None, True),
|
||||
"light.ceiling_lights": (None, True),
|
||||
"not_supported.not_supported": (None, False),
|
||||
}
|
||||
|
||||
assert entity(2).is_supported() is True
|
||||
assert len(mock_traits.mock_calls) == 0
|
||||
hass.states.async_set("light.new", "on", {"supported_features": 1})
|
||||
google_entities = helpers.async_get_entities(hass, config)
|
||||
|
||||
assert len(google_entities) == 3
|
||||
assert config.is_supported_cache == {
|
||||
"light.bed_light": (None, True),
|
||||
"light.new": (1, True),
|
||||
"light.ceiling_lights": (None, True),
|
||||
"not_supported.not_supported": (None, False),
|
||||
}
|
||||
|
||||
@@ -69,7 +69,7 @@ async def test_report_state(
|
||||
|
||||
# Test that if serialize returns same value, we don't send
|
||||
with patch(
|
||||
"homeassistant.components.google_assistant.report_state.GoogleEntity.query_serialize",
|
||||
"homeassistant.components.google_assistant.helpers.GoogleEntity.query_serialize",
|
||||
return_value={"same": "info"},
|
||||
), patch.object(BASIC_CONFIG, "async_report_state_all", AsyncMock()) as mock_report:
|
||||
# New state, so reported
|
||||
@@ -104,7 +104,7 @@ async def test_report_state(
|
||||
with patch.object(
|
||||
BASIC_CONFIG, "async_report_state_all", AsyncMock()
|
||||
) as mock_report, patch(
|
||||
"homeassistant.components.google_assistant.report_state.GoogleEntity.query_serialize",
|
||||
"homeassistant.components.google_assistant.helpers.GoogleEntity.query_serialize",
|
||||
side_effect=error.SmartHomeError("mock-error", "mock-msg"),
|
||||
):
|
||||
hass.states.async_set("light.kitchen", "off")
|
||||
|
||||
Reference in New Issue
Block a user