Cache is_supported for Google entities (#73936)

This commit is contained in:
Paulus Schoutsen 2022-06-24 17:05:36 -04:00 committed by GitHub
parent 44da543ca0
commit 57efa9569c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 46 additions and 2 deletions

View file

@ -327,7 +327,9 @@ async def test_sync_entities_all(agents, result):
def test_supported_features_string(caplog):
"""Test bad supported features."""
entity = helpers.GoogleEntity(
None, None, State("test.entity_id", "on", {"supported_features": "invalid"})
None,
MockConfig(),
State("test.entity_id", "on", {"supported_features": "invalid"}),
)
assert entity.is_supported() is False
assert "Entity test.entity_id contains invalid supported_features value invalid"
@ -427,3 +429,34 @@ async def test_config_local_sdk_warn_version(hass, hass_client, caplog, version)
f"Local SDK version is too old ({version}), check documentation on how "
"to update to the latest version"
) in caplog.text
def test_is_supported_cached():
"""Test is_supported is cached."""
config = MockConfig()
def entity(features: int):
return helpers.GoogleEntity(
None,
config,
State("test.entity_id", "on", {"supported_features": features}),
)
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
# Supported feature changes, so we calculate again
assert entity(2).is_supported() is True
assert len(mock_traits.mock_calls) == 2
mock_traits.reset_mock()
# Supported feature is same, so we do not calculate again
mock_traits.side_effect = ValueError
assert entity(2).is_supported() is True
assert len(mock_traits.mock_calls) == 0