Move IO out of event loop #2

This commit is contained in:
magico13
2020-11-17 20:27:15 -05:00
parent 5dd132e29c
commit 5f5777a977
3 changed files with 9 additions and 6 deletions

View File

@@ -58,7 +58,8 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
#_LOGGER.info(entry_data)
vue = PyEmVue()
try:
result = vue.login(username=email, password=password)
loop = asyncio.get_event_loop()
result = await loop.run_in_executor(None, vue.login, email, password)
if not result:
raise Exception("Could not authenticate with Emporia API")
except Exception:

View File

@@ -1,6 +1,6 @@
"""Config flow for Emporia Vue integration."""
import logging
import asyncio
import voluptuous as vol
from homeassistant import config_entries, core, exceptions
@@ -30,7 +30,9 @@ class VueHub:
async def authenticate(self, username, password) -> bool:
"""Test if we can authenticate with the host."""
return self.vue.login(username=username, password=password)
loop = asyncio.get_event_loop()
result = await loop.run_in_executor(None, self.vue.login, username, password)
return result
async def validate_input(hass: core.HomeAssistant, data):

View File

@@ -2,6 +2,7 @@
from datetime import timedelta
import logging
import asyncio
import async_timeout
from homeassistant.const import DEVICE_CLASS_POWER, POWER_WATT, ENERGY_WATT_HOUR, ENERGY_KILO_WATT_HOUR
@@ -49,7 +50,8 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
# Note: asyncio.TimeoutError and aiohttp.ClientError are already
# handled by the data update coordinator.
data = {}
channels = vue.get_recent_usage(scale=Scale.MINUTE.value)
loop = asyncio.get_event_loop()
channels = await loop.run_in_executor(None, vue.get_recent_usage, Scale.MINUTE.value)
if channels:
for channel in channels:
id = '{0}-{1}'.format(channel.device_gid, channel.channel_num)
@@ -84,8 +86,6 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
await coordinator.async_refresh()
_LOGGER.warn(coordinator.data)
async_add_entities(
CurrentVuePowerSensor(coordinator, id) for idx, id in enumerate(coordinator.data)
)