Skip to content

Commit

Permalink
Added exception custom exception printer for update() method
Browse files Browse the repository at this point in the history
  • Loading branch information
albertogeniola committed Apr 24, 2020
1 parent 2643d5b commit db43c2c
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 87 deletions.
27 changes: 15 additions & 12 deletions custom_components/meross_cloud/climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@
from meross_iot.cloud.devices.subdevices.thermostats import (ThermostatMode,
ThermostatV3Mode,
ValveSubDevice)
from meross_iot.cloud.exceptions.CommandTimeoutException import CommandTimeoutException
from meross_iot.manager import MerossManager
from meross_iot.meross_event import (DeviceOnlineStatusEvent,
DeviceSwitchStatusEvent,
ThermostatModeChange,
ThermostatTemperatureChange)

from .common import (DOMAIN, HA_CLIMATE, MANAGER, ConnectionWatchDog, MerossEntityWrapper)
from .common import (DOMAIN, HA_CLIMATE, MANAGER, ConnectionWatchDog, MerossEntityWrapper, log_exception)

_LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -53,9 +54,13 @@ def __init__(self, device: ValveSubDevice):

def update(self):
if self._device.online:
self._device.get_status(force_status_refresh=True)
self._target_temperature = float(self._device.get_status().get('temperature').get('currentSet'))/10
self._first_update_done = True
try:
self._device.get_status(force_status_refresh=True)
self._target_temperature = float(self._device.get_status().get('temperature').get('currentSet'))/10
self._first_update_done = True
except CommandTimeoutException as e:
log_exception(logger=_LOGGER)
raise

def device_event_handler(self, evt):
if isinstance(evt, ThermostatTemperatureChange):
Expand All @@ -65,14 +70,12 @@ def device_event_handler(self, evt):

def notify_client_state(self, status: ClientStatus):
# When a connection change occurs, update the internal state
if status == ClientStatus.SUBSCRIBED:
# If we are connecting back, schedule a full refresh of the device
self.schedule_update_ha_state(True)
else:
# In any other case, mark the device unavailable
# and only update the UI
self._available = False
self.schedule_update_ha_state(False)
# If we are connecting back, schedule a full refresh of the device
# In any other case, mark the device unavailable
# and only update the UI
client_online = status == ClientStatus.SUBSCRIBED
self._available = client_online
self.schedule_update_ha_state(client_online)

@property
def assumed_state(self) -> bool:
Expand Down
33 changes: 18 additions & 15 deletions custom_components/meross_cloud/cover.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@
STATE_OPENING, STATE_UNKNOWN)
from meross_iot.cloud.client_status import ClientStatus
from meross_iot.cloud.devices.door_openers import GenericGarageDoorOpener
from meross_iot.cloud.exceptions.CommandTimeoutException import CommandTimeoutException
from meross_iot.meross_event import (DeviceDoorStatusEvent,
DeviceOnlineStatusEvent)

from .common import (DOMAIN, HA_COVER, MANAGER, ConnectionWatchDog, MerossEntityWrapper)
from .common import (DOMAIN, HA_COVER, MANAGER, ConnectionWatchDog, MerossEntityWrapper, log_exception)

_LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -37,12 +38,16 @@ def __init__(self, device: GenericGarageDoorOpener):

def update(self):
if self._device.online:
self._device.get_status(force_status_refresh=True)
# Reset derived states
self._opening = False
self._closing = False
# Mark first update done
self._first_update_done = True
try:
self._device.get_status(force_status_refresh=True)
# Reset derived states
self._opening = False
self._closing = False
# Mark first update done
self._first_update_done = True
except CommandTimeoutException as e:
log_exception(logger=_LOGGER)
raise

def device_event_handler(self, evt):
# Whenever an open/closed push notitication is received, make sure to reset the
Expand All @@ -58,14 +63,12 @@ def device_event_handler(self, evt):

def notify_client_state(self, status: ClientStatus):
# When a connection change occurs, update the internal state
if status == ClientStatus.SUBSCRIBED:
# If we are connecting back, schedule a full refresh of the device
self.schedule_update_ha_state(True)
else:
# In any other case, mark the device unavailable
# and only update the UI
self._available = False
self.schedule_update_ha_state(False)
# If we are connecting back, schedule a full refresh of the device
# In any other case, mark the device unavailable
# and only update the UI
client_online = status == ClientStatus.SUBSCRIBED
self._available = client_online
self.schedule_update_ha_state(client_online)

@property
def assumed_state(self) -> bool:
Expand Down
25 changes: 14 additions & 11 deletions custom_components/meross_cloud/fan.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
from homeassistant.components.fan import SUPPORT_SET_SPEED, FanEntity
from meross_iot.cloud.client_status import ClientStatus
from meross_iot.cloud.devices.humidifier import GenericHumidifier, SprayMode
from meross_iot.cloud.exceptions.CommandTimeoutException import CommandTimeoutException
from meross_iot.manager import MerossManager
from meross_iot.meross_event import (DeviceOnlineStatusEvent,
HumidifierSpryEvent)

from .common import (DOMAIN, HA_FAN, MANAGER, ConnectionWatchDog, MerossEntityWrapper)
from .common import (DOMAIN, HA_FAN, MANAGER, ConnectionWatchDog, MerossEntityWrapper, log_exception)

_LOGGER = logging.getLogger(__name__)

Expand All @@ -29,23 +30,25 @@ def __init__(self, device: GenericHumidifier):

def update(self):
if self._device.online:
self._device.get_status(force_status_refresh=True)
self._first_update_done = True
try:
self._device.get_status(force_status_refresh=True)
self._first_update_done = True
except CommandTimeoutException as e:
log_exception(logger=_LOGGER)
raise

def device_event_handler(self, evt):
# Update the device state when an event occurs
self.schedule_update_ha_state(False)

def notify_client_state(self, status: ClientStatus):
# When a connection change occurs, update the internal state
if status == ClientStatus.SUBSCRIBED:
# If we are connecting back, schedule a full refresh of the device
self.schedule_update_ha_state(True)
else:
# In any other case, mark the device unavailable
# and only update the UI
self._available = False
self.schedule_update_ha_state(False)
# If we are connecting back, schedule a full refresh of the device
# In any other case, mark the device unavailable
# and only update the UI
client_online = status == ClientStatus.SUBSCRIBED
self._available = client_online
self.schedule_update_ha_state(client_online)

async def async_added_to_hass(self) -> None:
self._device.register_event_callback(self.device_event_handler)
Expand Down
37 changes: 20 additions & 17 deletions custom_components/meross_cloud/light.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
Light)
from meross_iot.cloud.client_status import ClientStatus
from meross_iot.cloud.devices.light_bulbs import GenericBulb
from meross_iot.cloud.exceptions.CommandTimeoutException import CommandTimeoutException
from meross_iot.manager import MerossManager
from meross_iot.meross_event import (BulbLightStateChangeEvent,
BulbSwitchStateChangeEvent,
Expand Down Expand Up @@ -50,15 +51,19 @@ def __init__(self, device: GenericBulb, channel: int):

def update(self):
if self._device.online:
self._device.get_status(force_status_refresh=True)
self._flags = 0
if self._device.supports_luminance():
self._flags |= SUPPORT_BRIGHTNESS
if self._device.is_rgb():
self._flags |= SUPPORT_COLOR
if self._device.is_light_temperature():
self._flags |= SUPPORT_COLOR_TEMP
self._first_update_done = True
try:
self._device.get_status(force_status_refresh=True)
self._flags = 0
if self._device.supports_luminance():
self._flags |= SUPPORT_BRIGHTNESS
if self._device.is_rgb():
self._flags |= SUPPORT_COLOR
if self._device.is_light_temperature():
self._flags |= SUPPORT_COLOR_TEMP
self._first_update_done = True
except CommandTimeoutException as e:
log_exception(logger=_LOGGER)
raise

def device_event_handler(self, evt):
# Update the device state as soon as an event occurs
Expand Down Expand Up @@ -89,14 +94,12 @@ def device_event_handler(self, evt):

def notify_client_state(self, status: ClientStatus):
# When a connection change occurs, update the internal state
if status == ClientStatus.SUBSCRIBED:
# If we are connecting back, schedule a full refresh of the device
self.schedule_update_ha_state(True)
else:
# In any other case, mark the device unavailable
# and only update the UI
self._available = False
self.schedule_update_ha_state(False)
# If we are connecting back, schedule a full refresh of the device
# In any other case, mark the device unavailable
# and only update the UI
client_online = status == ClientStatus.SUBSCRIBED
self._available = client_online
self.schedule_update_ha_state(client_online)

@property
def assumed_state(self) -> bool:
Expand Down
2 changes: 1 addition & 1 deletion custom_components/meross_cloud/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
"codeowners": ["@albertogeniola"],
"requirements": ["meross-iot==0.3.4.4"],
"config_flow": true,
"meross_cloud_version": "0.3.4.4-2",
"meross_cloud_version": "0.3.4.4-3",
"quality_scale": "gold"
}
44 changes: 24 additions & 20 deletions custom_components/meross_cloud/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
from homeassistant.helpers.entity import Entity
from meross_iot.cloud.client_status import ClientStatus
from meross_iot.cloud.devices.power_plugs import GenericPlug
from meross_iot.cloud.exceptions.CommandTimeoutException import CommandTimeoutException
from meross_iot.meross_event import DeviceOnlineStatusEvent

from .common import (DOMAIN, HA_SENSOR, MANAGER, calculate_sensor_id, ConnectionWatchDog, MerossEntityWrapper)
from .common import (DOMAIN, HA_SENSOR, MANAGER, calculate_sensor_id, ConnectionWatchDog, MerossEntityWrapper,
log_exception)

_LOGGER = logging.getLogger(__name__)

Expand All @@ -26,32 +28,34 @@ def update(self):
# Given that the device is online, we force a full state refresh.
# This is necessary as this device is handled with HA should_poll=True
# flag, so the UPDATE should every time update its status.
self._device.get_status(force_status_refresh=self._device.online)

# Update electricity stats only if the device is online and currently turned on
if self.available and self._device.get_status():
self._sensor_info = self._device.get_electricity()
else:
self._sensor_info = {
'voltage': 0,
'current': 0,
'power': 0
}
try:
self._device.get_status(force_status_refresh=self._device.online)

# Update electricity stats only if the device is online and currently turned on
if self.available and self._device.get_status():
self._sensor_info = self._device.get_electricity()
else:
self._sensor_info = {
'voltage': 0,
'current': 0,
'power': 0
}
except CommandTimeoutException as e:
log_exception(logger=_LOGGER)
raise

def device_event_handler(self, evt):
# Update the device state when an event occurs
self.schedule_update_ha_state(False)

def notify_client_state(self, status: ClientStatus):
# When a connection change occurs, update the internal state
if status == ClientStatus.SUBSCRIBED:
# If we are connecting back, schedule a full refresh of the device
self.schedule_update_ha_state(True)
else:
# In any other case, mark the device unavailable
# and only update the UI
self._available = False
self.schedule_update_ha_state(False)
# If we are connecting back, schedule a full refresh of the device
# In any other case, mark the device unavailable
# and only update the UI
client_online = status == ClientStatus.SUBSCRIBED
self._available = client_online
self.schedule_update_ha_state(client_online)

@property
def available(self) -> bool:
Expand Down
28 changes: 17 additions & 11 deletions custom_components/meross_cloud/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@
from homeassistant.components.switch import SwitchDevice
from meross_iot.cloud.client_status import ClientStatus
from meross_iot.cloud.devices.power_plugs import GenericPlug
from meross_iot.cloud.exceptions.CommandTimeoutException import CommandTimeoutException
from meross_iot.manager import MerossManager
from .common import (DOMAIN, HA_SWITCH, MANAGER, calculate_switch_id, ConnectionWatchDog, MerossEntityWrapper)
from pygments.unistring import Co

from .common import (DOMAIN, HA_SWITCH, MANAGER, calculate_switch_id, ConnectionWatchDog, MerossEntityWrapper,
log_exception)

_LOGGER = logging.getLogger(__name__)

Expand All @@ -31,23 +35,25 @@ def __init__(self, device: GenericPlug, channel: int):

def update(self):
if self._device.online:
self._device.get_status(force_status_refresh=True)
self._first_update_done = True
try:
self._device.get_status(force_status_refresh=True)
self._first_update_done = True
except CommandTimeoutException as e:
log_exception(logger=_LOGGER)
raise

def device_event_handler(self, evt):
# Update the device state when an event occurs
self.schedule_update_ha_state(False)

def notify_client_state(self, status: ClientStatus):
# When a connection change occurs, update the internal state
if status == ClientStatus.SUBSCRIBED:
# If we are connecting back, schedule a full refresh of the device
self.schedule_update_ha_state(True)
else:
# In any other case, mark the device unavailable
# and only update the UI
self._available = False
self.schedule_update_ha_state(False)
# If we are connecting back, schedule a full refresh of the device
# In any other case, mark the device unavailable
# and only update the UI
client_online = status == ClientStatus.SUBSCRIBED
self._available = client_online
self.schedule_update_ha_state(client_online)

@property
def unique_id(self) -> str:
Expand Down

0 comments on commit db43c2c

Please sign in to comment.