22
33from __future__ import annotations
44
5+ from typing import Any
6+
57import voluptuous as vol
68
79from custom_components .panda_status import tools
8- from homeassistant import config_entries
9- from homeassistant .config_entries import OptionsFlowWithReload
10+ from homeassistant .config_entries import ConfigFlow , ConfigFlowResult
1011from homeassistant .const import CONF_NAME , CONF_URL
11- from homeassistant .core import callback
1212from homeassistant .helpers import selector
1313
1414from .const import DOMAIN , LOGGER
15- from .websocket import (
16- PandaStatusWebSocket ,
17- PandaStatusWebsocketCommunicationError ,
18- PandaStatusWebsocketError ,
19- )
15+ from .websocket import PandaStatusWebsocketCommunicationError , PandaStatusWebsocketError
2016
2117OPTIONS_SCHEMA = vol .Schema (
2218 {
3026)
3127
3228
33- async def _test_credentials (url : str ) -> dict :
34- """Validate credentials."""
35- client = PandaStatusWebSocket (url = url , session = None )
36- return await client .async_get_data ()
37-
38-
39- class PandaStatusFlowHandler (config_entries .ConfigFlow , domain = DOMAIN ):
29+ class PandaStatusFlowHandler (ConfigFlow , domain = DOMAIN ):
4030 """Config flow for PandaStatus."""
4131
4232 VERSION = 1
4333 MINOR_VERSION = 1
4434
4535 async def async_step_user (
46- self , user_input : dict | None = None
47- ) -> config_entries . ConfigFlowResult :
36+ self , user_input : dict [ str , Any ] | None = None
37+ ) -> ConfigFlowResult :
4838 """Handle a flow initialized by the user."""
4939 errors = {}
5040 if user_input is not None :
5141 try :
5242 # Try and get initial data to validate the connection
53- initial_data = await _test_credentials (url = user_input [CONF_URL ])
43+ initial_data = await tools .test_credentials (
44+ url = user_input .get (CONF_URL )
45+ )
5446 except PandaStatusWebsocketCommunicationError as exception :
5547 LOGGER .error (exception )
5648 errors ["base" ] = "connection"
@@ -63,7 +55,7 @@ async def async_step_user(
6355
6456 # Use tools to extract printer and device name
6557 printer_name = tools .get_printer_name (initial_data = initial_data )
66- device_name = user_input [ CONF_NAME ] or tools .get_device_name (
58+ device_name = user_input . get ( CONF_NAME , None ) or tools .get_device_name (
6759 initial_data = initial_data
6860 )
6961
@@ -72,7 +64,7 @@ async def async_step_user(
7264 title = device_name ,
7365 description = f"Panda status for { printer_name } " ,
7466 data = {
75- CONF_URL : user_input [ CONF_URL ] ,
67+ CONF_URL : user_input . get ( CONF_URL ) ,
7668 CONF_NAME : device_name ,
7769 },
7870 )
@@ -82,59 +74,3 @@ async def async_step_user(
8274 data_schema = OPTIONS_SCHEMA ,
8375 errors = errors ,
8476 )
85-
86- @staticmethod
87- @callback
88- def async_get_options_flow (
89- config_entry : config_entries .ConfigEntry ,
90- ) -> config_entries .OptionsFlow :
91- """Return the options flow handler for PandaStatus."""
92- return PandaStatusOptionsFlowHandler (config_entry )
93-
94-
95- class PandaStatusOptionsFlowHandler (OptionsFlowWithReload ):
96- """Options flow for PandaStatus."""
97-
98- def __init__ (self , config_entry : config_entries .ConfigEntry ) -> None :
99- """Initialize options flow."""
100- self .config_entry = config_entry
101-
102- async def async_step_init (
103- self , user_input : dict | None = None
104- ) -> config_entries .ConfigFlowResult :
105- """Manage the options."""
106- errors = {}
107- if user_input is not None :
108- try :
109- # Try and get initial data to validate the connection
110- initial_data = await _test_credentials (url = user_input [CONF_URL ])
111- except PandaStatusWebsocketCommunicationError as exception :
112- LOGGER .error (exception )
113- errors ["base" ] = "connection"
114- except PandaStatusWebsocketError as exception :
115- LOGGER .exception (exception )
116- errors ["base" ] = "unknown"
117- else :
118- # Use tools to extract printer and device name
119- printer_name = tools .get_printer_name (initial_data = initial_data )
120- device_name = user_input [CONF_NAME ] or tools .get_device_name (
121- initial_data = initial_data
122- )
123-
124- # If user provided a name, use it; otherwise, use the device name
125- return self .async_create_entry (
126- title = device_name ,
127- description = f"Panda status for { printer_name } " ,
128- data = {
129- CONF_URL : user_input [CONF_URL ],
130- CONF_NAME : device_name ,
131- },
132- )
133-
134- return self .async_show_form (
135- step_id = "init" ,
136- data_schema = self .add_suggested_values_to_schema (
137- OPTIONS_SCHEMA , self .config_entry .options
138- ),
139- errors = errors ,
140- )
0 commit comments