Prevent spider from doing I/O in the event loop (#43182)
parent
33d5e79301
commit
39493fd3ea
|
@ -2,11 +2,12 @@
|
|||
import asyncio
|
||||
import logging
|
||||
|
||||
from spiderpy.spiderapi import SpiderApi, UnauthorizedException
|
||||
from spiderpy.spiderapi import SpiderApi, SpiderApiException, UnauthorizedException
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant.config_entries import SOURCE_IMPORT
|
||||
from homeassistant.const import CONF_PASSWORD, CONF_SCAN_INTERVAL, CONF_USERNAME
|
||||
from homeassistant.exceptions import ConfigEntryNotReady
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
|
||||
from .const import DEFAULT_SCAN_INTERVAL, DOMAIN, PLATFORMS
|
||||
|
@ -29,16 +30,6 @@ CONFIG_SCHEMA = vol.Schema(
|
|||
)
|
||||
|
||||
|
||||
def _spider_startup_wrapper(entry):
|
||||
"""Startup wrapper for spider."""
|
||||
api = SpiderApi(
|
||||
entry.data[CONF_USERNAME],
|
||||
entry.data[CONF_PASSWORD],
|
||||
entry.data[CONF_SCAN_INTERVAL],
|
||||
)
|
||||
return api
|
||||
|
||||
|
||||
async def async_setup(hass, config):
|
||||
"""Set up a config entry."""
|
||||
hass.data[DOMAIN] = {}
|
||||
|
@ -60,12 +51,20 @@ async def async_setup(hass, config):
|
|||
async def async_setup_entry(hass, entry):
|
||||
"""Set up Spider via config entry."""
|
||||
try:
|
||||
hass.data[DOMAIN][entry.entry_id] = await hass.async_add_executor_job(
|
||||
_spider_startup_wrapper, entry
|
||||
api = await hass.async_add_executor_job(
|
||||
SpiderApi,
|
||||
entry.data[CONF_USERNAME],
|
||||
entry.data[CONF_PASSWORD],
|
||||
entry.data[CONF_SCAN_INTERVAL],
|
||||
)
|
||||
except UnauthorizedException:
|
||||
_LOGGER.error("Can't connect to the Spider API")
|
||||
_LOGGER.error("Authorization failed")
|
||||
return False
|
||||
except SpiderApiException as err:
|
||||
_LOGGER.error("Can't connect to the Spider API: %s", err)
|
||||
raise ConfigEntryNotReady from err
|
||||
|
||||
hass.data[DOMAIN][entry.entry_id] = api
|
||||
|
||||
for component in PLATFORMS:
|
||||
hass.async_create_task(
|
||||
|
|
|
@ -28,9 +28,12 @@ async def async_setup_entry(hass, config, async_add_entities):
|
|||
"""Initialize a Spider thermostat."""
|
||||
api = hass.data[DOMAIN][config.entry_id]
|
||||
|
||||
entities = [SpiderThermostat(api, entity) for entity in api.get_thermostats()]
|
||||
|
||||
async_add_entities(entities)
|
||||
async_add_entities(
|
||||
[
|
||||
SpiderThermostat(api, entity)
|
||||
for entity in await hass.async_add_executor_job(api.get_thermostats)
|
||||
]
|
||||
)
|
||||
|
||||
|
||||
class SpiderThermostat(ClimateEntity):
|
||||
|
|
|
@ -7,10 +7,12 @@ from .const import DOMAIN
|
|||
async def async_setup_entry(hass, config, async_add_entities):
|
||||
"""Initialize a Spider thermostat."""
|
||||
api = hass.data[DOMAIN][config.entry_id]
|
||||
|
||||
entities = [SpiderPowerPlug(api, entity) for entity in api.get_power_plugs()]
|
||||
|
||||
async_add_entities(entities)
|
||||
async_add_entities(
|
||||
[
|
||||
SpiderPowerPlug(api, entity)
|
||||
for entity in await hass.async_add_executor_job(api.get_power_plugs)
|
||||
]
|
||||
)
|
||||
|
||||
|
||||
class SpiderPowerPlug(SwitchEntity):
|
||||
|
|
Loading…
Reference in New Issue