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:
J. Nick Koston 2023-09-04 19:53:59 -05:00 committed by GitHub
parent 63273a307a
commit ff2e0c570b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 144 additions and 65 deletions

View file

@ -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),
}