Stabilize alexa discovery (#108787)

This commit is contained in:
Jan Bouwhuis 2024-01-24 18:56:21 +01:00 committed by GitHub
parent 5467fe8ff1
commit a90d8b6a0c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 126 additions and 49 deletions

View file

@ -1,10 +1,11 @@
"""Test Alexa entity representation."""
from typing import Any
from unittest.mock import patch
import pytest
from homeassistant.components.alexa import smart_home
from homeassistant.const import EntityCategory, __version__
from homeassistant.const import EntityCategory, UnitOfTemperature, __version__
from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er
@ -75,7 +76,7 @@ async def test_categorized_hidden_entities(
async def test_serialize_discovery(hass: HomeAssistant) -> None:
"""Test we handle an interface raising unexpectedly during serialize discovery."""
"""Test we can serialize a discovery."""
request = get_new_request("Alexa.Discovery", "Discover")
hass.states.async_set("switch.bla", "on", {"friendly_name": "Boop Woz"})
@ -94,6 +95,82 @@ async def test_serialize_discovery(hass: HomeAssistant) -> None:
}
async def test_serialize_discovery_partly_fails(
hass: HomeAssistant, caplog: pytest.LogCaptureFixture
) -> None:
"""Test we can partly serialize a discovery."""
async def _mock_discovery() -> dict[str, Any]:
request = get_new_request("Alexa.Discovery", "Discover")
hass.states.async_set("switch.bla", "on", {"friendly_name": "My Switch"})
hass.states.async_set("fan.bla", "on", {"friendly_name": "My Fan"})
hass.states.async_set(
"humidifier.bla", "on", {"friendly_name": "My Humidifier"}
)
hass.states.async_set(
"sensor.bla",
"20.1",
{
"friendly_name": "Livingroom temperature",
"unit_of_measurement": UnitOfTemperature.CELSIUS,
"device_class": "temperature",
},
)
return await smart_home.async_handle_message(
hass, get_default_config(hass), request
)
msg = await _mock_discovery()
assert "event" in msg
msg = msg["event"]
assert len(msg["payload"]["endpoints"]) == 4
endpoint_ids = {
attributes["endpointId"] for attributes in msg["payload"]["endpoints"]
}
assert all(
entity in endpoint_ids
for entity in ["switch#bla", "fan#bla", "humidifier#bla", "sensor#bla"]
)
# Simulate fetching the interfaces fails for fan entity
with patch(
"homeassistant.components.alexa.entities.FanCapabilities.interfaces",
side_effect=TypeError(),
):
msg = await _mock_discovery()
assert "event" in msg
msg = msg["event"]
assert len(msg["payload"]["endpoints"]) == 3
endpoint_ids = {
attributes["endpointId"] for attributes in msg["payload"]["endpoints"]
}
assert all(
entity in endpoint_ids
for entity in ["switch#bla", "humidifier#bla", "sensor#bla"]
)
assert "Unable to serialize fan.bla for discovery" in caplog.text
caplog.clear()
# Simulate serializing properties fails for sensor entity
with patch(
"homeassistant.components.alexa.entities.SensorCapabilities.default_display_categories",
side_effect=ValueError(),
):
msg = await _mock_discovery()
assert "event" in msg
msg = msg["event"]
assert len(msg["payload"]["endpoints"]) == 3
endpoint_ids = {
attributes["endpointId"] for attributes in msg["payload"]["endpoints"]
}
assert all(
entity in endpoint_ids
for entity in ["switch#bla", "humidifier#bla", "fan#bla"]
)
assert "Unable to serialize sensor.bla for discovery" in caplog.text
caplog.clear()
async def test_serialize_discovery_recovers(
hass: HomeAssistant, caplog: pytest.LogCaptureFixture
) -> None: