[go: nahoru, domu]

Skip to content

Commit

Permalink
Reuse the API instance created in config flow
Browse files Browse the repository at this point in the history
  • Loading branch information
qqaatw committed Jun 30, 2023
1 parent c0fb36b commit a6164a6
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 21 deletions.
43 changes: 24 additions & 19 deletions custom_components/jcihitachi_tw/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,30 +132,35 @@ async def async_setup_entry(hass, config_entry):
if config.get(CONF_DEVICES) == []:
config[CONF_DEVICES] = None

api = JciHitachiAWSAPI(
email=config.get(CONF_EMAIL),
password=config.get(CONF_PASSWORD),
device_names=config.get(CONF_DEVICES),
max_retries=config.get(CONF_RETRY),
)
if DOMAIN not in hass.data:
api = JciHitachiAWSAPI(
email=config.get(CONF_EMAIL),
password=config.get(CONF_PASSWORD),
device_names=config.get(CONF_DEVICES),
max_retries=config.get(CONF_RETRY),
)

try:
await hass.async_add_executor_job(api.login)
except AssertionError as err:
_LOGGER.error(f"Assertion check error: {err}")
return False
except RuntimeError as err:
_LOGGER.error(f"Failed to login API: {err}")
return False
try:
await hass.async_add_executor_job(api.login)
except AssertionError as err:
_LOGGER.error(f"Assertion check error: {err}")
return False
except RuntimeError as err:
_LOGGER.error(f"Failed to login API: {err}")
return False

hass.data[DOMAIN] = {}
hass.data[DOMAIN][API] = api
else:
assert API in hass.data[DOMAIN], f"The storage for {DOMAIN} exists but the API instance does not."
_LOGGER.debug("The API instance has been created in config flow, skipping login.")

_LOGGER.debug(f"Backend version: {__version__}")
_LOGGER.debug(f"Thing info: {[thing for thing in api.things.values()]}")
_LOGGER.debug(f"Thing info: {[thing for thing in hass.data[DOMAIN][API].things.values()]}")

hass.data[DOMAIN] = {}
hass.data[DOMAIN][API] = api
hass.data[DOMAIN][UPDATE_DATA] = Queue()
hass.data[DOMAIN][UPDATED_DATA] = api.get_status(legacy=True)
hass.data[DOMAIN][COORDINATOR] = build_coordinator(hass, api)
hass.data[DOMAIN][UPDATED_DATA] = hass.data[DOMAIN][API].get_status(legacy=True)
hass.data[DOMAIN][COORDINATOR] = build_coordinator(hass, hass.data[DOMAIN][API])

# Start jcihitachi components
_LOGGER.debug("Starting JciHitachi components.")
Expand Down
8 changes: 6 additions & 2 deletions custom_components/jcihitachi_tw/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,26 @@
from homeassistant.const import CONF_DEVICES, CONF_EMAIL, CONF_PASSWORD
from JciHitachi.api import JciHitachiAWSAPI

from .const import (CONF_ADD_ANOTHER_DEVICE, CONF_RETRY,
from .const import (API, CONF_ADD_ANOTHER_DEVICE, CONF_RETRY,
CONFIG_FLOW_ADD_DEVICE_SCHEMA, CONFIG_FLOW_SCHEMA, DOMAIN)

_LOGGER = logging.getLogger(__name__)

async def validate_auth(hass, email, password, device_names, max_retries) -> None:
"""Validates JciHitachiAWS account and devices."""

device_names_ = None if device_names == [] else device_names

api = JciHitachiAWSAPI(
email=email,
password=password,
device_names=device_names,
device_names=device_names_,
max_retries=max_retries,
)
await hass.async_add_executor_job(api.login)

hass.data[DOMAIN] = {API: api}


class JciHitachiConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
"""JciHitachi config flow."""
Expand Down

0 comments on commit a6164a6

Please sign in to comment.