Skip to content

Commit

Permalink
Add exceptions and translations for slide_local (#133490)
Browse files Browse the repository at this point in the history
  • Loading branch information
dontinelli authored Dec 18, 2024
1 parent c9f1829 commit d6c201d
Show file tree
Hide file tree
Showing 6 changed files with 153 additions and 8 deletions.
24 changes: 22 additions & 2 deletions homeassistant/components/slide_local/button.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,25 @@

from __future__ import annotations

from goslideapi.goslideapi import (
AuthenticationFailed,
ClientConnectionError,
ClientTimeoutError,
DigestAuthCalcError,
)

from homeassistant.components.button import ButtonEntity
from homeassistant.const import EntityCategory
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers.entity_platform import AddEntitiesCallback

from . import SlideConfigEntry
from .const import DOMAIN
from .coordinator import SlideCoordinator
from .entity import SlideEntity

PARALLEL_UPDATES = 0
PARALLEL_UPDATES = 1


async def async_setup_entry(
Expand Down Expand Up @@ -39,4 +48,15 @@ def __init__(self, coordinator: SlideCoordinator) -> None:

async def async_press(self) -> None:
"""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
4 changes: 1 addition & 3 deletions homeassistant/components/slide_local/quality_scale.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@ rules:
config-entry-unloading: done
log-when-unavailable: done
entity-unavailable: done
action-exceptions:
status: exempt
comment: No custom action.
action-exceptions: done
reauthentication-flow: todo
parallel-updates: done
test-coverage: todo
Expand Down
6 changes: 6 additions & 0 deletions homeassistant/components/slide_local/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@
}
},
"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": {
"message": "Error while updating data from the API."
}
Expand Down
43 changes: 40 additions & 3 deletions homeassistant/components/slide_local/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,25 @@

from typing import Any

from goslideapi.goslideapi import (
AuthenticationFailed,
ClientConnectionError,
ClientTimeoutError,
DigestAuthCalcError,
)

from homeassistant.components.switch import SwitchDeviceClass, SwitchEntity
from homeassistant.const import EntityCategory
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers.entity_platform import AddEntitiesCallback

from . import SlideConfigEntry
from .const import DOMAIN
from .coordinator import SlideCoordinator
from .entity import SlideEntity

PARALLEL_UPDATES = 0
PARALLEL_UPDATES = 1


async def async_setup_entry(
Expand Down Expand Up @@ -47,10 +56,38 @@ def is_on(self) -> bool:

async def async_turn_off(self, **kwargs: Any) -> None:
"""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()

async def async_turn_on(self, **kwargs: Any) -> None:
"""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()
42 changes: 42 additions & 0 deletions tests/components/slide_local/test_button.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,19 @@

from unittest.mock import AsyncMock

from goslideapi.goslideapi import (
AuthenticationFailed,
ClientConnectionError,
ClientTimeoutError,
DigestAuthCalcError,
)
import pytest
from syrupy import SnapshotAssertion

from homeassistant.components.button import DOMAIN as BUTTON_DOMAIN, SERVICE_PRESS
from homeassistant.const import ATTR_ENTITY_ID, Platform
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers import entity_registry as er

from . import setup_platform
Expand Down Expand Up @@ -44,3 +52,37 @@ async def test_pressing_button(
blocking=True,
)
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,
)
42 changes: 42 additions & 0 deletions tests/components/slide_local/test_switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

from unittest.mock import AsyncMock

from goslideapi.goslideapi import (
AuthenticationFailed,
ClientConnectionError,
ClientTimeoutError,
DigestAuthCalcError,
)
import pytest
from syrupy import SnapshotAssertion

Expand All @@ -13,6 +19,7 @@
)
from homeassistant.const import ATTR_ENTITY_ID, Platform
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers import entity_registry as er

from . import setup_platform
Expand Down Expand Up @@ -59,3 +66,38 @@ async def test_services(
blocking=True,
)
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,
)

0 comments on commit d6c201d

Please sign in to comment.