mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 05:07:41 +00:00
Add exceptions and translations for slide_local (#133490)
This commit is contained in:
parent
c9f1829c0b
commit
d6c201de4a
@ -2,16 +2,25 @@
|
|||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from goslideapi.goslideapi import (
|
||||||
|
AuthenticationFailed,
|
||||||
|
ClientConnectionError,
|
||||||
|
ClientTimeoutError,
|
||||||
|
DigestAuthCalcError,
|
||||||
|
)
|
||||||
|
|
||||||
from homeassistant.components.button import ButtonEntity
|
from homeassistant.components.button import ButtonEntity
|
||||||
from homeassistant.const import EntityCategory
|
from homeassistant.const import EntityCategory
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
|
from homeassistant.exceptions import HomeAssistantError
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
|
|
||||||
from . import SlideConfigEntry
|
from . import SlideConfigEntry
|
||||||
|
from .const import DOMAIN
|
||||||
from .coordinator import SlideCoordinator
|
from .coordinator import SlideCoordinator
|
||||||
from .entity import SlideEntity
|
from .entity import SlideEntity
|
||||||
|
|
||||||
PARALLEL_UPDATES = 0
|
PARALLEL_UPDATES = 1
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(
|
async def async_setup_entry(
|
||||||
@ -39,4 +48,15 @@ class SlideButton(SlideEntity, ButtonEntity):
|
|||||||
|
|
||||||
async def async_press(self) -> None:
|
async def async_press(self) -> None:
|
||||||
"""Send out a calibrate command."""
|
"""Send out a calibrate command."""
|
||||||
await self.coordinator.slide.slide_calibrate(self.coordinator.host)
|
try:
|
||||||
|
await self.coordinator.slide.slide_calibrate(self.coordinator.host)
|
||||||
|
except (
|
||||||
|
ClientConnectionError,
|
||||||
|
AuthenticationFailed,
|
||||||
|
ClientTimeoutError,
|
||||||
|
DigestAuthCalcError,
|
||||||
|
) as ex:
|
||||||
|
raise HomeAssistantError(
|
||||||
|
translation_domain=DOMAIN,
|
||||||
|
translation_key="calibration_error",
|
||||||
|
) from ex
|
||||||
|
@ -25,9 +25,7 @@ rules:
|
|||||||
config-entry-unloading: done
|
config-entry-unloading: done
|
||||||
log-when-unavailable: done
|
log-when-unavailable: done
|
||||||
entity-unavailable: done
|
entity-unavailable: done
|
||||||
action-exceptions:
|
action-exceptions: done
|
||||||
status: exempt
|
|
||||||
comment: No custom action.
|
|
||||||
reauthentication-flow: todo
|
reauthentication-flow: todo
|
||||||
parallel-updates: done
|
parallel-updates: done
|
||||||
test-coverage: todo
|
test-coverage: todo
|
||||||
|
@ -54,6 +54,12 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"exceptions": {
|
"exceptions": {
|
||||||
|
"calibration_error": {
|
||||||
|
"message": "Error while sending the calibration request to the device."
|
||||||
|
},
|
||||||
|
"touchgo_error": {
|
||||||
|
"message": "Error while sending the request setting Touch&Go to {state} to the device."
|
||||||
|
},
|
||||||
"update_error": {
|
"update_error": {
|
||||||
"message": "Error while updating data from the API."
|
"message": "Error while updating data from the API."
|
||||||
}
|
}
|
||||||
|
@ -4,16 +4,25 @@ from __future__ import annotations
|
|||||||
|
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
|
from goslideapi.goslideapi import (
|
||||||
|
AuthenticationFailed,
|
||||||
|
ClientConnectionError,
|
||||||
|
ClientTimeoutError,
|
||||||
|
DigestAuthCalcError,
|
||||||
|
)
|
||||||
|
|
||||||
from homeassistant.components.switch import SwitchDeviceClass, SwitchEntity
|
from homeassistant.components.switch import SwitchDeviceClass, SwitchEntity
|
||||||
from homeassistant.const import EntityCategory
|
from homeassistant.const import EntityCategory
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
|
from homeassistant.exceptions import HomeAssistantError
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
|
|
||||||
from . import SlideConfigEntry
|
from . import SlideConfigEntry
|
||||||
|
from .const import DOMAIN
|
||||||
from .coordinator import SlideCoordinator
|
from .coordinator import SlideCoordinator
|
||||||
from .entity import SlideEntity
|
from .entity import SlideEntity
|
||||||
|
|
||||||
PARALLEL_UPDATES = 0
|
PARALLEL_UPDATES = 1
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(
|
async def async_setup_entry(
|
||||||
@ -47,10 +56,38 @@ class SlideSwitch(SlideEntity, SwitchEntity):
|
|||||||
|
|
||||||
async def async_turn_off(self, **kwargs: Any) -> None:
|
async def async_turn_off(self, **kwargs: Any) -> None:
|
||||||
"""Turn off touchgo."""
|
"""Turn off touchgo."""
|
||||||
await self.coordinator.slide.slide_set_touchgo(self.coordinator.host, False)
|
try:
|
||||||
|
await self.coordinator.slide.slide_set_touchgo(self.coordinator.host, False)
|
||||||
|
except (
|
||||||
|
ClientConnectionError,
|
||||||
|
AuthenticationFailed,
|
||||||
|
ClientTimeoutError,
|
||||||
|
DigestAuthCalcError,
|
||||||
|
) as ex:
|
||||||
|
raise HomeAssistantError(
|
||||||
|
translation_domain=DOMAIN,
|
||||||
|
translation_key="touchgo_error",
|
||||||
|
translation_placeholders={
|
||||||
|
"state": "off",
|
||||||
|
},
|
||||||
|
) from ex
|
||||||
await self.coordinator.async_request_refresh()
|
await self.coordinator.async_request_refresh()
|
||||||
|
|
||||||
async def async_turn_on(self, **kwargs: Any) -> None:
|
async def async_turn_on(self, **kwargs: Any) -> None:
|
||||||
"""Turn on touchgo."""
|
"""Turn on touchgo."""
|
||||||
await self.coordinator.slide.slide_set_touchgo(self.coordinator.host, True)
|
try:
|
||||||
|
await self.coordinator.slide.slide_set_touchgo(self.coordinator.host, True)
|
||||||
|
except (
|
||||||
|
ClientConnectionError,
|
||||||
|
AuthenticationFailed,
|
||||||
|
ClientTimeoutError,
|
||||||
|
DigestAuthCalcError,
|
||||||
|
) as ex:
|
||||||
|
raise HomeAssistantError(
|
||||||
|
translation_domain=DOMAIN,
|
||||||
|
translation_key="touchgo_error",
|
||||||
|
translation_placeholders={
|
||||||
|
"state": "on",
|
||||||
|
},
|
||||||
|
) from ex
|
||||||
await self.coordinator.async_request_refresh()
|
await self.coordinator.async_request_refresh()
|
||||||
|
@ -2,11 +2,19 @@
|
|||||||
|
|
||||||
from unittest.mock import AsyncMock
|
from unittest.mock import AsyncMock
|
||||||
|
|
||||||
|
from goslideapi.goslideapi import (
|
||||||
|
AuthenticationFailed,
|
||||||
|
ClientConnectionError,
|
||||||
|
ClientTimeoutError,
|
||||||
|
DigestAuthCalcError,
|
||||||
|
)
|
||||||
|
import pytest
|
||||||
from syrupy import SnapshotAssertion
|
from syrupy import SnapshotAssertion
|
||||||
|
|
||||||
from homeassistant.components.button import DOMAIN as BUTTON_DOMAIN, SERVICE_PRESS
|
from homeassistant.components.button import DOMAIN as BUTTON_DOMAIN, SERVICE_PRESS
|
||||||
from homeassistant.const import ATTR_ENTITY_ID, Platform
|
from homeassistant.const import ATTR_ENTITY_ID, Platform
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
|
from homeassistant.exceptions import HomeAssistantError
|
||||||
from homeassistant.helpers import entity_registry as er
|
from homeassistant.helpers import entity_registry as er
|
||||||
|
|
||||||
from . import setup_platform
|
from . import setup_platform
|
||||||
@ -44,3 +52,37 @@ async def test_pressing_button(
|
|||||||
blocking=True,
|
blocking=True,
|
||||||
)
|
)
|
||||||
mock_slide_api.slide_calibrate.assert_called_once()
|
mock_slide_api.slide_calibrate.assert_called_once()
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
("exception"),
|
||||||
|
[
|
||||||
|
ClientConnectionError,
|
||||||
|
ClientTimeoutError,
|
||||||
|
AuthenticationFailed,
|
||||||
|
DigestAuthCalcError,
|
||||||
|
],
|
||||||
|
)
|
||||||
|
async def test_pressing_button_exception(
|
||||||
|
hass: HomeAssistant,
|
||||||
|
exception: Exception,
|
||||||
|
mock_slide_api: AsyncMock,
|
||||||
|
mock_config_entry: MockConfigEntry,
|
||||||
|
) -> None:
|
||||||
|
"""Test pressing button."""
|
||||||
|
await setup_platform(hass, mock_config_entry, [Platform.BUTTON])
|
||||||
|
|
||||||
|
mock_slide_api.slide_calibrate.side_effect = exception
|
||||||
|
|
||||||
|
with pytest.raises(
|
||||||
|
HomeAssistantError,
|
||||||
|
match="Error while sending the calibration request to the device",
|
||||||
|
):
|
||||||
|
await hass.services.async_call(
|
||||||
|
BUTTON_DOMAIN,
|
||||||
|
SERVICE_PRESS,
|
||||||
|
{
|
||||||
|
ATTR_ENTITY_ID: "button.slide_bedroom_calibrate",
|
||||||
|
},
|
||||||
|
blocking=True,
|
||||||
|
)
|
||||||
|
@ -2,6 +2,12 @@
|
|||||||
|
|
||||||
from unittest.mock import AsyncMock
|
from unittest.mock import AsyncMock
|
||||||
|
|
||||||
|
from goslideapi.goslideapi import (
|
||||||
|
AuthenticationFailed,
|
||||||
|
ClientConnectionError,
|
||||||
|
ClientTimeoutError,
|
||||||
|
DigestAuthCalcError,
|
||||||
|
)
|
||||||
import pytest
|
import pytest
|
||||||
from syrupy import SnapshotAssertion
|
from syrupy import SnapshotAssertion
|
||||||
|
|
||||||
@ -13,6 +19,7 @@ from homeassistant.components.switch import (
|
|||||||
)
|
)
|
||||||
from homeassistant.const import ATTR_ENTITY_ID, Platform
|
from homeassistant.const import ATTR_ENTITY_ID, Platform
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
|
from homeassistant.exceptions import HomeAssistantError
|
||||||
from homeassistant.helpers import entity_registry as er
|
from homeassistant.helpers import entity_registry as er
|
||||||
|
|
||||||
from . import setup_platform
|
from . import setup_platform
|
||||||
@ -59,3 +66,38 @@ async def test_services(
|
|||||||
blocking=True,
|
blocking=True,
|
||||||
)
|
)
|
||||||
mock_slide_api.slide_set_touchgo.assert_called_once()
|
mock_slide_api.slide_set_touchgo.assert_called_once()
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
("exception", "service"),
|
||||||
|
[
|
||||||
|
(ClientConnectionError, SERVICE_TURN_OFF),
|
||||||
|
(ClientTimeoutError, SERVICE_TURN_ON),
|
||||||
|
(AuthenticationFailed, SERVICE_TURN_OFF),
|
||||||
|
(DigestAuthCalcError, SERVICE_TURN_ON),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
async def test_service_exception(
|
||||||
|
hass: HomeAssistant,
|
||||||
|
exception: Exception,
|
||||||
|
service: str,
|
||||||
|
mock_slide_api: AsyncMock,
|
||||||
|
mock_config_entry: MockConfigEntry,
|
||||||
|
) -> None:
|
||||||
|
"""Test pressing button."""
|
||||||
|
await setup_platform(hass, mock_config_entry, [Platform.SWITCH])
|
||||||
|
|
||||||
|
mock_slide_api.slide_set_touchgo.side_effect = exception
|
||||||
|
|
||||||
|
with pytest.raises(
|
||||||
|
HomeAssistantError,
|
||||||
|
match=f"Error while sending the request setting Touch&Go to {service[5:]} to the device",
|
||||||
|
):
|
||||||
|
await hass.services.async_call(
|
||||||
|
SWITCH_DOMAIN,
|
||||||
|
service,
|
||||||
|
{
|
||||||
|
ATTR_ENTITY_ID: "switch.slide_bedroom_touchgo",
|
||||||
|
},
|
||||||
|
blocking=True,
|
||||||
|
)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user