|
| 1 | +"""Config flow for Droplet integration.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +from typing import Any |
| 6 | + |
| 7 | +from pydroplet.droplet import DropletConnection, DropletDiscovery |
| 8 | +import voluptuous as vol |
| 9 | + |
| 10 | +from homeassistant.config_entries import ConfigFlow, ConfigFlowResult |
| 11 | +from homeassistant.const import CONF_CODE, CONF_DEVICE_ID, CONF_IP_ADDRESS, CONF_PORT |
| 12 | +from homeassistant.helpers.aiohttp_client import async_get_clientsession |
| 13 | +from homeassistant.helpers.service_info.zeroconf import ZeroconfServiceInfo |
| 14 | + |
| 15 | +from .const import DOMAIN |
| 16 | + |
| 17 | + |
| 18 | +class DropletConfigFlow(ConfigFlow, domain=DOMAIN): |
| 19 | + """Handle Droplet config flow.""" |
| 20 | + |
| 21 | + _droplet_discovery: DropletDiscovery |
| 22 | + |
| 23 | + async def async_step_zeroconf( |
| 24 | + self, discovery_info: ZeroconfServiceInfo |
| 25 | + ) -> ConfigFlowResult: |
| 26 | + """Handle zeroconf discovery.""" |
| 27 | + self._droplet_discovery = DropletDiscovery( |
| 28 | + discovery_info.host, |
| 29 | + discovery_info.port, |
| 30 | + discovery_info.name, |
| 31 | + ) |
| 32 | + if not self._droplet_discovery.is_valid(): |
| 33 | + return self.async_abort(reason="invalid_discovery_info") |
| 34 | + |
| 35 | + # In this case, device ID was part of the zeroconf discovery info |
| 36 | + device_id: str = await self._droplet_discovery.get_device_id() |
| 37 | + await self.async_set_unique_id(device_id) |
| 38 | + |
| 39 | + self._abort_if_unique_id_configured( |
| 40 | + updates={CONF_IP_ADDRESS: self._droplet_discovery.host}, |
| 41 | + ) |
| 42 | + |
| 43 | + self.context.update({"title_placeholders": {"name": device_id}}) |
| 44 | + return await self.async_step_confirm() |
| 45 | + |
| 46 | + async def async_step_confirm( |
| 47 | + self, user_input: dict[str, Any] | None = None |
| 48 | + ) -> ConfigFlowResult: |
| 49 | + """Confirm the setup.""" |
| 50 | + errors: dict[str, str] = {} |
| 51 | + device_id: str = await self._droplet_discovery.get_device_id() |
| 52 | + if user_input is not None: |
| 53 | + # Test if we can connect before returning |
| 54 | + session = async_get_clientsession(self.hass) |
| 55 | + if await self._droplet_discovery.try_connect( |
| 56 | + session, user_input[CONF_CODE] |
| 57 | + ): |
| 58 | + device_data = { |
| 59 | + CONF_IP_ADDRESS: self._droplet_discovery.host, |
| 60 | + CONF_PORT: self._droplet_discovery.port, |
| 61 | + CONF_DEVICE_ID: device_id, |
| 62 | + CONF_CODE: user_input[CONF_CODE], |
| 63 | + } |
| 64 | + |
| 65 | + return self.async_create_entry( |
| 66 | + title=device_id, |
| 67 | + data=device_data, |
| 68 | + ) |
| 69 | + errors["base"] = "cannot_connect" |
| 70 | + return self.async_show_form( |
| 71 | + step_id="confirm", |
| 72 | + data_schema=vol.Schema( |
| 73 | + { |
| 74 | + vol.Required(CONF_CODE): str, |
| 75 | + } |
| 76 | + ), |
| 77 | + description_placeholders={ |
| 78 | + "device_name": device_id, |
| 79 | + }, |
| 80 | + errors=errors, |
| 81 | + ) |
| 82 | + |
| 83 | + async def async_step_user( |
| 84 | + self, user_input: dict[str, Any] | None = None |
| 85 | + ) -> ConfigFlowResult: |
| 86 | + """Handle a flow initialized by the user.""" |
| 87 | + errors: dict[str, str] = {} |
| 88 | + if user_input is not None: |
| 89 | + self._droplet_discovery = DropletDiscovery( |
| 90 | + user_input[CONF_IP_ADDRESS], DropletConnection.DEFAULT_PORT, "" |
| 91 | + ) |
| 92 | + session = async_get_clientsession(self.hass) |
| 93 | + if await self._droplet_discovery.try_connect( |
| 94 | + session, user_input[CONF_CODE] |
| 95 | + ) and (device_id := await self._droplet_discovery.get_device_id()): |
| 96 | + device_data = { |
| 97 | + CONF_IP_ADDRESS: self._droplet_discovery.host, |
| 98 | + CONF_PORT: self._droplet_discovery.port, |
| 99 | + CONF_DEVICE_ID: device_id, |
| 100 | + CONF_CODE: user_input[CONF_CODE], |
| 101 | + } |
| 102 | + await self.async_set_unique_id(device_id, raise_on_progress=False) |
| 103 | + self._abort_if_unique_id_configured( |
| 104 | + description_placeholders={CONF_DEVICE_ID: device_id}, |
| 105 | + ) |
| 106 | + |
| 107 | + return self.async_create_entry( |
| 108 | + title=device_id, |
| 109 | + data=device_data, |
| 110 | + ) |
| 111 | + errors["base"] = "cannot_connect" |
| 112 | + return self.async_show_form( |
| 113 | + step_id="user", |
| 114 | + data_schema=vol.Schema( |
| 115 | + {vol.Required(CONF_IP_ADDRESS): str, vol.Required(CONF_CODE): str} |
| 116 | + ), |
| 117 | + errors=errors, |
| 118 | + ) |
0 commit comments