Skip to content

Commit

Permalink
Fixed bad usage of asyncio directives
Browse files Browse the repository at this point in the history
  • Loading branch information
albertogeniola committed Feb 16, 2020
1 parent 2dc1afd commit d85a1d5
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 59 deletions.
21 changes: 12 additions & 9 deletions custom_components/meross_cloud/climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

23 changes: 13 additions & 10 deletions custom_components/meross_cloud/cover.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
23 changes: 13 additions & 10 deletions custom_components/meross_cloud/light.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

36 changes: 19 additions & 17 deletions custom_components/meross_cloud/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
30 changes: 17 additions & 13 deletions custom_components/meross_cloud/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)
Expand Down Expand Up @@ -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

0 comments on commit d85a1d5

Please sign in to comment.