From d85a1d521a633eec27288f7843bdb7ca7d55e0df Mon Sep 17 00:00:00 2001 From: Alberto Geniola Date: Sun, 16 Feb 2020 19:39:44 +0100 Subject: [PATCH] Fixed bad usage of asyncio directives --- custom_components/meross_cloud/climate.py | 21 +++++++------ custom_components/meross_cloud/cover.py | 23 ++++++++------- custom_components/meross_cloud/light.py | 23 ++++++++------- custom_components/meross_cloud/sensor.py | 36 ++++++++++++----------- custom_components/meross_cloud/switch.py | 30 +++++++++++-------- 5 files changed, 74 insertions(+), 59 deletions(-) diff --git a/custom_components/meross_cloud/climate.py b/custom_components/meross_cloud/climate.py index f6af999d86..818f73e2a6 100644 --- a/custom_components/meross_cloud/climate.py +++ b/custom_components/meross_cloud/climate.py @@ -279,17 +279,20 @@ def turn_aux_heat_off(self) -> None: async def async_setup_entry(hass, config_entry, async_add_entities): - thermostat_devices = [] - manager = hass.data[DOMAIN][MANAGER] # type:MerossManager - valves = manager.get_devices_by_kind(ValveSubDevice) - for valve in valves: # type: ValveSubDevice - w = ValveEntityWrapper(device=valve) - thermostat_devices.append(w) - hass.data[DOMAIN][HA_CLIMATE][w.unique_id] = w - + def sync_logic(): + thermostat_devices = [] + manager = hass.data[DOMAIN][MANAGER] # type:MerossManager + valves = manager.get_devices_by_kind(ValveSubDevice) + for valve in valves: # type: ValveSubDevice + w = ValveEntityWrapper(device=valve) + thermostat_devices.append(w) + hass.data[DOMAIN][HA_CLIMATE][w.unique_id] = w + return thermostat_devices + + thermostat_devices = await hass.async_add_executor_job(sync_logic) async_add_entities(thermostat_devices) -async def async_setup_platform(hass, config, async_add_entities, discovery_info=None): +def setup_platform(hass, config, async_add_entities, discovery_info=None): pass diff --git a/custom_components/meross_cloud/cover.py b/custom_components/meross_cloud/cover.py index c464acf09d..0abfc6ac74 100644 --- a/custom_components/meross_cloud/cover.py +++ b/custom_components/meross_cloud/cover.py @@ -150,17 +150,20 @@ def device_info(self): async def async_setup_entry(hass, config_entry, async_add_entities): - cover_entities = [] - manager = hass.data[DOMAIN][MANAGER] # type:MerossManager - openers = manager.get_devices_by_kind(GenericGarageDoorOpener) - - for opener in openers: # type: GenericGarageDoorOpener - w = OpenGarageCover(device=opener) - cover_entities.append(w) - hass.data[DOMAIN][HA_COVER][w.unique_id] = w - + def sync_logic(): + cover_entities = [] + manager = hass.data[DOMAIN][MANAGER] # type:MerossManager + openers = manager.get_devices_by_kind(GenericGarageDoorOpener) + + for opener in openers: # type: GenericGarageDoorOpener + w = OpenGarageCover(device=opener) + cover_entities.append(w) + hass.data[DOMAIN][HA_COVER][w.unique_id] = w + return cover_entities + + cover_entities = await hass.async_add_executor_job(sync_logic) async_add_entities(cover_entities) -async def async_setup_platform(hass, config, async_add_entities, discovery_info=None): +def setup_platform(hass, config, async_add_entities, discovery_info=None): pass diff --git a/custom_components/meross_cloud/light.py b/custom_components/meross_cloud/light.py index 8aba03485e..71c72c8ebd 100644 --- a/custom_components/meross_cloud/light.py +++ b/custom_components/meross_cloud/light.py @@ -180,18 +180,21 @@ def turn_on(self, **kwargs) -> None: async def async_setup_entry(hass, config_entry, async_add_entities): - bulb_devices = [] - manager = hass.data[DOMAIN][MANAGER] # type:MerossManager - bulbs = manager.get_devices_by_kind(GenericBulb) - - for bulb in bulbs: - w = LightEntityWrapper(device=bulb, channel=0) - bulb_devices.append(w) - hass.data[DOMAIN][HA_LIGHT][w.unique_id] = w - + def sync_logic(): + bulb_devices = [] + manager = hass.data[DOMAIN][MANAGER] # type:MerossManager + bulbs = manager.get_devices_by_kind(GenericBulb) + + for bulb in bulbs: + w = LightEntityWrapper(device=bulb, channel=0) + bulb_devices.append(w) + hass.data[DOMAIN][HA_LIGHT][w.unique_id] = w + return bulb_devices + + bulb_devices = await hass.async_add_executor_job(sync_logic) async_add_entities(bulb_devices) -async def async_setup_platform(hass, config, async_add_entities, discovery_info=None): +def setup_platform(hass, config, async_add_entities, discovery_info=None): pass diff --git a/custom_components/meross_cloud/sensor.py b/custom_components/meross_cloud/sensor.py index aee74ac2c8..5f365e62c2 100644 --- a/custom_components/meross_cloud/sensor.py +++ b/custom_components/meross_cloud/sensor.py @@ -137,24 +137,26 @@ def device_info(self): async def async_setup_entry(hass, config_entry, async_add_entities): - sensor_entities = [] - manager = hass.data[DOMAIN][MANAGER] - plugs = manager.get_devices_by_kind(GenericPlug) - - # First, parse power sensors that are embedded into power plugs - for plug in plugs: # type: GenericPlug - if not plug.online: - _LOGGER.warning("The plug %s is offline; it's impossible to determine if it supports any ability" - % plug.name) - elif plug.type.startswith("mss310") or plug.supports_consumption_reading(): - w = PowerSensorWrapper(device=plug) - sensor_entities.append(w) - hass.data[DOMAIN][HA_SENSOR][w.unique_id] = w - - # TODO: Then parse thermostat sensors? - + def sync_logic(): + sensor_entities = [] + manager = hass.data[DOMAIN][MANAGER] + plugs = manager.get_devices_by_kind(GenericPlug) + + # First, parse power sensors that are embedded into power plugs + for plug in plugs: # type: GenericPlug + if not plug.online: + _LOGGER.warning("The plug %s is offline; it's impossible to determine if it supports any ability" + % plug.name) + elif plug.type.startswith("mss310") or plug.supports_consumption_reading(): + w = PowerSensorWrapper(device=plug) + sensor_entities.append(w) + hass.data[DOMAIN][HA_SENSOR][w.unique_id] = w + # TODO: Then parse thermostat sensors? + return sensor_entities + + sensor_entities = await hass.async_add_executor_job(sync_logic) async_add_entities(sensor_entities) -async def async_setup_platform(hass, config, async_add_entities, discovery_info=None): +def setup_platform(hass, config, async_add_entities, discovery_info=None): pass \ No newline at end of file diff --git a/custom_components/meross_cloud/switch.py b/custom_components/meross_cloud/switch.py index b0e7ae0e94..066d243531 100644 --- a/custom_components/meross_cloud/switch.py +++ b/custom_components/meross_cloud/switch.py @@ -3,6 +3,7 @@ from meross_iot.meross_event import DeviceOnlineStatusEvent, DeviceSwitchStatusEvent from .common import DOMAIN, MANAGER, calculate_switch_id, AbstractMerossEntityWrapper, cloud_io, HA_SWITCH import logging +import asyncio _LOGGER = logging.getLogger(__name__) @@ -104,21 +105,24 @@ def turn_on(self, **kwargs) -> None: async def async_setup_entry(hass, config_entry, async_add_entities): - switch_entities = [] - manager = hass.data[DOMAIN][MANAGER] # type:MerossManager - plugs = manager.get_devices_by_kind(GenericPlug) - - for plug in plugs: # type: GenericPlug - # Every Meross plug might have multiple switches onboard. For this reason we need to - # instantiate multiple switch entities for every channel. - for channel_index, channel in enumerate(plug.get_channels()): - w = SwitchEntityWrapper(device=plug, channel=channel_index) - switch_entities.append(w) - hass.data[DOMAIN][HA_SWITCH][w.unique_id] = w - + def sync_logic(): + switch_entities = [] + manager = hass.data[DOMAIN][MANAGER] # type:MerossManager + plugs = manager.get_devices_by_kind(GenericPlug) + + for plug in plugs: # type: GenericPlug + # Every Meross plug might have multiple switches onboard. For this reason we need to + # instantiate multiple switch entities for every channel. + for channel_index, channel in enumerate(plug.get_channels()): + w = SwitchEntityWrapper(device=plug, channel=channel_index) + switch_entities.append(w) + hass.data[DOMAIN][HA_SWITCH][w.unique_id] = w + return switch_entities + + switch_entities = await hass.async_add_executor_job(sync_logic) async_add_entities(switch_entities) -async def async_setup_platform(hass, config, async_add_entities, discovery_info=None): +def setup_platform(hass, config, async_add_entities, discovery_info=None): pass