Skip to content

Commit

Permalink
Implemented workaround for connection drop
Browse files Browse the repository at this point in the history
  • Loading branch information
albertogeniola committed Apr 11, 2020
1 parent 4601c57 commit a75eee6
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 309 deletions.
8 changes: 2 additions & 6 deletions custom_components/meross_cloud/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,12 @@
from homeassistant.exceptions import ConfigEntryNotReady
from homeassistant.helpers.typing import HomeAssistantType
from meross_iot.api import UnauthorizedException
from meross_iot.logger import ROOT_MEROSS_LOGGER, h, set_log_level
from meross_iot.logger import ROOT_MEROSS_LOGGER, h
from meross_iot.manager import MerossManager

from .common import (ATTR_CONFIG, CLOUD_HANDLER, DOMAIN, HA_CLIMATE, HA_COVER,
HA_FAN, HA_LIGHT, HA_SENSOR, HA_SWITCH, MANAGER,
MEROSS_PLATFORMS, SENSORS, MerossCloudConnectionWatchdog,
dismiss_notification, notify_error)
MEROSS_PLATFORMS, SENSORS, dismiss_notification, notify_error)

# Unset the default stream handler for logger of the meross_iot library
ROOT_MEROSS_LOGGER.removeHandler(h)
Expand Down Expand Up @@ -56,8 +55,6 @@ async def async_setup_entry(hass: HomeAssistantType, config_entry):
# The following call can cause am UnauthorizedException if bad login credentials are provided
# or if a network exception occurs.
manager = MerossManager(meross_email=config_entry.data.get(CONF_USERNAME), meross_password=config_entry.data.get(CONF_PASSWORD))
cloud_handler = MerossCloudConnectionWatchdog(manager=manager, hass=hass)
hass.data[DOMAIN][CLOUD_HANDLER] = cloud_handler
hass.data[DOMAIN][MANAGER] = manager
hass.data[DOMAIN][HA_CLIMATE] = {}
hass.data[DOMAIN][HA_COVER] = {}
Expand All @@ -70,7 +67,6 @@ async def async_setup_entry(hass: HomeAssistantType, config_entry):
manager.start()

_LOGGER.info("Starting meross cloud connection watchdog")
cloud_handler.register_manager()

for platform in MEROSS_PLATFORMS:
hass.async_create_task(
Expand Down
32 changes: 10 additions & 22 deletions custom_components/meross_cloud/climate.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import logging
from typing import Any, List, Optional, Union
from typing import List, Optional

from homeassistant.components.climate import (SUPPORT_PRESET_MODE,
SUPPORT_TARGET_TEMPERATURE,
Expand All @@ -10,10 +10,7 @@
HVAC_MODE_AUTO,
HVAC_MODE_HEAT,
HVAC_MODE_OFF, PRESET_NONE)
from homeassistant.components.fan import FanEntity
from homeassistant.const import STATE_OFF, STATE_UNKNOWN, TEMP_CELSIUS
from homeassistant.helpers.entity import Entity
from meross_iot.cloud.devices.humidifier import GenericHumidifier, SprayMode
from homeassistant.const import TEMP_CELSIUS
from meross_iot.cloud.devices.subdevices.thermostats import (ThermostatMode,
ThermostatV3Mode,
ValveSubDevice)
Expand All @@ -23,8 +20,7 @@
ThermostatModeChange,
ThermostatTemperatureChange)

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

_LOGGER = logging.getLogger(__name__)

Expand All @@ -36,11 +32,11 @@ def none_callback(err, resp):
pass


class ValveEntityWrapper(ClimateDevice, AbstractMerossEntityWrapper):
class ValveEntityWrapper(ClimateDevice):
"""Wrapper class to adapt the Meross thermostat into the Homeassistant platform"""

def __init__(self, device: ValveSubDevice):
super().__init__(device)
self._device = device

self._id = self._device.uuid + ":" + self._device.subdevice_id
self._device_name = self._device.name
Expand All @@ -63,8 +59,6 @@ def __init__(self, device: ValveSubDevice):
self.update()

def device_event_handler(self, evt):
# Any event received from the device causes the reset of the error state
self.reset_error_state()

# Handle here events that are common to all the wrappers
if isinstance(evt, DeviceOnlineStatusEvent):
Expand All @@ -85,14 +79,6 @@ def device_event_handler(self, evt):

self.schedule_update_ha_state(False)

def force_state_update(self, ui_only=False):
if not self.enabled:
return

force_refresh = not ui_only
self.schedule_update_ha_state(force_refresh=force_refresh)

@cloud_io
def update(self):
state = self._device.get_status(True)
self._is_online = self._device.online
Expand Down Expand Up @@ -176,14 +162,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 @@ -239,7 +223,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 Expand Up @@ -345,6 +328,11 @@ def sync_logic():

return climante_devices

# Register a connection watchdog to notify devices when connection to the cloud MQTT goes down.
manager = hass.data[DOMAIN][MANAGER] # type:MerossManager
watchdog = ConnectionWatchDog(hass=hass, platform=HA_CLIMATE)
manager.register_event_handler(watchdog.connection_handler)

thermostat_devices = await hass.async_add_executor_job(sync_logic)
async_add_entities(thermostat_devices)

Expand Down
193 changes: 0 additions & 193 deletions custom_components/meross_cloud/common.py

This file was deleted.

Loading

0 comments on commit a75eee6

Please sign in to comment.