2022-02-05 00:20:21 +00:00
|
|
|
"""Config flow for WiZ Platform."""
|
2022-02-05 15:23:19 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2022-02-05 00:20:21 +00:00
|
|
|
import logging
|
2022-02-05 15:23:19 +00:00
|
|
|
from typing import Any
|
2022-02-05 00:20:21 +00:00
|
|
|
|
|
|
|
from pywizlight import wizlight
|
|
|
|
from pywizlight.exceptions import WizLightConnectionError, WizLightTimeOutError
|
|
|
|
import voluptuous as vol
|
|
|
|
|
|
|
|
from homeassistant import config_entries
|
2022-02-05 15:23:19 +00:00
|
|
|
from homeassistant.const import CONF_HOST
|
|
|
|
from homeassistant.data_entry_flow import FlowResult
|
2022-02-05 00:20:21 +00:00
|
|
|
|
|
|
|
from .const import DEFAULT_NAME, DOMAIN
|
2022-02-05 15:23:19 +00:00
|
|
|
from .utils import _short_mac
|
2022-02-05 00:20:21 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
|
|
|
"""Handle a config flow for WiZ."""
|
|
|
|
|
|
|
|
VERSION = 1
|
|
|
|
|
2022-02-05 15:23:19 +00:00
|
|
|
async def async_step_user(
|
|
|
|
self, user_input: dict[str, Any] | None = None
|
|
|
|
) -> FlowResult:
|
2022-02-05 00:20:21 +00:00
|
|
|
"""Handle a flow initialized by the user."""
|
|
|
|
errors = {}
|
|
|
|
if user_input is not None:
|
|
|
|
bulb = wizlight(user_input[CONF_HOST])
|
|
|
|
try:
|
|
|
|
mac = await bulb.getMac()
|
2022-02-05 15:23:19 +00:00
|
|
|
bulbtype = await bulb.get_bulbtype()
|
2022-02-05 00:20:21 +00:00
|
|
|
except WizLightTimeOutError:
|
|
|
|
errors["base"] = "bulb_time_out"
|
|
|
|
except ConnectionRefusedError:
|
|
|
|
errors["base"] = "cannot_connect"
|
|
|
|
except WizLightConnectionError:
|
|
|
|
errors["base"] = "no_wiz_light"
|
|
|
|
except Exception: # pylint: disable=broad-except
|
|
|
|
_LOGGER.exception("Unexpected exception")
|
|
|
|
errors["base"] = "unknown"
|
|
|
|
else:
|
|
|
|
await self.async_set_unique_id(mac)
|
2022-02-05 15:23:19 +00:00
|
|
|
self._abort_if_unique_id_configured(
|
|
|
|
updates={CONF_HOST: user_input[CONF_HOST]}
|
|
|
|
)
|
|
|
|
bulb_type = bulbtype.bulb_type.value if bulbtype else "Unknown"
|
|
|
|
name = f"{DEFAULT_NAME} {bulb_type} {_short_mac(mac)}"
|
2022-02-05 00:20:21 +00:00
|
|
|
return self.async_create_entry(
|
2022-02-05 15:23:19 +00:00
|
|
|
title=name,
|
|
|
|
data=user_input,
|
2022-02-05 00:20:21 +00:00
|
|
|
)
|
2022-02-05 15:23:19 +00:00
|
|
|
|
2022-02-05 00:20:21 +00:00
|
|
|
return self.async_show_form(
|
2022-02-05 15:23:19 +00:00
|
|
|
step_id="user",
|
|
|
|
data_schema=vol.Schema({vol.Required(CONF_HOST): str}),
|
|
|
|
errors=errors,
|
2022-02-05 00:20:21 +00:00
|
|
|
)
|