Skip to content

Commit

Permalink
Rempved cloud_io() and implemented assumed_state property
Browse files Browse the repository at this point in the history
  • Loading branch information
albertogeniola committed Apr 23, 2020
1 parent 1a72263 commit b773a8b
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 55 deletions.
13 changes: 5 additions & 8 deletions custom_components/meross_cloud/climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
ThermostatModeChange,
ThermostatTemperatureChange)

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

_LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -51,7 +51,6 @@ def __init__(self, device: ValveSubDevice):
self._first_update_done = False
self._target_temperature = None

@cloud_io()
def update(self):
if self._device.online:
self._device.get_status(force_status_refresh=True)
Expand All @@ -76,7 +75,10 @@ def notify_client_state(self, status: ClientStatus):
self.schedule_update_ha_state(False)

@property
@cloud_io(default_return_value=None)
def assumed_state(self) -> bool:
return not self._first_update_done

@property
def current_temperature(self) -> float:
if not self._first_update_done:
# Schedule update and return
Expand All @@ -86,7 +88,6 @@ def current_temperature(self) -> float:
return float(self._device.get_status().get('temperature').get('room'))/10

@property
@cloud_io(default_return_value=None)
def hvac_action(self) -> str:
if not self._first_update_done:
# Schedule update and return
Expand All @@ -104,7 +105,6 @@ def hvac_action(self) -> str:
return CURRENT_HVAC_IDLE

@property
@cloud_io(default_return_value=HVAC_MODE_OFF)
def hvac_mode(self) -> str:
if not self._first_update_done:
# Schedule update and return
Expand Down Expand Up @@ -160,14 +160,12 @@ def temperature_unit(self) -> str:
def hvac_modes(self) -> List[str]:
return [HVAC_MODE_OFF, HVAC_MODE_AUTO, HVAC_MODE_HEAT]

@cloud_io()
def set_temperature(self, **kwargs) -> None:
target = kwargs.get('temperature')
self._device.set_target_temperature(target, callback=none_callback, timeout=THERMOSTAT_TIMEOUT)
# Assume update will work, thus update local state immediately
self._target_temperature = target

@cloud_io()
def set_hvac_mode(self, hvac_mode: str) -> None:
# NOTE: this method will also update the local state as the thermostat will take too much time to get the
# command ACK.turn_on
Expand Down Expand Up @@ -223,7 +221,6 @@ def action(error, response):
else:
_LOGGER.warning("Unsupported mode for this device")

@cloud_io()
def set_preset_mode(self, preset_mode: str) -> None:
if self._device.type == "mts100v3":
self._device_mode = ThermostatV3Mode[preset_mode] # Update local state
Expand Down
16 changes: 0 additions & 16 deletions custom_components/meross_cloud/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,22 +75,6 @@ def notify_client_state(self, status: ClientStatus):
pass


def cloud_io(default_return_value=None):
def cloud_io_inner(func):
@functools.wraps(func)
def func_wrapper(*args, **kwargs):
try:
return func(*args, **kwargs)
except CommandTimeoutException as e:
log_exception("A command timeout occurred while handling cloud_io function.", logger=_LOGGER)
if default_return_value is not None:
return default_return_value
else:
return None
return func_wrapper
return cloud_io_inner


def log_exception(message: str = None, logger: logging = None):
if logger is None:
logger = logging.getLogger(__name__)
Expand Down
13 changes: 5 additions & 8 deletions custom_components/meross_cloud/cover.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from meross_iot.meross_event import (DeviceDoorStatusEvent,
DeviceOnlineStatusEvent)

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

_LOGGER = logging.getLogger(__name__)

Expand All @@ -35,7 +35,6 @@ def __init__(self, device: GenericGarageDoorOpener):
self._opening = False
self._closing = False

@cloud_io()
def update(self):
if self._device.online:
self._device.get_status(force_status_refresh=True)
Expand Down Expand Up @@ -68,6 +67,10 @@ def notify_client_state(self, status: ClientStatus):
self._available = False
self.schedule_update_ha_state(False)

@property
def assumed_state(self) -> bool:
return not self._first_update_done

@property
def name(self) -> str:
"""Return the name of the cover."""
Expand All @@ -80,7 +83,6 @@ def available(self) -> bool:
return self._available and self._device.online

@property
@cloud_io(default_return_value=True)
def is_closed(self):
if not self._first_update_done:
# Schedule update and return
Expand All @@ -91,7 +93,6 @@ def is_closed(self):
return not self._device.get_status(False).get(self._channel)

@property
@cloud_io(default_return_value=False)
def is_open(self):
if not self._first_update_done:
# Schedule update and return
Expand All @@ -102,7 +103,6 @@ def is_open(self):
return self._device.get_status(False).get(self._channel)

@property
@cloud_io(default_return_value=False)
def is_opening(self):
if not self._first_update_done:
# Schedule update and return
Expand All @@ -113,15 +113,13 @@ def is_opening(self):
return self._opening

@property
@cloud_io(default_return_value=False)
def is_closing(self):
if not self._first_update_done:
# Schedule update and return
self.schedule_update_ha_state(True)
return None
return self._closing

@cloud_io()
def close_cover(self, **kwargs):
"""Close the cover."""
is_closed = not self._device.get_status(False).get(self._channel)
Expand All @@ -133,7 +131,6 @@ def close_cover(self, **kwargs):
if self.enabled:
self.schedule_update_ha_state(False)

@cloud_io()
def open_cover(self, **kwargs):
"""Open the cover."""
is_open = self._device.get_status(False).get(self._channel)
Expand Down
12 changes: 5 additions & 7 deletions custom_components/meross_cloud/fan.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from meross_iot.meross_event import (DeviceOnlineStatusEvent,
HumidifierSpryEvent)

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

_LOGGER = logging.getLogger(__name__)

Expand All @@ -27,7 +27,6 @@ def __init__(self, device: GenericHumidifier):
self._available = True # Assume the mqtt client is connected
self._first_update_done = False

@cloud_io()
def update(self):
if self._device.online:
self._device.get_status(force_status_refresh=True)
Expand All @@ -54,6 +53,10 @@ async def async_added_to_hass(self) -> None:
async def async_will_remove_from_hass(self) -> None:
self._device.unregister_event_callback(self.device_event_handler)

@property
def assumed_state(self) -> bool:
return not self._first_update_done

@property
def available(self) -> bool:
# A device is available if the client library is connected to the MQTT broker and if the
Expand All @@ -72,7 +75,6 @@ def is_on(self) -> bool:
return spry_mode != SprayMode.OFF

@property
@cloud_io(default_return_value=None)
def speed(self) -> Optional[str]:
if not self._first_update_done:
# Schedule update and return
Expand All @@ -90,17 +92,14 @@ def speed_list(self) -> list:
"""Get the list of available speeds."""
return [e.name for e in SprayMode]

@cloud_io()
def set_speed(self, speed: str) -> None:
mode = SprayMode[speed]
self._device.set_spray_mode(mode)

@cloud_io()
def set_direction(self, direction: str) -> None:
# Not supported
pass

@cloud_io()
def turn_on(self, speed: Optional[str] = None, **kwargs) -> None:
if speed is None:
mode = SprayMode.CONTINUOUS
Expand All @@ -109,7 +108,6 @@ def turn_on(self, speed: Optional[str] = None, **kwargs) -> None:

self._device.set_spray_mode(mode)

@cloud_io()
def turn_off(self, **kwargs: Any) -> None:
self._device.set_spray_mode(SprayMode.OFF)

Expand Down
13 changes: 5 additions & 8 deletions custom_components/meross_cloud/light.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from meross_iot.meross_event import (BulbLightStateChangeEvent,
BulbSwitchStateChangeEvent,
DeviceOnlineStatusEvent)
from .common import (DOMAIN, HA_LIGHT, MANAGER, ConnectionWatchDog, cloud_io, log_exception)
from .common import (DOMAIN, HA_LIGHT, MANAGER, ConnectionWatchDog, log_exception)
_LOGGER = logging.getLogger(__name__)


Expand Down Expand Up @@ -48,7 +48,6 @@ def __init__(self, device: GenericBulb, channel: int):
self._flags |= SUPPORT_COLOR
self._flags |= SUPPORT_COLOR_TEMP

@cloud_io()
def update(self):
if self._device.online:
self._device.get_status(force_status_refresh=True)
Expand Down Expand Up @@ -99,6 +98,10 @@ def notify_client_state(self, status: ClientStatus):
self._available = False
self.schedule_update_ha_state(False)

@property
def assumed_state(self) -> bool:
return not self._first_update_done

@property
def should_poll(self) -> bool:
return False
Expand All @@ -109,7 +112,6 @@ def available(self) -> bool:
return self._available and self._device.online

@property
@cloud_io(default_return_value=False)
def is_on(self) -> bool:
if not self._first_update_done:
# Schedule update and return
Expand All @@ -119,7 +121,6 @@ def is_on(self) -> bool:
return self._device.get_channel_status(channel=self._channel_id).get('onoff')

@property
@cloud_io(default_return_value=0)
def brightness(self):
if not self._first_update_done:
# Schedule update and return
Expand All @@ -134,7 +135,6 @@ def brightness(self):
return float(luminance) / 100 * 255

@property
@cloud_io(default_return_value=0)
def hs_color(self):
if not self._first_update_done:
# Schedule update and return
Expand All @@ -148,7 +148,6 @@ def hs_color(self):
return None

@property
@cloud_io(default_return_value=0)
def color_temp(self):
if not self._first_update_done:
# Schedule update and return
Expand Down Expand Up @@ -184,11 +183,9 @@ def device_info(self):
'sw_version': self._device.fwversion
}

@cloud_io()
def turn_off(self, **kwargs) -> None:
self._device.turn_off(channel=self._channel_id)

@cloud_io()
def turn_on(self, **kwargs) -> None:
if not self.is_on:
self._device.turn_on(channel=self._channel_id)
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",
"meross_cloud_version": "0.3.4.4-2",
"quality_scale": "gold"
}
7 changes: 5 additions & 2 deletions custom_components/meross_cloud/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from meross_iot.cloud.devices.power_plugs import GenericPlug
from meross_iot.meross_event import DeviceOnlineStatusEvent

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

_LOGGER = logging.getLogger(__name__)

Expand All @@ -22,7 +22,6 @@ def __init__(self, device: GenericPlug):
self._sensor_info = None
self._available = True # Assume the mqtt client is connected

@cloud_io()
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
Expand Down Expand Up @@ -54,6 +53,10 @@ def notify_client_state(self, status: ClientStatus):
self._available = False
self.schedule_update_ha_state(False)

@property
def assumed_state(self) -> bool:
return not self._first_update_done

@property
def available(self) -> bool:
return self._available and self._device.online
Expand Down
6 changes: 1 addition & 5 deletions custom_components/meross_cloud/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from meross_iot.cloud.client_status import ClientStatus
from meross_iot.cloud.devices.power_plugs import GenericPlug
from meross_iot.manager import MerossManager
from .common import (DOMAIN, HA_SWITCH, MANAGER, calculate_switch_id, ConnectionWatchDog, cloud_io, MerossEntityWrapper)
from .common import (DOMAIN, HA_SWITCH, MANAGER, calculate_switch_id, ConnectionWatchDog, MerossEntityWrapper)

_LOGGER = logging.getLogger(__name__)

Expand All @@ -29,7 +29,6 @@ def __init__(self, device: GenericPlug, channel: int):
self._available = True # Assume the mqtt client is connected
self._first_update_done = False

@cloud_io()
def update(self):
if self._device.online:
self._device.get_status(force_status_refresh=True)
Expand Down Expand Up @@ -83,7 +82,6 @@ def should_poll(self) -> bool:
return False

@property
@cloud_io(default_return_value=False)
def is_on(self) -> bool:
if not self._first_update_done:
# Schedule update and return
Expand All @@ -96,11 +94,9 @@ def is_on(self) -> bool:
def assumed_state(self) -> bool:
return not self._first_update_done

@cloud_io()
def turn_off(self, **kwargs) -> None:
self._device.turn_off_channel(self._channel_id)

@cloud_io()
def turn_on(self, **kwargs) -> None:
self._device.turn_on_channel(self._channel_id)

Expand Down

0 comments on commit b773a8b

Please sign in to comment.