Introduced checkbox to choose whether to setup Cloud API account
This commit is contained in:
@@ -29,9 +29,10 @@ from homeassistant.helpers.event import async_track_time_interval
|
||||
|
||||
from .cloud_api import TuyaCloudApi
|
||||
from .common import TuyaDevice, async_config_entry_by_device_id
|
||||
from .config_flow import config_schema
|
||||
from .config_flow import config_schema, ENTRIES_VERSION
|
||||
from .const import (
|
||||
ATTR_UPDATED_AT,
|
||||
CONF_NO_CLOUD,
|
||||
CONF_PRODUCT_KEY,
|
||||
CONF_USER_ID,
|
||||
DATA_CLOUD,
|
||||
@@ -183,10 +184,10 @@ async def async_setup(hass: HomeAssistant, config: dict):
|
||||
|
||||
async def async_migrate_entry(hass, config_entry: ConfigEntry):
|
||||
"""Migrate old entries merging all of them in one."""
|
||||
new_version = 2
|
||||
new_version = ENTRIES_VERSION
|
||||
stored_entries = hass.config_entries.async_entries(DOMAIN)
|
||||
if config_entry.version == 1:
|
||||
_LOGGER.debug("Migrating config entry from version %s", config_entry.version)
|
||||
stored_entries = hass.config_entries.async_entries(DOMAIN)
|
||||
|
||||
if config_entry.entry_id == stored_entries[0].entry_id:
|
||||
_LOGGER.debug(
|
||||
@@ -198,6 +199,7 @@ async def async_migrate_entry(hass, config_entry: ConfigEntry):
|
||||
new_data[CONF_CLIENT_SECRET] = ""
|
||||
new_data[CONF_USER_ID] = ""
|
||||
new_data[CONF_USERNAME] = DOMAIN
|
||||
new_data[CONF_NO_CLOUD] = True
|
||||
new_data[CONF_DEVICES] = {
|
||||
config_entry.data[CONF_DEVICE_ID]: config_entry.data.copy()
|
||||
}
|
||||
@@ -223,24 +225,41 @@ async def async_migrate_entry(hass, config_entry: ConfigEntry):
|
||||
config_entry.entry_id,
|
||||
new_version,
|
||||
)
|
||||
|
||||
return True
|
||||
|
||||
|
||||
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
|
||||
"""Set up LocalTuya integration from a config entry."""
|
||||
unsub_listener = entry.add_update_listener(update_listener)
|
||||
hass.data[DOMAIN][UNSUB_LISTENER] = unsub_listener
|
||||
if entry.version < ENTRIES_VERSION:
|
||||
_LOGGER.debug(
|
||||
"Skipping setup for entry %s since its version (%s) is old",
|
||||
entry.entry_id,
|
||||
entry.version,
|
||||
)
|
||||
return
|
||||
|
||||
_LOGGER.debug("ASE: %s %s", entry.entry_id, entry.data)
|
||||
|
||||
region = entry.data[CONF_REGION]
|
||||
client_id = entry.data[CONF_CLIENT_ID]
|
||||
secret = entry.data[CONF_CLIENT_SECRET]
|
||||
user_id = entry.data[CONF_USER_ID]
|
||||
tuya_api = TuyaCloudApi(hass, region, client_id, secret, user_id)
|
||||
res = await tuya_api.async_get_access_token()
|
||||
if res != "ok":
|
||||
_LOGGER.error("Cloud API connection failed: %s", res)
|
||||
_LOGGER.info("Cloud API connection succeeded.")
|
||||
res = await tuya_api.async_get_devices_list()
|
||||
no_cloud = True
|
||||
if CONF_NO_CLOUD in entry.data:
|
||||
no_cloud = entry.data.get(CONF_NO_CLOUD)
|
||||
_LOGGER.debug("ASE DC: %s", no_cloud)
|
||||
if no_cloud:
|
||||
_LOGGER.info("Cloud API account not configured.")
|
||||
# wait 1 second to make sure possible migration has finished
|
||||
await asyncio.sleep(1)
|
||||
else:
|
||||
res = await tuya_api.async_get_access_token()
|
||||
if res != "ok":
|
||||
_LOGGER.error("Cloud API connection failed: %s", res)
|
||||
_LOGGER.info("Cloud API connection succeeded.")
|
||||
res = await tuya_api.async_get_devices_list()
|
||||
hass.data[DOMAIN][DATA_CLOUD] = tuya_api
|
||||
|
||||
async def setup_entities(dev_id):
|
||||
@@ -262,6 +281,9 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
|
||||
for dev_id in entry.data[CONF_DEVICES]:
|
||||
hass.async_create_task(setup_entities(dev_id))
|
||||
|
||||
unsub_listener = entry.add_update_listener(update_listener)
|
||||
hass.data[DOMAIN][entry.entry_id] = {UNSUB_LISTENER: unsub_listener}
|
||||
|
||||
return True
|
||||
|
||||
|
||||
@@ -269,7 +291,7 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry):
|
||||
"""Unload a config entry."""
|
||||
platforms = {}
|
||||
|
||||
hass.data[DOMAIN][UNSUB_LISTENER]()
|
||||
hass.data[DOMAIN][entry.entry_id][UNSUB_LISTENER]()
|
||||
for dev_id, device in hass.data[DOMAIN][TUYA_DEVICES].items():
|
||||
if device.connected:
|
||||
await device.close()
|
||||
|
Reference in New Issue
Block a user