1
0
mirror of https://github.com/home-assistant/core.git synced 2025-12-24 12:59:34 +00:00

ESPHome: Catch and re-raise client library errors as HomeAssistantErrors (#113026)

This commit is contained in:
Jesse Hills
2024-03-13 17:06:25 +13:00
committed by GitHub
parent d2bd68ba30
commit a2a8a8f119
14 changed files with 176 additions and 14 deletions

View File

@@ -1,14 +1,16 @@
"""Test ESPHome numbers."""
import math
from unittest.mock import call
from unittest.mock import Mock, call
from aioesphomeapi import (
APIClient,
APIConnectionError,
NumberInfo,
NumberMode as ESPHomeNumberMode,
NumberState,
)
import pytest
from homeassistant.components.number import (
ATTR_VALUE,
@@ -17,6 +19,7 @@ from homeassistant.components.number import (
)
from homeassistant.const import ATTR_ENTITY_ID, ATTR_UNIT_OF_MEASUREMENT, STATE_UNKNOWN
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import HomeAssistantError
async def test_generic_number_entity(
@@ -122,3 +125,42 @@ async def test_generic_number_with_unit_of_measurement_as_empty_string(
assert state is not None
assert state.state == "42"
assert ATTR_UNIT_OF_MEASUREMENT not in state.attributes
async def test_generic_number_entity_set_when_disconnected(
hass: HomeAssistant,
mock_client: APIClient,
mock_generic_device_entry,
) -> None:
"""Test a generic number entity."""
entity_info = [
NumberInfo(
object_id="mynumber",
key=1,
name="my number",
unique_id="my_number",
max_value=100,
min_value=0,
step=1,
unit_of_measurement="%",
)
]
states = [NumberState(key=1, state=50)]
user_service = []
await mock_generic_device_entry(
mock_client=mock_client,
entity_info=entity_info,
user_service=user_service,
states=states,
)
mock_client.number_command = Mock(side_effect=APIConnectionError("Not connected"))
with pytest.raises(HomeAssistantError):
await hass.services.async_call(
NUMBER_DOMAIN,
SERVICE_SET_VALUE,
{ATTR_ENTITY_ID: "number.test_mynumber", ATTR_VALUE: 20},
blocking=True,
)
mock_client.number_command.reset_mock()