mirror of
https://github.com/home-assistant/core.git
synced 2025-07-10 06:47:09 +00:00
Guard cannot connect during Tuya init (#61267)
This commit is contained in:
parent
3223332c1e
commit
ce59ed2a5e
@ -4,6 +4,7 @@ from __future__ import annotations
|
|||||||
import logging
|
import logging
|
||||||
from typing import NamedTuple
|
from typing import NamedTuple
|
||||||
|
|
||||||
|
import requests
|
||||||
from tuya_iot import (
|
from tuya_iot import (
|
||||||
AuthType,
|
AuthType,
|
||||||
TuyaDevice,
|
TuyaDevice,
|
||||||
@ -18,6 +19,7 @@ from homeassistant.components.light import DOMAIN as LIGHT_DOMAIN
|
|||||||
from homeassistant.components.switch import DOMAIN as SWITCH_DOMAIN
|
from homeassistant.components.switch import DOMAIN as SWITCH_DOMAIN
|
||||||
from homeassistant.config_entries import ConfigEntry
|
from homeassistant.config_entries import ConfigEntry
|
||||||
from homeassistant.core import HomeAssistant, callback
|
from homeassistant.core import HomeAssistant, callback
|
||||||
|
from homeassistant.exceptions import ConfigEntryNotReady
|
||||||
from homeassistant.helpers import device_registry as dr, entity_registry as er
|
from homeassistant.helpers import device_registry as dr, entity_registry as er
|
||||||
from homeassistant.helpers.dispatcher import dispatcher_send
|
from homeassistant.helpers.dispatcher import dispatcher_send
|
||||||
|
|
||||||
@ -60,18 +62,6 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|||||||
data.pop(CONF_PROJECT_TYPE)
|
data.pop(CONF_PROJECT_TYPE)
|
||||||
hass.config_entries.async_update_entry(entry, data=data)
|
hass.config_entries.async_update_entry(entry, data=data)
|
||||||
|
|
||||||
success = await _init_tuya_sdk(hass, entry)
|
|
||||||
|
|
||||||
if not success:
|
|
||||||
hass.data[DOMAIN].pop(entry.entry_id)
|
|
||||||
|
|
||||||
if not hass.data[DOMAIN]:
|
|
||||||
hass.data.pop(DOMAIN)
|
|
||||||
|
|
||||||
return bool(success)
|
|
||||||
|
|
||||||
|
|
||||||
async def _init_tuya_sdk(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|
||||||
auth_type = AuthType(entry.data[CONF_AUTH_TYPE])
|
auth_type = AuthType(entry.data[CONF_AUTH_TYPE])
|
||||||
api = TuyaOpenAPI(
|
api = TuyaOpenAPI(
|
||||||
endpoint=entry.data[CONF_ENDPOINT],
|
endpoint=entry.data[CONF_ENDPOINT],
|
||||||
@ -82,6 +72,7 @@ async def _init_tuya_sdk(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|||||||
|
|
||||||
api.set_dev_channel("hass")
|
api.set_dev_channel("hass")
|
||||||
|
|
||||||
|
try:
|
||||||
if auth_type == AuthType.CUSTOM:
|
if auth_type == AuthType.CUSTOM:
|
||||||
response = await hass.async_add_executor_job(
|
response = await hass.async_add_executor_job(
|
||||||
api.connect, entry.data[CONF_USERNAME], entry.data[CONF_PASSWORD]
|
api.connect, entry.data[CONF_USERNAME], entry.data[CONF_PASSWORD]
|
||||||
@ -94,10 +85,11 @@ async def _init_tuya_sdk(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|||||||
entry.data[CONF_COUNTRY_CODE],
|
entry.data[CONF_COUNTRY_CODE],
|
||||||
entry.data[CONF_APP_TYPE],
|
entry.data[CONF_APP_TYPE],
|
||||||
)
|
)
|
||||||
|
except requests.exceptions.RequestException as err:
|
||||||
|
raise ConfigEntryNotReady(err) from err
|
||||||
|
|
||||||
if response.get("success", False) is False:
|
if response.get("success", False) is False:
|
||||||
_LOGGER.error("Tuya login error response: %s", response)
|
raise ConfigEntryNotReady(response)
|
||||||
return False
|
|
||||||
|
|
||||||
tuya_mq = TuyaOpenMQ(api)
|
tuya_mq = TuyaOpenMQ(api)
|
||||||
tuya_mq.start()
|
tuya_mq.start()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user