diff --git a/custom_components/wyzeapi/alarm_control_panel.py b/custom_components/wyzeapi/alarm_control_panel.py index 713599c9..04a18f42 100644 --- a/custom_components/wyzeapi/alarm_control_panel.py +++ b/custom_components/wyzeapi/alarm_control_panel.py @@ -5,6 +5,7 @@ import logging from datetime import timedelta from typing import Optional, Callable, List, Any +from aiohttp.client_exceptions import ClientConnectionError from homeassistant.components.alarm_control_panel import ( AlarmControlPanelEntity, @@ -13,8 +14,10 @@ from homeassistant.config_entries import ConfigEntry from homeassistant.const import ATTR_ATTRIBUTION from homeassistant.core import HomeAssistant +from homeassistant.exceptions import HomeAssistantError from wyzeapy import Wyzeapy, HMSService from wyzeapy.services.hms_service import HMSMode +from wyzeapy.exceptions import AccessTokenError, ParameterError, UnknownApiError from .token_manager import token_exception_handler from .const import DOMAIN, CONF_CLIENT @@ -63,33 +66,42 @@ def alarm_arm_vacation(self, code: Optional[str] = None) -> None: def state(self): return self._state - def alarm_disarm(self, code: Optional[str] = None) -> None: - raise NotImplementedError - - def alarm_arm_home(self, code: Optional[str] = None) -> None: - raise NotImplementedError - - def alarm_arm_away(self, code: Optional[str] = None) -> None: - raise NotImplementedError - @token_exception_handler async def async_alarm_disarm(self, code=None) -> None: """Send disarm command.""" - await self._hms_service.set_mode(HMSMode.DISARMED) - self._state = "disarmed" - self._server_out_of_sync = True + try: + await self._hms_service.set_mode(HMSMode.DISARMED) + except (AccessTokenError, ParameterError, UnknownApiError) as err: + raise HomeAssistantError(f"Wyze returned an error: {err.args}") from err + except ClientConnectionError as err: + raise HomeAssistantError(err) from err + else: + self._state = "disarmed" + self._server_out_of_sync = True @token_exception_handler async def async_alarm_arm_home(self, code=None): - await self._hms_service.set_mode(HMSMode.HOME) - self._state = "armed_home" - self._server_out_of_sync = True + try: + await self._hms_service.set_mode(HMSMode.HOME) + except (AccessTokenError, ParameterError, UnknownApiError) as err: + raise HomeAssistantError(f"Wyze returned an error: {err.args}") from err + except ClientConnectionError as err: + raise HomeAssistantError(err) from err + else: + self._state = "armed_home" + self._server_out_of_sync = True @token_exception_handler async def async_alarm_arm_away(self, code=None): - await self._hms_service.set_mode(HMSMode.AWAY) - self._state = "armed_away" - self._server_out_of_sync = True + try: + await self._hms_service.set_mode(HMSMode.AWAY) + except (AccessTokenError, ParameterError, UnknownApiError) as err: + raise HomeAssistantError(f"Wyze returned an error: {err.args}") from err + except ClientConnectionError as err: + raise HomeAssistantError(err) from err + else: + self._state = "armed_away" + self._server_out_of_sync = True def alarm_arm_night(self, code=None): raise NotImplementedError diff --git a/custom_components/wyzeapi/climate.py b/custom_components/wyzeapi/climate.py index 88b36376..2a49abc0 100644 --- a/custom_components/wyzeapi/climate.py +++ b/custom_components/wyzeapi/climate.py @@ -3,6 +3,7 @@ # Import the device class from the component that you want to support from datetime import timedelta from typing import List, Optional, Callable, Any +from aiohttp.client_exceptions import ClientConnectionError from homeassistant.components.climate import ( ClimateEntity, @@ -20,8 +21,10 @@ from homeassistant.config_entries import ConfigEntry from homeassistant.const import ATTR_ATTRIBUTION, UnitOfTemperature from homeassistant.core import HomeAssistant, callback +from homeassistant.exceptions import HomeAssistantError from homeassistant.helpers import device_registry as dr from wyzeapy import Wyzeapy, ThermostatService +from wyzeapy.exceptions import AccessTokenError, ParameterError, UnknownApiError from wyzeapy.services.thermostat_service import Thermostat, TemperatureUnit, Preset, FanMode, HVACState, HVACMode as WyzeHVACMode from .token_manager import token_exception_handler @@ -190,66 +193,86 @@ async def async_set_temperature(self, **kwargs) -> None: target_temp_low = kwargs['target_temp_low'] target_temp_high = kwargs['target_temp_high'] - if target_temp_low != self._thermostat.heat_set_point: - await self._thermostat_service.set_heat_point(self._thermostat, int(target_temp_low)) - self._thermostat.heat_set_point = int(target_temp_low) - if target_temp_high != self._thermostat.cool_set_point: - await self._thermostat_service.set_cool_point(self._thermostat, int(target_temp_high)) - self._thermostat.cool_set_point = int(target_temp_high) - - self._server_out_of_sync = True - self.async_schedule_update_ha_state() + try: + if target_temp_low != self._thermostat.heat_set_point: + await self._thermostat_service.set_heat_point(self._thermostat, int(target_temp_low)) + self._thermostat.heat_set_point = int(target_temp_low) + if target_temp_high != self._thermostat.cool_set_point: + await self._thermostat_service.set_cool_point(self._thermostat, int(target_temp_high)) + self._thermostat.cool_set_point = int(target_temp_high) + except (AccessTokenError, ParameterError, UnknownApiError) as err: + raise HomeAssistantError(f"Wyze returned an error: {err.args}") from err + except ClientConnectionError as err: + raise HomeAssistantError(err) from err + else: + self._server_out_of_sync = True + self.async_schedule_update_ha_state() async def async_set_humidity(self, humidity: int) -> None: raise NotImplementedError @token_exception_handler async def async_set_fan_mode(self, fan_mode: str) -> None: - if fan_mode == FAN_ON: - await self._thermostat_service.set_fan_mode(self._thermostat, FanMode.ON) - self._thermostat.fan_mode = FanMode.ON - elif fan_mode == FAN_AUTO: - await self._thermostat_service.set_fan_mode(self._thermostat, FanMode.AUTO) - self._thermostat.fan_mode = FanMode.AUTO - - self._server_out_of_sync = True - self.async_schedule_update_ha_state() + try: + if fan_mode == FAN_ON: + await self._thermostat_service.set_fan_mode(self._thermostat, FanMode.ON) + self._thermostat.fan_mode = FanMode.ON + elif fan_mode == FAN_AUTO: + await self._thermostat_service.set_fan_mode(self._thermostat, FanMode.AUTO) + self._thermostat.fan_mode = FanMode.AUTO + except (AccessTokenError, ParameterError, UnknownApiError) as err: + raise HomeAssistantError(f"Wyze returned an error: {err.args}") from err + except ClientConnectionError as err: + raise HomeAssistantError(err) from err + else: + self._server_out_of_sync = True + self.async_schedule_update_ha_state() @token_exception_handler async def async_set_hvac_mode(self, hvac_mode: str) -> None: - if hvac_mode == HVACMode.OFF: - await self._thermostat_service.set_hvac_mode(self._thermostat, WyzeHVACMode.OFF) - self._thermostat.hvac_mode = HVACMode.OFF - elif hvac_mode == HVACMode.HEAT: - await self._thermostat_service.set_hvac_mode(self._thermostat, WyzeHVACMode.HEAT) - self._thermostat.hvac_mode = HVACMode.HEAT - elif hvac_mode == HVACMode.COOL: - await self._thermostat_service.set_hvac_mode(self._thermostat, WyzeHVACMode.COOL) - self._thermostat.hvac_mode = HVACMode.COOL - elif hvac_mode == HVACMode.AUTO: - await self._thermostat_service.set_hvac_mode(self._thermostat, WyzeHVACMode.AUTO) - self._thermostat.hvac_mode = HVACMode.AUTO - - self._server_out_of_sync = True - self.async_schedule_update_ha_state() + try: + if hvac_mode == HVACMode.OFF: + await self._thermostat_service.set_hvac_mode(self._thermostat, WyzeHVACMode.OFF) + self._thermostat.hvac_mode = HVACMode.OFF + elif hvac_mode == HVACMode.HEAT: + await self._thermostat_service.set_hvac_mode(self._thermostat, WyzeHVACMode.HEAT) + self._thermostat.hvac_mode = HVACMode.HEAT + elif hvac_mode == HVACMode.COOL: + await self._thermostat_service.set_hvac_mode(self._thermostat, WyzeHVACMode.COOL) + self._thermostat.hvac_mode = HVACMode.COOL + elif hvac_mode == HVACMode.AUTO: + await self._thermostat_service.set_hvac_mode(self._thermostat, WyzeHVACMode.AUTO) + self._thermostat.hvac_mode = HVACMode.AUTO + except (AccessTokenError, ParameterError, UnknownApiError) as err: + raise HomeAssistantError(f"Wyze returned an error: {err.args}") from err + except ClientConnectionError as err: + raise HomeAssistantError(err) from err + else: + self._server_out_of_sync = True + self.async_schedule_update_ha_state() async def async_set_swing_mode(self, swing_mode: str) -> None: raise NotImplementedError @token_exception_handler async def async_set_preset_mode(self, preset_mode: str) -> None: - if preset_mode == PRESET_SLEEP: - await self._thermostat_service.set_preset(self._thermostat, Preset.SLEEP) - self._thermostat.preset = Preset.SLEEP - elif preset_mode == PRESET_AWAY: - await self._thermostat_service.set_preset(self._thermostat, Preset.AWAY) - self._thermostat.preset = Preset.AWAY - elif preset_mode == PRESET_HOME: - await self._thermostat_service.set_preset(self._thermostat, Preset.HOME) - self._thermostat.preset = Preset.HOME - - self._server_out_of_sync = True - self.async_schedule_update_ha_state() + try: + if preset_mode == PRESET_SLEEP: + await self._thermostat_service.set_preset(self._thermostat, Preset.SLEEP) + self._thermostat.preset = Preset.SLEEP + elif preset_mode == PRESET_AWAY: + await self._thermostat_service.set_preset(self._thermostat, Preset.AWAY) + self._thermostat.preset = Preset.AWAY + elif preset_mode == PRESET_HOME: + await self._thermostat_service.set_preset(self._thermostat, Preset.HOME) + self._thermostat.preset = Preset.HOME + except (AccessTokenError, ParameterError, UnknownApiError) as err: + raise HomeAssistantError(f"Wyze returned an error: {err.args}") from err + except ClientConnectionError as err: + raise HomeAssistantError(err) from err + else: + self._server_out_of_sync = True + self.async_schedule_update_ha_state() async def async_turn_aux_heat_on(self) -> None: raise NotImplementedError diff --git a/custom_components/wyzeapi/light.py b/custom_components/wyzeapi/light.py index f9197e3f..959f7305 100644 --- a/custom_components/wyzeapi/light.py +++ b/custom_components/wyzeapi/light.py @@ -1,11 +1,11 @@ #!/usr/bin/python3 """Platform for light integration.""" -import asyncio import logging # Import the device class from the component that you want to support from datetime import timedelta from typing import Any, Callable, List +from aiohttp.client_exceptions import ClientConnectionError import homeassistant.util.color as color_util from homeassistant.components.light import ( @@ -21,7 +21,9 @@ from homeassistant.const import ATTR_ATTRIBUTION from homeassistant.core import HomeAssistant, callback from homeassistant.helpers import device_registry as dr +from homeassistant.exceptions import HomeAssistantError from wyzeapy import Wyzeapy, BulbService, CameraService +from wyzeapy.exceptions import AccessTokenError, ParameterError, UnknownApiError from wyzeapy.services.bulb_service import Bulb from wyzeapy.types import DeviceTypes, PropertyIDs from wyzeapy.utils import create_pid_pair @@ -100,12 +102,6 @@ def __init__(self, bulb_service: BulbService, bulb: Bulb, config_entry): self._bulb_service = bulb_service - def turn_on(self, **kwargs: Any) -> None: - raise NotImplementedError - - def turn_off(self, **kwargs: Any) -> None: - raise NotImplementedError - @property def device_info(self): return { @@ -205,22 +201,30 @@ async def async_turn_on(self, **kwargs: Any) -> None: self._bulb.effects = "3" _LOGGER.debug("Turning on light") - loop = asyncio.get_event_loop() - loop.create_task(self._bulb_service.turn_on(self._bulb, self._local_control, options)) - - self._bulb.on = True - self._just_updated = True - self.async_schedule_update_ha_state() + try: + await self._bulb_service.turn_on(self._bulb, self._local_control, options) + except (AccessTokenError, ParameterError, UnknownApiError) as err: + raise HomeAssistantError(f"Wyze returned an error: {err.args}") from err + except ClientConnectionError as err: + raise HomeAssistantError(err) from err + else: + self._bulb.on = True + self._just_updated = True + self.async_schedule_update_ha_state() @token_exception_handler async def async_turn_off(self, **kwargs: Any) -> None: self._local_control = self._config_entry.options.get(BULB_LOCAL_CONTROL) - loop = asyncio.get_event_loop() - loop.create_task(self._bulb_service.turn_off(self._bulb, self._local_control)) - - self._bulb.on = False - self._just_updated = True - self.async_schedule_update_ha_state() + try: + await self._bulb_service.turn_off(self._bulb, self._local_control) + except (AccessTokenError, ParameterError, UnknownApiError) as err: + raise HomeAssistantError(f"Wyze returned an error: {err.args}") from err + except ClientConnectionError as err: + raise HomeAssistantError(err) from err + else: + self._bulb.on = False + self._just_updated = True + self.async_schedule_update_ha_state() @property def supported_color_modes(self): @@ -380,20 +384,30 @@ def __init__(self, camera: Camera, camera_service: CameraService, light_type: st @token_exception_handler async def async_turn_on(self, **kwargs) -> None: """Turn the floodlight on.""" - await self._service.floodlight_on(self._device) - - self._device.floodlight = True - self._just_updated = True - self.async_schedule_update_ha_state() + try: + await self._service.floodlight_on(self._device) + except (AccessTokenError, ParameterError, UnknownApiError) as err: + raise HomeAssistantError(f"Wyze returned an error: {err.args}") from err + except ClientConnectionError as err: + raise HomeAssistantError(err) from err + else: + self._is_on = True + self._just_updated = True + self.async_schedule_update_ha_state() @token_exception_handler async def async_turn_off(self, **kwargs): """Turn the floodlight off.""" - await self._service.floodlight_off(self._device) - - self._device.floodlight = False - self._just_updated = True - self.async_schedule_update_ha_state() + try: + await self._service.floodlight_off(self._device) + except (AccessTokenError, ParameterError, UnknownApiError) as err: + raise HomeAssistantError(f"Wyze returned an error: {err.args}") from err + except ClientConnectionError as err: + raise HomeAssistantError(err) from err + else: + self._is_on = False + self._just_updated = True + self.async_schedule_update_ha_state() @property def should_poll(self) -> bool: diff --git a/custom_components/wyzeapi/lock.py b/custom_components/wyzeapi/lock.py index 32588431..72b44cb4 100644 --- a/custom_components/wyzeapi/lock.py +++ b/custom_components/wyzeapi/lock.py @@ -5,10 +5,12 @@ from datetime import timedelta import logging from typing import Any, Callable, List +from aiohttp.client_exceptions import ClientConnectionError from wyzeapy import LockService, Wyzeapy from wyzeapy.services.lock_service import Lock from wyzeapy.types import DeviceTypes +from wyzeapy.exceptions import AccessTokenError, ParameterError, UnknownApiError import homeassistant.components.lock from homeassistant.config_entries import ConfigEntry @@ -16,6 +18,7 @@ from homeassistant.core import HomeAssistant, callback from homeassistant.helpers.dispatcher import async_dispatcher_send from homeassistant.helpers import device_registry as dr +from homeassistant.exceptions import HomeAssistantError from .const import CONF_CLIENT, DOMAIN, LOCK_UPDATED from .token_manager import token_exception_handler @@ -92,17 +95,27 @@ def should_poll(self) -> bool: @token_exception_handler async def async_lock(self, **kwargs): _LOGGER.debug("Turning on lock") - await self._lock_service.lock(self._lock) - - self._lock.unlocked = False - self.async_schedule_update_ha_state() + try: + await self._lock_service.lock(self._lock) + except (AccessTokenError, ParameterError, UnknownApiError) as err: + raise HomeAssistantError(f"Wyze returned an error: {err.args}") from err + except ClientConnectionError as err: + raise HomeAssistantError(err) from err + else: + self._lock.unlocked = False + self.async_schedule_update_ha_state() @token_exception_handler async def async_unlock(self, **kwargs): - await self._lock_service.unlock(self._lock) - - self._lock.unlocked = True - self.async_schedule_update_ha_state() + try: + await self._lock_service.unlock(self._lock) + except (AccessTokenError, ParameterError, UnknownApiError) as err: + raise HomeAssistantError(f"Wyze returned an error: {err.args}") from err + except ClientConnectionError as err: + raise HomeAssistantError(err) from err + else: + self._lock.unlocked = True + self.async_schedule_update_ha_state() @property def is_locked(self): diff --git a/custom_components/wyzeapi/siren.py b/custom_components/wyzeapi/siren.py index 200daa83..ce3c941c 100644 --- a/custom_components/wyzeapi/siren.py +++ b/custom_components/wyzeapi/siren.py @@ -3,9 +3,11 @@ """Platform for siren integration.""" import logging from typing import Any, Callable +from aiohttp.client_exceptions import ClientConnectionError from wyzeapy import CameraService, Wyzeapy from wyzeapy.services.camera_service import Camera +from wyzeapy.exceptions import AccessTokenError, ParameterError, UnknownApiError from homeassistant.components.siren import ( SirenEntity, @@ -14,6 +16,7 @@ from homeassistant.config_entries import ConfigEntry from homeassistant.const import ATTR_ATTRIBUTION from homeassistant.core import HomeAssistant, callback +from homeassistant.exceptions import HomeAssistantError from homeassistant.helpers.dispatcher import async_dispatcher_connect from .const import CAMERA_UPDATED, CONF_CLIENT, DOMAIN @@ -63,20 +66,30 @@ def __init__(self, camera: Camera, camera_service: CameraService) -> None: @token_exception_handler async def async_turn_on(self, **kwargs) -> None: """Turn the siren on.""" - await self._service.siren_on(self._device) - - self._device.siren = True - self._just_updated = True - self.async_schedule_update_ha_state() + try: + await self._service.siren_on(self._device) + except (AccessTokenError, ParameterError, UnknownApiError) as err: + raise HomeAssistantError(f"Wyze returned an error: {err.args}") from err + except ClientConnectionError as err: + raise HomeAssistantError(err) from err + else: + self._device.siren = True + self._just_updated = True + self.async_schedule_update_ha_state() @token_exception_handler async def async_turn_off(self, **kwargs): """Turn the siren off.""" - await self._service.siren_off(self._device) - - self._device.siren = False - self._just_updated = True - self.async_schedule_update_ha_state() + try: + await self._service.siren_off(self._device) + except (AccessTokenError, ParameterError, UnknownApiError) as err: + raise HomeAssistantError(f"Wyze returned an error: {err.args}") from err + except ClientConnectionError as err: + raise HomeAssistantError(err) from err + else: + self._device.siren = False + self._just_updated = True + self.async_schedule_update_ha_state() @property def should_poll(self) -> bool: diff --git a/custom_components/wyzeapi/switch.py b/custom_components/wyzeapi/switch.py index e4cfa01a..f12ec160 100644 --- a/custom_components/wyzeapi/switch.py +++ b/custom_components/wyzeapi/switch.py @@ -3,16 +3,18 @@ """Platform for switch integration.""" import logging from datetime import timedelta -# Import the device class from the component that you want to support from typing import Any, Callable, List, Union +from aiohttp.client_exceptions import ClientConnectionError from homeassistant.components.switch import SwitchEntity from homeassistant.config_entries import ConfigEntry from homeassistant.const import ATTR_ATTRIBUTION from homeassistant.core import HomeAssistant, callback +from homeassistant.exceptions import HomeAssistantError from homeassistant.helpers.dispatcher import async_dispatcher_send, async_dispatcher_connect from homeassistant.helpers import device_registry as dr from wyzeapy import CameraService, SwitchService, WallSwitchService, Wyzeapy, BulbService +from wyzeapy.exceptions import AccessTokenError, ParameterError, UnknownApiError from wyzeapy.services.camera_service import Camera from wyzeapy.services.switch_service import Switch from wyzeapy.services.bulb_service import Bulb @@ -118,18 +120,28 @@ def should_poll(self) -> bool: return False async def async_turn_on(self, **kwargs: Any) -> None: - await self._client.enable_notifications() - - self._is_on = True - self._just_updated = True - self.async_schedule_update_ha_state() + try: + await self._client.enable_notifications() + except (AccessTokenError, ParameterError, UnknownApiError) as err: + raise HomeAssistantError(f"Wyze returned an error: {err.args}") from err + except ClientConnectionError as err: + raise HomeAssistantError(err) from err + else: + self._is_on = True + self._just_updated = True + self.async_schedule_update_ha_state() async def async_turn_off(self, **kwargs: Any) -> None: - await self._client.disable_notifications() - - self._is_on = False - self._just_updated = True - self.async_schedule_update_ha_state() + try: + await self._client.disable_notifications() + except (AccessTokenError, ParameterError, UnknownApiError) as err: + raise HomeAssistantError(f"Wyze returned an error: {err.args}") from err + except ClientConnectionError as err: + raise HomeAssistantError(err) from err + else: + self._is_on = False + self._just_updated = True + self.async_schedule_update_ha_state() @property def name(self): @@ -155,12 +167,6 @@ async def async_update(self): class WyzeSwitch(SwitchEntity): """Representation of a Wyze Switch.""" - def turn_on(self, **kwargs: Any) -> None: - pass - - def turn_off(self, **kwargs: Any) -> None: - pass - _on: bool _available: bool _just_updated = False @@ -199,19 +205,29 @@ def should_poll(self) -> bool: @token_exception_handler async def async_turn_on(self, **kwargs: Any) -> None: - await self._service.turn_on(self._device) - - self._device.on = True - self._just_updated = True - self.async_schedule_update_ha_state() + try: + await self._service.turn_on(self._device) + except (AccessTokenError, ParameterError, UnknownApiError) as err: + raise HomeAssistantError(f"Wyze returned an error: {err.args}") from err + except ClientConnectionError as err: + raise HomeAssistantError(err) from err + else: + self._device.on = True + self._just_updated = True + self.async_schedule_update_ha_state() @token_exception_handler async def async_turn_off(self, **kwargs: Any) -> None: - await self._service.turn_off(self._device) - - self._device.on = False - self._just_updated = True - self.async_schedule_update_ha_state() + try: + await self._service.turn_off(self._device) + except (AccessTokenError, ParameterError, UnknownApiError) as err: + raise HomeAssistantError(f"Wyze returned an error: {err.args}") from err + except ClientConnectionError as err: + raise HomeAssistantError(err) from err + else: + self._device.on = False + self._just_updated = True + self.async_schedule_update_ha_state() @property def name(self): @@ -254,10 +270,7 @@ def extra_state_attributes(self): @token_exception_handler async def async_update(self): - """ - This function updates the entity - """ - + """Update the entity.""" if not self._just_updated: self._device = await self._service.update(self._device) else: @@ -337,17 +350,28 @@ def should_poll(self) -> bool: async def async_turn_on(self, **kwargs: Any) -> None: """Turn on the switch.""" - await self._service.turn_on_notifications(self._device) + try: + await self._service.turn_on_notifications(self._device) + except (AccessTokenError, ParameterError, UnknownApiError) as err: + raise HomeAssistantError(f"Wyze returned an error: {err.args}") from err + except ClientConnectionError as err: + raise HomeAssistantError(err) from err + else: + self._device.notify = True + self.async_write_ha_state() - self._device.notify = True - self.async_write_ha_state() async def async_turn_off(self, **kwargs: Any) -> None: """Turn off the switch.""" - await self._service.turn_off_notifications(self._device) - - self._device.notify = False - self.async_write_ha_state() + try: + await self._service.turn_off_notifications(self._device) + except (AccessTokenError, ParameterError, UnknownApiError) as err: + raise HomeAssistantError(f"Wyze returned an error: {err.args}") from err + except ClientConnectionError as err: + raise HomeAssistantError(err) from err + else: + self._device.notify = False + self.async_write_ha_state() @property def name(self): @@ -413,17 +437,27 @@ def should_poll(self) -> bool: async def async_turn_on(self, **kwargs: Any) -> None: """Turn on the switch.""" - await self._service.turn_on_motion_detection(self._device) - - self._device.motion = True - self.async_write_ha_state() + try: + await self._service.turn_on_motion_detection(self._device) + except (AccessTokenError, ParameterError, UnknownApiError) as err: + raise HomeAssistantError(f"Wyze returned an error: {err.args}") from err + except ClientConnectionError as err: + raise HomeAssistantError(err) from err + else: + self._device.motion = True + self.async_write_ha_state() async def async_turn_off(self, **kwargs: Any) -> None: """Turn off the switch.""" - await self._service.turn_off_motion_detection(self._device) - - self._device.motion = False - self.async_write_ha_state() + try: + await self._service.turn_off_motion_detection(self._device) + except (AccessTokenError, ParameterError, UnknownApiError) as err: + raise HomeAssistantError(f"Wyze returned an error: {err.args}") from err + except ClientConnectionError as err: + raise HomeAssistantError(err) from err + else: + self._device.motion = False + self.async_write_ha_state() @property def name(self): @@ -487,17 +521,27 @@ def should_poll(self) -> bool: async def async_turn_on(self, **kwargs: Any) -> None: """Turn on the switch.""" - await self._service.music_mode_on(self._device) - - self._device.music_mode = True - self.async_schedule_update_ha_state() + try: + await self._service.music_mode_on(self._device) + except (AccessTokenError, ParameterError, UnknownApiError) as err: + raise HomeAssistantError(f"Wyze returned an error: {err.args}") from err + except ClientConnectionError as err: + raise HomeAssistantError(err) from err + else: + self._device.music_mode = True + self.async_schedule_update_ha_state() async def async_turn_off(self, **kwargs: Any) -> None: """Turn off the switch.""" - await self._service.music_mode_off(self._device) - - self._device.music_mode = False - self.async_schedule_update_ha_state() + try: + await self._service.music_mode_off(self._device) + except (AccessTokenError, ParameterError, UnknownApiError) as err: + raise HomeAssistantError(f"Wyze returned an error: {err.args}") from err + except ClientConnectionError as err: + raise HomeAssistantError(err) from err + else: + self._device.music_mode = False + self.async_schedule_update_ha_state() @property def name(self):