|
4 | 4 |
|
5 | 5 | from typing import Any |
6 | 6 |
|
7 | | -from pyuptimerobot import ( |
8 | | - UptimeRobotAuthenticationException, |
9 | | - UptimeRobotException, |
10 | | - UptimeRobotMonitor, |
11 | | -) |
| 7 | +from pyuptimerobot import UptimeRobotMonitor |
12 | 8 |
|
13 | 9 | from homeassistant.components.switch import ( |
14 | 10 | SwitchDeviceClass, |
15 | 11 | SwitchEntity, |
16 | 12 | SwitchEntityDescription, |
17 | 13 | ) |
18 | 14 | from homeassistant.core import HomeAssistant |
19 | | -from homeassistant.exceptions import HomeAssistantError |
20 | 15 | from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback |
21 | 16 |
|
22 | | -from .const import DOMAIN, STATUS_DOWN, STATUS_UP |
| 17 | +from .const import STATUS_UP |
23 | 18 | from .coordinator import UptimeRobotConfigEntry |
24 | 19 | from .entity import UptimeRobotEntity |
25 | | -from .utils import new_device_listener |
| 20 | +from .utils import new_device_listener, uptimerobot_api_call |
26 | 21 |
|
27 | 22 | # Limit the number of parallel updates to 1 |
28 | 23 | PARALLEL_UPDATES = 1 |
@@ -65,26 +60,14 @@ def is_on(self) -> bool: |
65 | 60 | """Return True if the entity is on.""" |
66 | 61 | return bool(self._monitor.status == STATUS_UP) |
67 | 62 |
|
68 | | - async def _async_edit_monitor(self, **kwargs: Any) -> None: |
69 | | - """Edit monitor status.""" |
70 | | - try: |
71 | | - await self.api.async_edit_monitor(**kwargs) |
72 | | - except UptimeRobotAuthenticationException: |
73 | | - self.coordinator.config_entry.async_start_reauth(self.hass) |
74 | | - return |
75 | | - except UptimeRobotException as exception: |
76 | | - raise HomeAssistantError( |
77 | | - translation_domain=DOMAIN, |
78 | | - translation_key="api_exception", |
79 | | - translation_placeholders={"error": "Generic UptimeRobot exception"}, |
80 | | - ) from exception |
81 | | - |
82 | | - await self.coordinator.async_request_refresh() |
83 | | - |
| 63 | + @uptimerobot_api_call |
84 | 64 | async def async_turn_off(self, **kwargs: Any) -> None: |
85 | 65 | """Turn off switch.""" |
86 | | - await self._async_edit_monitor(monitor_id=self._monitor.id, status=STATUS_DOWN) |
| 66 | + await self.api.async_pause_monitor(monitor_id=self._monitor.id) |
| 67 | + await self.coordinator.async_request_refresh() |
87 | 68 |
|
| 69 | + @uptimerobot_api_call |
88 | 70 | async def async_turn_on(self, **kwargs: Any) -> None: |
89 | 71 | """Turn on switch.""" |
90 | | - await self._async_edit_monitor(monitor_id=self._monitor.id, status=STATUS_UP) |
| 72 | + await self.api.async_start_monitor(monitor_id=self._monitor.id) |
| 73 | + await self.coordinator.async_request_refresh() |
0 commit comments