Add error handling to LaMetric number platform (#80159)

pull/80162/head
Franck Nijhof 2022-10-12 11:33:09 +02:00 committed by GitHub
parent 107e1ed16c
commit 77571c8a84
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 69 additions and 0 deletions

View File

@ -16,6 +16,7 @@ from homeassistant.helpers.entity_platform import AddEntitiesCallback
from .const import DOMAIN
from .coordinator import LaMetricDataUpdateCoordinator
from .entity import LaMetricEntity
from .helpers import lametric_exception_handler
@dataclass
@ -96,6 +97,7 @@ class LaMetricNumberEntity(LaMetricEntity, NumberEntity):
"""Return the number value."""
return self.entity_description.value_fn(self.coordinator.data)
@lametric_exception_handler
async def async_set_native_value(self, value: float) -> None:
"""Change to new number value."""
await self.entity_description.set_value_fn(self.coordinator.lametric, value)

View File

@ -1,6 +1,9 @@
"""Tests for the LaMetric number platform."""
from unittest.mock import MagicMock
from demetriek import LaMetricConnectionError, LaMetricError
import pytest
from homeassistant.components.lametric.const import DOMAIN
from homeassistant.components.number import (
ATTR_MAX,
@ -16,8 +19,10 @@ from homeassistant.const import (
ATTR_FRIENDLY_NAME,
ATTR_ICON,
ATTR_UNIT_OF_MEASUREMENT,
STATE_UNAVAILABLE,
)
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers import device_registry as dr, entity_registry as er
from homeassistant.helpers.entity import EntityCategory
@ -125,3 +130,65 @@ async def test_volume(
assert len(mock_lametric.audio.mock_calls) == 1
mock_lametric.audio.assert_called_once_with(volume=42)
async def test_number_error(
hass: HomeAssistant,
init_integration: MockConfigEntry,
mock_lametric: MagicMock,
) -> None:
"""Test error handling of the LaMetric numbers."""
mock_lametric.audio.side_effect = LaMetricError
state = hass.states.get("number.frenck_s_lametric_volume")
assert state
assert state.state == "100"
with pytest.raises(
HomeAssistantError, match="Invalid response from the LaMetric device"
):
await hass.services.async_call(
NUMBER_DOMAIN,
SERVICE_SET_VALUE,
{
ATTR_ENTITY_ID: "number.frenck_s_lametric_volume",
ATTR_VALUE: 42,
},
blocking=True,
)
await hass.async_block_till_done()
state = hass.states.get("number.frenck_s_lametric_volume")
assert state
assert state.state == "100"
async def test_number_connection_error(
hass: HomeAssistant,
init_integration: MockConfigEntry,
mock_lametric: MagicMock,
) -> None:
"""Test connection error handling of the LaMetric numbers."""
mock_lametric.audio.side_effect = LaMetricConnectionError
state = hass.states.get("number.frenck_s_lametric_volume")
assert state
assert state.state == "100"
with pytest.raises(
HomeAssistantError, match="Error communicating with the LaMetric device"
):
await hass.services.async_call(
NUMBER_DOMAIN,
SERVICE_SET_VALUE,
{
ATTR_ENTITY_ID: "number.frenck_s_lametric_volume",
ATTR_VALUE: 42,
},
blocking=True,
)
await hass.async_block_till_done()
state = hass.states.get("number.frenck_s_lametric_volume")
assert state
assert state.state == STATE_UNAVAILABLE