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