Do not swallow WLED errors (#72407)

This commit is contained in:
Franck Nijhof 2022-05-24 16:30:41 +02:00 committed by GitHub
parent a5402d725f
commit 652892e535
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 188 additions and 201 deletions

View file

@ -2,7 +2,7 @@
from wled import WLEDConnectionError, WLEDError
from .const import LOGGER
from homeassistant.exceptions import HomeAssistantError
def wled_exception_handler(func):
@ -18,11 +18,11 @@ def wled_exception_handler(func):
self.coordinator.update_listeners()
except WLEDConnectionError as error:
LOGGER.error("Error communicating with API: %s", error)
self.coordinator.last_update_success = False
self.coordinator.update_listeners()
raise HomeAssistantError("Error communicating with WLED API") from error
except WLEDError as error:
LOGGER.error("Invalid response from API: %s", error)
raise HomeAssistantError("Invalid response from WLED API") from error
return handler

View file

@ -17,6 +17,7 @@ from homeassistant.const import (
STATE_UNKNOWN,
)
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers import entity_registry as er
from homeassistant.helpers.entity import EntityCategory
@ -55,43 +56,41 @@ async def test_button_error(
hass: HomeAssistant,
init_integration: MockConfigEntry,
mock_wled: MagicMock,
caplog: pytest.LogCaptureFixture,
) -> None:
"""Test error handling of the WLED buttons."""
mock_wled.reset.side_effect = WLEDError
await hass.services.async_call(
BUTTON_DOMAIN,
SERVICE_PRESS,
{ATTR_ENTITY_ID: "button.wled_rgb_light_restart"},
blocking=True,
)
await hass.async_block_till_done()
with pytest.raises(HomeAssistantError, match="Invalid response from WLED API"):
await hass.services.async_call(
BUTTON_DOMAIN,
SERVICE_PRESS,
{ATTR_ENTITY_ID: "button.wled_rgb_light_restart"},
blocking=True,
)
await hass.async_block_till_done()
state = hass.states.get("button.wled_rgb_light_restart")
assert state
assert state.state == "2021-11-04T16:37:00+00:00"
assert "Invalid response from API" in caplog.text
async def test_button_connection_error(
hass: HomeAssistant,
init_integration: MockConfigEntry,
mock_wled: MagicMock,
caplog: pytest.LogCaptureFixture,
) -> None:
"""Test error handling of the WLED buttons."""
mock_wled.reset.side_effect = WLEDConnectionError
await hass.services.async_call(
BUTTON_DOMAIN,
SERVICE_PRESS,
{ATTR_ENTITY_ID: "button.wled_rgb_light_restart"},
blocking=True,
)
await hass.async_block_till_done()
with pytest.raises(HomeAssistantError, match="Error communicating with WLED API"):
await hass.services.async_call(
BUTTON_DOMAIN,
SERVICE_PRESS,
{ATTR_ENTITY_ID: "button.wled_rgb_light_restart"},
blocking=True,
)
await hass.async_block_till_done()
state = hass.states.get("button.wled_rgb_light_restart")
assert state
assert state.state == STATE_UNAVAILABLE
assert "Error communicating with API" in caplog.text

View file

@ -25,6 +25,7 @@ from homeassistant.const import (
STATE_UNAVAILABLE,
)
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers import entity_registry as er
import homeassistant.util.dt as dt_util
@ -305,23 +306,22 @@ async def test_light_error(
hass: HomeAssistant,
init_integration: MockConfigEntry,
mock_wled: MagicMock,
caplog: pytest.LogCaptureFixture,
) -> None:
"""Test error handling of the WLED lights."""
mock_wled.segment.side_effect = WLEDError
await hass.services.async_call(
LIGHT_DOMAIN,
SERVICE_TURN_OFF,
{ATTR_ENTITY_ID: "light.wled_rgb_light"},
blocking=True,
)
await hass.async_block_till_done()
with pytest.raises(HomeAssistantError, match="Invalid response from WLED API"):
await hass.services.async_call(
LIGHT_DOMAIN,
SERVICE_TURN_OFF,
{ATTR_ENTITY_ID: "light.wled_rgb_light"},
blocking=True,
)
await hass.async_block_till_done()
state = hass.states.get("light.wled_rgb_light")
assert state
assert state.state == STATE_ON
assert "Invalid response from API" in caplog.text
assert mock_wled.segment.call_count == 1
mock_wled.segment.assert_called_with(on=False, segment_id=0, transition=None)
@ -330,23 +330,22 @@ async def test_light_connection_error(
hass: HomeAssistant,
init_integration: MockConfigEntry,
mock_wled: MagicMock,
caplog: pytest.LogCaptureFixture,
) -> None:
"""Test error handling of the WLED switches."""
mock_wled.segment.side_effect = WLEDConnectionError
await hass.services.async_call(
LIGHT_DOMAIN,
SERVICE_TURN_OFF,
{ATTR_ENTITY_ID: "light.wled_rgb_light"},
blocking=True,
)
await hass.async_block_till_done()
with pytest.raises(HomeAssistantError, match="Error communicating with WLED API"):
await hass.services.async_call(
LIGHT_DOMAIN,
SERVICE_TURN_OFF,
{ATTR_ENTITY_ID: "light.wled_rgb_light"},
blocking=True,
)
await hass.async_block_till_done()
state = hass.states.get("light.wled_rgb_light")
assert state
assert state.state == STATE_UNAVAILABLE
assert "Error communicating with API" in caplog.text
assert mock_wled.segment.call_count == 1
mock_wled.segment.assert_called_with(on=False, segment_id=0, transition=None)

View file

@ -14,6 +14,7 @@ from homeassistant.components.number.const import (
from homeassistant.components.wled.const import SCAN_INTERVAL
from homeassistant.const import ATTR_ENTITY_ID, ATTR_ICON, STATE_UNAVAILABLE
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers import entity_registry as er
import homeassistant.util.dt as dt_util
@ -109,26 +110,25 @@ async def test_speed_error(
hass: HomeAssistant,
init_integration: MockConfigEntry,
mock_wled: MagicMock,
caplog: pytest.LogCaptureFixture,
) -> None:
"""Test error handling of the WLED numbers."""
mock_wled.segment.side_effect = WLEDError
await hass.services.async_call(
NUMBER_DOMAIN,
SERVICE_SET_VALUE,
{
ATTR_ENTITY_ID: "number.wled_rgb_light_segment_1_speed",
ATTR_VALUE: 42,
},
blocking=True,
)
await hass.async_block_till_done()
with pytest.raises(HomeAssistantError, match="Invalid response from WLED API"):
await hass.services.async_call(
NUMBER_DOMAIN,
SERVICE_SET_VALUE,
{
ATTR_ENTITY_ID: "number.wled_rgb_light_segment_1_speed",
ATTR_VALUE: 42,
},
blocking=True,
)
await hass.async_block_till_done()
state = hass.states.get("number.wled_rgb_light_segment_1_speed")
assert state
assert state.state == "16"
assert "Invalid response from API" in caplog.text
assert mock_wled.segment.call_count == 1
mock_wled.segment.assert_called_with(segment_id=1, speed=42)
@ -137,26 +137,25 @@ async def test_speed_connection_error(
hass: HomeAssistant,
init_integration: MockConfigEntry,
mock_wled: MagicMock,
caplog: pytest.LogCaptureFixture,
) -> None:
"""Test error handling of the WLED numbers."""
mock_wled.segment.side_effect = WLEDConnectionError
await hass.services.async_call(
NUMBER_DOMAIN,
SERVICE_SET_VALUE,
{
ATTR_ENTITY_ID: "number.wled_rgb_light_segment_1_speed",
ATTR_VALUE: 42,
},
blocking=True,
)
await hass.async_block_till_done()
with pytest.raises(HomeAssistantError, match="Error communicating with WLED API"):
await hass.services.async_call(
NUMBER_DOMAIN,
SERVICE_SET_VALUE,
{
ATTR_ENTITY_ID: "number.wled_rgb_light_segment_1_speed",
ATTR_VALUE: 42,
},
blocking=True,
)
await hass.async_block_till_done()
state = hass.states.get("number.wled_rgb_light_segment_1_speed")
assert state
assert state.state == STATE_UNAVAILABLE
assert "Error communicating with API" in caplog.text
assert mock_wled.segment.call_count == 1
mock_wled.segment.assert_called_with(segment_id=1, speed=42)
@ -250,26 +249,25 @@ async def test_intensity_error(
hass: HomeAssistant,
init_integration: MockConfigEntry,
mock_wled: MagicMock,
caplog: pytest.LogCaptureFixture,
) -> None:
"""Test error handling of the WLED numbers."""
mock_wled.segment.side_effect = WLEDError
await hass.services.async_call(
NUMBER_DOMAIN,
SERVICE_SET_VALUE,
{
ATTR_ENTITY_ID: "number.wled_rgb_light_segment_1_intensity",
ATTR_VALUE: 21,
},
blocking=True,
)
await hass.async_block_till_done()
with pytest.raises(HomeAssistantError, match="Invalid response from WLED API"):
await hass.services.async_call(
NUMBER_DOMAIN,
SERVICE_SET_VALUE,
{
ATTR_ENTITY_ID: "number.wled_rgb_light_segment_1_intensity",
ATTR_VALUE: 21,
},
blocking=True,
)
await hass.async_block_till_done()
state = hass.states.get("number.wled_rgb_light_segment_1_intensity")
assert state
assert state.state == "64"
assert "Invalid response from API" in caplog.text
assert mock_wled.segment.call_count == 1
mock_wled.segment.assert_called_with(segment_id=1, intensity=21)
@ -278,25 +276,24 @@ async def test_intensity_connection_error(
hass: HomeAssistant,
init_integration: MockConfigEntry,
mock_wled: MagicMock,
caplog: pytest.LogCaptureFixture,
) -> None:
"""Test error handling of the WLED numbers."""
mock_wled.segment.side_effect = WLEDConnectionError
await hass.services.async_call(
NUMBER_DOMAIN,
SERVICE_SET_VALUE,
{
ATTR_ENTITY_ID: "number.wled_rgb_light_segment_1_intensity",
ATTR_VALUE: 128,
},
blocking=True,
)
await hass.async_block_till_done()
with pytest.raises(HomeAssistantError, match="Error communicating with WLED API"):
await hass.services.async_call(
NUMBER_DOMAIN,
SERVICE_SET_VALUE,
{
ATTR_ENTITY_ID: "number.wled_rgb_light_segment_1_intensity",
ATTR_VALUE: 128,
},
blocking=True,
)
await hass.async_block_till_done()
state = hass.states.get("number.wled_rgb_light_segment_1_intensity")
assert state
assert state.state == STATE_UNAVAILABLE
assert "Error communicating with API" in caplog.text
assert mock_wled.segment.call_count == 1
mock_wled.segment.assert_called_with(segment_id=1, intensity=128)

View file

@ -16,6 +16,7 @@ from homeassistant.const import (
STATE_UNKNOWN,
)
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers import entity_registry as er
from homeassistant.helpers.entity import EntityCategory
import homeassistant.util.dt as dt_util
@ -161,26 +162,25 @@ async def test_color_palette_select_error(
hass: HomeAssistant,
init_integration: MockConfigEntry,
mock_wled: MagicMock,
caplog: pytest.LogCaptureFixture,
) -> None:
"""Test error handling of the WLED selects."""
mock_wled.segment.side_effect = WLEDError
await hass.services.async_call(
SELECT_DOMAIN,
SERVICE_SELECT_OPTION,
{
ATTR_ENTITY_ID: "select.wled_rgb_light_segment_1_color_palette",
ATTR_OPTION: "Icefire",
},
blocking=True,
)
await hass.async_block_till_done()
with pytest.raises(HomeAssistantError, match="Invalid response from WLED API"):
await hass.services.async_call(
SELECT_DOMAIN,
SERVICE_SELECT_OPTION,
{
ATTR_ENTITY_ID: "select.wled_rgb_light_segment_1_color_palette",
ATTR_OPTION: "Icefire",
},
blocking=True,
)
await hass.async_block_till_done()
state = hass.states.get("select.wled_rgb_light_segment_1_color_palette")
assert state
assert state.state == "Random Cycle"
assert "Invalid response from API" in caplog.text
assert mock_wled.segment.call_count == 1
mock_wled.segment.assert_called_with(segment_id=1, palette="Icefire")
@ -189,26 +189,25 @@ async def test_color_palette_select_connection_error(
hass: HomeAssistant,
init_integration: MockConfigEntry,
mock_wled: MagicMock,
caplog: pytest.LogCaptureFixture,
) -> None:
"""Test error handling of the WLED selects."""
mock_wled.segment.side_effect = WLEDConnectionError
await hass.services.async_call(
SELECT_DOMAIN,
SERVICE_SELECT_OPTION,
{
ATTR_ENTITY_ID: "select.wled_rgb_light_segment_1_color_palette",
ATTR_OPTION: "Icefire",
},
blocking=True,
)
await hass.async_block_till_done()
with pytest.raises(HomeAssistantError, match="Error communicating with WLED API"):
await hass.services.async_call(
SELECT_DOMAIN,
SERVICE_SELECT_OPTION,
{
ATTR_ENTITY_ID: "select.wled_rgb_light_segment_1_color_palette",
ATTR_OPTION: "Icefire",
},
blocking=True,
)
await hass.async_block_till_done()
state = hass.states.get("select.wled_rgb_light_segment_1_color_palette")
assert state
assert state.state == STATE_UNAVAILABLE
assert "Error communicating with API" in caplog.text
assert mock_wled.segment.call_count == 1
mock_wled.segment.assert_called_with(segment_id=1, palette="Icefire")
@ -280,26 +279,25 @@ async def test_preset_select_error(
hass: HomeAssistant,
init_integration: MockConfigEntry,
mock_wled: MagicMock,
caplog: pytest.LogCaptureFixture,
) -> None:
"""Test error handling of the WLED selects."""
mock_wled.preset.side_effect = WLEDError
await hass.services.async_call(
SELECT_DOMAIN,
SERVICE_SELECT_OPTION,
{
ATTR_ENTITY_ID: "select.wled_rgbw_light_preset",
ATTR_OPTION: "Preset 2",
},
blocking=True,
)
await hass.async_block_till_done()
with pytest.raises(HomeAssistantError, match="Invalid response from WLED API"):
await hass.services.async_call(
SELECT_DOMAIN,
SERVICE_SELECT_OPTION,
{
ATTR_ENTITY_ID: "select.wled_rgbw_light_preset",
ATTR_OPTION: "Preset 2",
},
blocking=True,
)
await hass.async_block_till_done()
state = hass.states.get("select.wled_rgbw_light_preset")
assert state
assert state.state == "Preset 1"
assert "Invalid response from API" in caplog.text
assert mock_wled.preset.call_count == 1
mock_wled.preset.assert_called_with(preset="Preset 2")
@ -309,26 +307,25 @@ async def test_preset_select_connection_error(
hass: HomeAssistant,
init_integration: MockConfigEntry,
mock_wled: MagicMock,
caplog: pytest.LogCaptureFixture,
) -> None:
"""Test error handling of the WLED selects."""
mock_wled.preset.side_effect = WLEDConnectionError
await hass.services.async_call(
SELECT_DOMAIN,
SERVICE_SELECT_OPTION,
{
ATTR_ENTITY_ID: "select.wled_rgbw_light_preset",
ATTR_OPTION: "Preset 2",
},
blocking=True,
)
await hass.async_block_till_done()
with pytest.raises(HomeAssistantError, match="Error communicating with WLED API"):
await hass.services.async_call(
SELECT_DOMAIN,
SERVICE_SELECT_OPTION,
{
ATTR_ENTITY_ID: "select.wled_rgbw_light_preset",
ATTR_OPTION: "Preset 2",
},
blocking=True,
)
await hass.async_block_till_done()
state = hass.states.get("select.wled_rgbw_light_preset")
assert state
assert state.state == STATE_UNAVAILABLE
assert "Error communicating with API" in caplog.text
assert mock_wled.preset.call_count == 1
mock_wled.preset.assert_called_with(preset="Preset 2")
@ -400,26 +397,25 @@ async def test_playlist_select_error(
hass: HomeAssistant,
init_integration: MockConfigEntry,
mock_wled: MagicMock,
caplog: pytest.LogCaptureFixture,
) -> None:
"""Test error handling of the WLED selects."""
mock_wled.playlist.side_effect = WLEDError
await hass.services.async_call(
SELECT_DOMAIN,
SERVICE_SELECT_OPTION,
{
ATTR_ENTITY_ID: "select.wled_rgbw_light_playlist",
ATTR_OPTION: "Playlist 2",
},
blocking=True,
)
await hass.async_block_till_done()
with pytest.raises(HomeAssistantError, match="Invalid response from WLED API"):
await hass.services.async_call(
SELECT_DOMAIN,
SERVICE_SELECT_OPTION,
{
ATTR_ENTITY_ID: "select.wled_rgbw_light_playlist",
ATTR_OPTION: "Playlist 2",
},
blocking=True,
)
await hass.async_block_till_done()
state = hass.states.get("select.wled_rgbw_light_playlist")
assert state
assert state.state == "Playlist 1"
assert "Invalid response from API" in caplog.text
assert mock_wled.playlist.call_count == 1
mock_wled.playlist.assert_called_with(playlist="Playlist 2")
@ -429,26 +425,25 @@ async def test_playlist_select_connection_error(
hass: HomeAssistant,
init_integration: MockConfigEntry,
mock_wled: MagicMock,
caplog: pytest.LogCaptureFixture,
) -> None:
"""Test error handling of the WLED selects."""
mock_wled.playlist.side_effect = WLEDConnectionError
await hass.services.async_call(
SELECT_DOMAIN,
SERVICE_SELECT_OPTION,
{
ATTR_ENTITY_ID: "select.wled_rgbw_light_playlist",
ATTR_OPTION: "Playlist 2",
},
blocking=True,
)
await hass.async_block_till_done()
with pytest.raises(HomeAssistantError, match="Error communicating with WLED API"):
await hass.services.async_call(
SELECT_DOMAIN,
SERVICE_SELECT_OPTION,
{
ATTR_ENTITY_ID: "select.wled_rgbw_light_playlist",
ATTR_OPTION: "Playlist 2",
},
blocking=True,
)
await hass.async_block_till_done()
state = hass.states.get("select.wled_rgbw_light_playlist")
assert state
assert state.state == STATE_UNAVAILABLE
assert "Error communicating with API" in caplog.text
assert mock_wled.playlist.call_count == 1
mock_wled.playlist.assert_called_with(playlist="Playlist 2")
@ -489,26 +484,25 @@ async def test_live_select_error(
hass: HomeAssistant,
init_integration: MockConfigEntry,
mock_wled: MagicMock,
caplog: pytest.LogCaptureFixture,
) -> None:
"""Test error handling of the WLED selects."""
mock_wled.live.side_effect = WLEDError
await hass.services.async_call(
SELECT_DOMAIN,
SERVICE_SELECT_OPTION,
{
ATTR_ENTITY_ID: "select.wled_rgb_light_live_override",
ATTR_OPTION: "1",
},
blocking=True,
)
await hass.async_block_till_done()
with pytest.raises(HomeAssistantError, match="Invalid response from WLED API"):
await hass.services.async_call(
SELECT_DOMAIN,
SERVICE_SELECT_OPTION,
{
ATTR_ENTITY_ID: "select.wled_rgb_light_live_override",
ATTR_OPTION: "1",
},
blocking=True,
)
await hass.async_block_till_done()
state = hass.states.get("select.wled_rgb_light_live_override")
assert state
assert state.state == "0"
assert "Invalid response from API" in caplog.text
assert mock_wled.live.call_count == 1
mock_wled.live.assert_called_with(live=1)
@ -517,25 +511,24 @@ async def test_live_select_connection_error(
hass: HomeAssistant,
init_integration: MockConfigEntry,
mock_wled: MagicMock,
caplog: pytest.LogCaptureFixture,
) -> None:
"""Test error handling of the WLED selects."""
mock_wled.live.side_effect = WLEDConnectionError
await hass.services.async_call(
SELECT_DOMAIN,
SERVICE_SELECT_OPTION,
{
ATTR_ENTITY_ID: "select.wled_rgb_light_live_override",
ATTR_OPTION: "2",
},
blocking=True,
)
await hass.async_block_till_done()
with pytest.raises(HomeAssistantError, match="Error communicating with WLED API"):
await hass.services.async_call(
SELECT_DOMAIN,
SERVICE_SELECT_OPTION,
{
ATTR_ENTITY_ID: "select.wled_rgb_light_live_override",
ATTR_OPTION: "2",
},
blocking=True,
)
await hass.async_block_till_done()
state = hass.states.get("select.wled_rgb_light_live_override")
assert state
assert state.state == STATE_UNAVAILABLE
assert "Error communicating with API" in caplog.text
assert mock_wled.live.call_count == 1
mock_wled.live.assert_called_with(live=2)

View file

@ -23,6 +23,7 @@ from homeassistant.const import (
STATE_UNAVAILABLE,
)
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers import entity_registry as er
from homeassistant.helpers.entity import EntityCategory
import homeassistant.util.dt as dt_util
@ -175,46 +176,44 @@ async def test_switch_error(
hass: HomeAssistant,
init_integration: MockConfigEntry,
mock_wled: MagicMock,
caplog: pytest.LogCaptureFixture,
) -> None:
"""Test error handling of the WLED switches."""
mock_wled.nightlight.side_effect = WLEDError
await hass.services.async_call(
SWITCH_DOMAIN,
SERVICE_TURN_ON,
{ATTR_ENTITY_ID: "switch.wled_rgb_light_nightlight"},
blocking=True,
)
await hass.async_block_till_done()
with pytest.raises(HomeAssistantError, match="Invalid response from WLED API"):
await hass.services.async_call(
SWITCH_DOMAIN,
SERVICE_TURN_ON,
{ATTR_ENTITY_ID: "switch.wled_rgb_light_nightlight"},
blocking=True,
)
await hass.async_block_till_done()
state = hass.states.get("switch.wled_rgb_light_nightlight")
assert state
assert state.state == STATE_OFF
assert "Invalid response from API" in caplog.text
async def test_switch_connection_error(
hass: HomeAssistant,
init_integration: MockConfigEntry,
mock_wled: MagicMock,
caplog: pytest.LogCaptureFixture,
) -> None:
"""Test error handling of the WLED switches."""
mock_wled.nightlight.side_effect = WLEDConnectionError
await hass.services.async_call(
SWITCH_DOMAIN,
SERVICE_TURN_ON,
{ATTR_ENTITY_ID: "switch.wled_rgb_light_nightlight"},
blocking=True,
)
await hass.async_block_till_done()
with pytest.raises(HomeAssistantError, match="Error communicating with WLED API"):
await hass.services.async_call(
SWITCH_DOMAIN,
SERVICE_TURN_ON,
{ATTR_ENTITY_ID: "switch.wled_rgb_light_nightlight"},
blocking=True,
)
await hass.async_block_till_done()
state = hass.states.get("switch.wled_rgb_light_nightlight")
assert state
assert state.state == STATE_UNAVAILABLE
assert "Error communicating with API" in caplog.text
@pytest.mark.parametrize("mock_wled", ["wled/rgb_single_segment.json"], indirect=True)