1 """Config flow for ScreenLogic."""
3 from __future__
import annotations
8 from screenlogicpy
import ScreenLogicError, discovery
9 from screenlogicpy.const.common
import SL_GATEWAY_IP, SL_GATEWAY_NAME, SL_GATEWAY_PORT
10 from screenlogicpy.requests
import login
11 import voluptuous
as vol
25 from .const
import DEFAULT_SCAN_INTERVAL, DOMAIN, MIN_SCAN_INTERVAL
27 _LOGGER = logging.getLogger(__name__)
29 GATEWAY_SELECT_KEY =
"selected_gateway"
30 GATEWAY_MANUAL_ENTRY =
"manual"
32 PENTAIR_OUI =
"00-C0-33"
36 """Discover gateways and return a dict of them by unique id."""
37 discovered_gateways: dict[str, dict[str, Any]] = {}
39 hosts = await discovery.async_discover()
40 _LOGGER.debug(
"Discovered hosts: %s", hosts)
41 except ScreenLogicError
as ex:
43 return discovered_gateways
46 if (name := host[SL_GATEWAY_NAME]).startswith(
"Pentair:"):
48 discovered_gateways[mac] = host
50 _LOGGER.debug(
"Discovered gateways: %s", discovered_gateways)
51 return discovered_gateways
55 return format_mac(f
"{PENTAIR_OUI}-{name.split(':')[1].strip()}")
59 """Short version of the mac as seen in the app."""
60 return "-".join(mac.split(
":")[3:]).upper()
64 """Derive the gateway name from the mac."""
65 return f
"Pentair: {short_mac(mac)}"
69 """Config flow to setup screen logic devices."""
74 """Initialize ScreenLogic ConfigFlow."""
81 config_entry: ConfigEntry,
82 ) -> ScreenLogicOptionsFlowHandler:
83 """Get the options flow for ScreenLogic."""
87 self, user_input: dict[str, Any] |
None =
None
88 ) -> ConfigFlowResult:
89 """Handle the start of the config flow."""
94 self, discovery_info: dhcp.DhcpServiceInfo
95 ) -> ConfigFlowResult:
96 """Handle dhcp discovery."""
100 updates={CONF_IP_ADDRESS: discovery_info.ip}
103 self.context[
"title_placeholders"] = {
"name": discovery_info.hostname}
107 """Handle the selection of a discovered ScreenLogic gateway."""
109 unconfigured_gateways = {
110 mac: gateway[SL_GATEWAY_NAME]
112 if mac
not in existing
115 if not unconfigured_gateways:
118 errors: dict[str, str] = {}
119 if user_input
is not None:
120 if user_input[GATEWAY_SELECT_KEY] == GATEWAY_MANUAL_ENTRY:
123 mac = user_input[GATEWAY_SELECT_KEY]
130 CONF_IP_ADDRESS: selected_gateway[SL_GATEWAY_IP],
131 CONF_PORT: selected_gateway[SL_GATEWAY_PORT],
136 step_id=
"gateway_select",
137 data_schema=vol.Schema(
139 vol.Required(GATEWAY_SELECT_KEY): vol.In(
141 **unconfigured_gateways,
142 GATEWAY_MANUAL_ENTRY: (
143 "Manually configure a ScreenLogic gateway"
150 description_placeholders={},
154 """Handle the manual entry of a ScreenLogic gateway."""
155 errors: dict[str, str] = {}
159 if user_input
is not None:
160 ip_address = user_input[CONF_IP_ADDRESS]
161 port = user_input[CONF_PORT]
163 mac =
format_mac(await login.async_get_mac_address(ip_address, port))
164 except ScreenLogicError
as ex:
166 errors[CONF_IP_ADDRESS] =
"cannot_connect"
174 CONF_IP_ADDRESS: ip_address,
180 step_id=
"gateway_entry",
181 data_schema=vol.Schema(
183 vol.Required(CONF_IP_ADDRESS, default=ip_address): str,
184 vol.Required(CONF_PORT, default=port): int,
188 description_placeholders={},
193 """Handles the options for the ScreenLogic integration."""
196 """Manage the options."""
197 if user_input
is not None:
203 CONF_SCAN_INTERVAL, DEFAULT_SCAN_INTERVAL
207 data_schema=vol.Schema(
211 default=current_interval,
212 ): vol.All(cv.positive_int, vol.Clamp(min=MIN_SCAN_INTERVAL))
ConfigFlowResult async_step_init(self, user_input=None)
ConfigFlowResult async_step_gateway_select(self, user_input=None)
ScreenLogicOptionsFlowHandler async_get_options_flow(ConfigEntry config_entry)
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
ConfigFlowResult async_step_dhcp(self, dhcp.DhcpServiceInfo discovery_info)
ConfigFlowResult async_step_gateway_entry(self, user_input=None)
None _abort_if_unique_id_configured(self, dict[str, Any]|None updates=None, bool reload_on_update=True, *str error="already_configured")
set[str|None] _async_current_ids(self, bool include_ignore=True)
ConfigEntry|None async_set_unique_id(self, str|None unique_id=None, *bool raise_on_progress=True)
ConfigFlowResult async_create_entry(self, *str title, Mapping[str, Any] data, str|None description=None, Mapping[str, str]|None description_placeholders=None, Mapping[str, Any]|None options=None)
ConfigFlowResult async_show_form(self, *str|None step_id=None, vol.Schema|None data_schema=None, dict[str, str]|None errors=None, Mapping[str, str]|None description_placeholders=None, bool|None last_step=None, str|None preview=None)
ConfigEntry config_entry(self)
None config_entry(self, ConfigEntry value)
_FlowResultT async_show_form(self, *str|None step_id=None, vol.Schema|None data_schema=None, dict[str, str]|None errors=None, Mapping[str, str]|None description_placeholders=None, bool|None last_step=None, str|None preview=None)
_FlowResultT async_create_entry(self, *str|None title=None, Mapping[str, Any] data, str|None description=None, Mapping[str, str]|None description_placeholders=None)
str _extract_mac_from_name(str name)
str name_for_mac(str mac)
dict[str, dict[str, Any]] async_discover_gateways_by_unique_id()