From 88c793340cba29f592e456a3946fd7c8049131ad Mon Sep 17 00:00:00 2001 From: Brian Rogers Date: Thu, 5 Sep 2024 16:31:08 -0400 Subject: [PATCH 1/2] raise HA errors --- .../wyzeapi/alarm_control_panel.py | 41 +++--- custom_components/wyzeapi/climate.py | 104 ++++++++------ custom_components/wyzeapi/light.py | 63 +++++---- custom_components/wyzeapi/lock.py | 24 ++-- custom_components/wyzeapi/siren.py | 28 ++-- custom_components/wyzeapi/switch.py | 130 +++++++++++------- 6 files changed, 227 insertions(+), 163 deletions(-) diff --git a/custom_components/wyzeapi/alarm_control_panel.py b/custom_components/wyzeapi/alarm_control_panel.py index 713599c9..63b610af 100644 --- a/custom_components/wyzeapi/alarm_control_panel.py +++ b/custom_components/wyzeapi/alarm_control_panel.py @@ -13,8 +13,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 +65,36 @@ 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 + 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 + 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 + 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..8cef5fb1 100644 --- a/custom_components/wyzeapi/climate.py +++ b/custom_components/wyzeapi/climate.py @@ -20,8 +20,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 +192,78 @@ 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 + 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 + 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 + 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 + 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 df5d0491..403c8860 100644 --- a/custom_components/wyzeapi/light.py +++ b/custom_components/wyzeapi/light.py @@ -1,7 +1,6 @@ #!/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 @@ -21,7 +20,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 @@ -91,12 +92,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 { @@ -196,22 +191,26 @@ 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 + 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 + else: + self._bulb.on = False + self._just_updated = True + self.async_schedule_update_ha_state() @property def supported_color_modes(self): @@ -371,20 +370,26 @@ def __init__(self, camera: Camera, camera_service: CameraService) -> None: @token_exception_handler async def async_turn_on(self, **kwargs) -> None: """Turn the floodlight on.""" - await self._service.floodlight_on(self._device) - - self._is_on = 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 + 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._is_on = 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 + 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..b9e48a28 100644 --- a/custom_components/wyzeapi/lock.py +++ b/custom_components/wyzeapi/lock.py @@ -9,6 +9,7 @@ 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 +17,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 +94,23 @@ 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 + 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 + 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 a63f59a9..14c71c27 100644 --- a/custom_components/wyzeapi/siren.py +++ b/custom_components/wyzeapi/siren.py @@ -6,6 +6,7 @@ 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 +15,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 +65,26 @@ 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 + 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 + 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 bfe9d02a..26a25811 100644 --- a/custom_components/wyzeapi/switch.py +++ b/custom_components/wyzeapi/switch.py @@ -10,9 +10,11 @@ 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 @@ -110,18 +112,24 @@ 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 + 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 + else: + self._is_on = False + self._just_updated = True + self.async_schedule_update_ha_state() @property def name(self): @@ -147,12 +155,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 @@ -191,19 +193,25 @@ 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 + 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 + else: + self._device.on = False + self._just_updated = True + self.async_schedule_update_ha_state() @property def name(self): @@ -246,10 +254,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: @@ -331,17 +336,24 @@ 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 + 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 + else: + self._device.notify = False + self.async_write_ha_state() @property def name(self): @@ -407,17 +419,23 @@ 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 + 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 + else: + self._device.motion = False + self.async_write_ha_state() @property def name(self): @@ -481,17 +499,23 @@ 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 + 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 + else: + self._device.music_mode = False + self.async_schedule_update_ha_state() @property def name(self): From b487c2b4958fd7ff731018afea249ed43a2cbca0 Mon Sep 17 00:00:00 2001 From: Brian Rogers Date: Fri, 6 Sep 2024 10:56:18 -0400 Subject: [PATCH 2/2] catch connection errors --- .../wyzeapi/alarm_control_panel.py | 7 ++++++ custom_components/wyzeapi/climate.py | 9 ++++++++ custom_components/wyzeapi/light.py | 9 ++++++++ custom_components/wyzeapi/lock.py | 5 +++++ custom_components/wyzeapi/siren.py | 5 +++++ custom_components/wyzeapi/switch.py | 22 ++++++++++++++++++- 6 files changed, 56 insertions(+), 1 deletion(-) diff --git a/custom_components/wyzeapi/alarm_control_panel.py b/custom_components/wyzeapi/alarm_control_panel.py index 63b610af..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, @@ -72,6 +73,8 @@ async def async_alarm_disarm(self, code=None) -> None: 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 @@ -82,6 +85,8 @@ async def async_alarm_arm_home(self, code=None): 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 @@ -92,6 +97,8 @@ async def async_alarm_arm_away(self, code=None): 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 diff --git a/custom_components/wyzeapi/climate.py b/custom_components/wyzeapi/climate.py index 8cef5fb1..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, @@ -201,6 +202,8 @@ async def async_set_temperature(self, **kwargs) -> None: 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() @@ -219,6 +222,8 @@ async def async_set_fan_mode(self, fan_mode: str) -> None: 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() @@ -240,6 +245,8 @@ async def async_set_hvac_mode(self, hvac_mode: str) -> None: 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() @@ -261,6 +268,8 @@ async def async_set_preset_mode(self, preset_mode: str) -> None: 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() diff --git a/custom_components/wyzeapi/light.py b/custom_components/wyzeapi/light.py index 403c8860..fe681238 100644 --- a/custom_components/wyzeapi/light.py +++ b/custom_components/wyzeapi/light.py @@ -5,6 +5,7 @@ # 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 ( @@ -195,6 +196,8 @@ async def async_turn_on(self, **kwargs: Any) -> None: 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 @@ -207,6 +210,8 @@ async def async_turn_off(self, **kwargs: Any) -> None: 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 @@ -374,6 +379,8 @@ async def async_turn_on(self, **kwargs) -> None: 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 @@ -386,6 +393,8 @@ async def async_turn_off(self, **kwargs): 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 diff --git a/custom_components/wyzeapi/lock.py b/custom_components/wyzeapi/lock.py index b9e48a28..72b44cb4 100644 --- a/custom_components/wyzeapi/lock.py +++ b/custom_components/wyzeapi/lock.py @@ -5,6 +5,7 @@ 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 @@ -98,6 +99,8 @@ async def async_lock(self, **kwargs): 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() @@ -108,6 +111,8 @@ async def async_unlock(self, **kwargs): 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() diff --git a/custom_components/wyzeapi/siren.py b/custom_components/wyzeapi/siren.py index 14c71c27..ccdcfea6 100644 --- a/custom_components/wyzeapi/siren.py +++ b/custom_components/wyzeapi/siren.py @@ -3,6 +3,7 @@ """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 @@ -69,6 +70,8 @@ async def async_turn_on(self, **kwargs) -> None: 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 @@ -81,6 +84,8 @@ async def async_turn_off(self, **kwargs): 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 diff --git a/custom_components/wyzeapi/switch.py b/custom_components/wyzeapi/switch.py index 26a25811..f7c2caeb 100644 --- a/custom_components/wyzeapi/switch.py +++ b/custom_components/wyzeapi/switch.py @@ -3,8 +3,8 @@ """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 @@ -116,6 +116,8 @@ async def async_turn_on(self, **kwargs: Any) -> None: 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 @@ -126,6 +128,8 @@ async def async_turn_off(self, **kwargs: Any) -> None: 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 @@ -197,6 +201,8 @@ async def async_turn_on(self, **kwargs: Any) -> None: 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 @@ -208,6 +214,8 @@ async def async_turn_off(self, **kwargs: Any) -> None: 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 @@ -340,6 +348,8 @@ async def async_turn_on(self, **kwargs: Any) -> None: 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() @@ -351,6 +361,8 @@ async def async_turn_off(self, **kwargs: Any) -> None: 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() @@ -423,6 +435,8 @@ async def async_turn_on(self, **kwargs: Any) -> None: 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() @@ -433,6 +447,8 @@ async def async_turn_off(self, **kwargs: Any) -> None: 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() @@ -503,6 +519,8 @@ async def async_turn_on(self, **kwargs: Any) -> None: 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() @@ -513,6 +531,8 @@ async def async_turn_off(self, **kwargs: Any) -> None: 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()