homeassistant-vorwerk/config_flow.py

128 lines
4.0 KiB
Python
Raw Normal View History

2021-03-20 16:11:04 +01:00
"""Config flow to configure Vorwerk integration."""
2021-04-27 11:12:23 +02:00
from __future__ import annotations
2021-03-20 16:11:04 +01:00
import logging
2021-04-27 11:12:23 +02:00
from typing import Any
2021-03-20 16:11:04 +01:00
from pybotvac.exceptions import NeatoException
from requests.models import HTTPError
import voluptuous as vol
from homeassistant import config_entries
from homeassistant.const import CONF_CODE, CONF_EMAIL, CONF_TOKEN
2021-04-25 20:30:12 +02:00
from . import api
2021-03-20 16:11:04 +01:00
# pylint: disable=unused-import
from .const import (
VORWERK_DOMAIN,
VORWERK_ROBOT_ENDPOINT,
VORWERK_ROBOT_NAME,
VORWERK_ROBOT_SECRET,
VORWERK_ROBOT_SERIAL,
VORWERK_ROBOT_TRAITS,
VORWERK_ROBOTS,
)
DOCS_URL = "https://www.home-assistant.io/integrations/vorwerk"
_LOGGER = logging.getLogger(__name__)
class VorwerkConfigFlow(config_entries.ConfigFlow, domain=VORWERK_DOMAIN):
"""Vorwerk integration config flow."""
VERSION = 1
CONNECTION_CLASS = config_entries.CONN_CLASS_CLOUD_POLL
def __init__(self):
"""Initialize the config flow."""
2021-04-27 11:12:23 +02:00
self._email: str | None = None
2021-04-25 20:30:12 +02:00
self._session = api.VorwerkSession()
2021-03-20 16:11:04 +01:00
async def async_step_user(self, user_input=None):
"""Step when user initializes a integration."""
if user_input is not None:
self._email = user_input.get(CONF_EMAIL)
if self._email:
await self.async_set_unique_id(self._email)
self._abort_if_unique_id_configured()
return await self.async_step_code()
return self.async_show_form(
step_id="user",
data_schema=vol.Schema(
{
vol.Required(CONF_EMAIL): str,
}
),
description_placeholders={"docs_url": DOCS_URL},
)
2021-03-20 16:14:52 +01:00
async def async_step_code(
2021-04-27 11:12:23 +02:00
self, user_input: dict[str, Any] = None
) -> dict[str, Any]:
2021-03-20 16:11:04 +01:00
"""Step when user enters OTP Code from email."""
assert self._email is not None # typing
errors = {}
code = user_input.get(CONF_CODE) if user_input else None
if code:
try:
2021-04-25 20:30:12 +02:00
robots = await self.hass.async_add_executor_job(
self._get_robots, self._email, code
)
2021-03-20 16:11:04 +01:00
return self.async_create_entry(
title=self._email,
data={
CONF_EMAIL: self._email,
CONF_TOKEN: self._session.token,
VORWERK_ROBOTS: robots,
},
)
except (HTTPError, NeatoException):
errors["base"] = "invalid_auth"
2021-04-25 20:30:12 +02:00
await self.hass.async_add_executor_job(
self._session.send_email_otp, self._email
)
2021-03-20 16:11:04 +01:00
return self.async_show_form(
step_id="code",
data_schema=vol.Schema(
{
vol.Required(CONF_CODE): str,
}
),
description_placeholders={"docs_url": DOCS_URL},
errors=errors,
)
2021-04-27 11:12:23 +02:00
async def async_step_import(self, user_input: dict[str, Any]) -> dict[str, Any]:
2021-03-20 16:11:04 +01:00
"""Import a config flow from configuration."""
unique_id = "from configuration"
data = {VORWERK_ROBOTS: user_input}
await self.async_set_unique_id(unique_id)
self._abort_if_unique_id_configured(data)
_LOGGER.info("Creating new Vorwerk robot config entry")
return self.async_create_entry(
title="from configuration",
data=data,
)
2021-04-25 20:30:12 +02:00
def _get_robots(self, email: str, code: str):
2021-03-20 16:11:04 +01:00
"""Fetch the robot list from vorwerk."""
self._session.fetch_token_passwordless(email, code)
return [
{
VORWERK_ROBOT_NAME: robot["name"],
VORWERK_ROBOT_SERIAL: robot["serial"],
VORWERK_ROBOT_SECRET: robot["secret_key"],
VORWERK_ROBOT_TRAITS: robot["traits"],
VORWERK_ROBOT_ENDPOINT: robot["nucleo_url"],
}
for robot in self._session.get("users/me/robots").json()
]