66from homeassistant .const import CONF_HOST , CONF_PORT
77from homeassistant .core import HomeAssistant
88from homeassistant .data_entry_flow import FlowResult
9+ from homeassistant .helpers import device_registry as dr
910import aiohttp
1011import async_timeout
1112
12- DOMAIN = "hyperhdr_control"
13+ from . const import DOMAIN , DEFAULT_PORT
1314
1415class ConfigFlow (config_entries .ConfigFlow , domain = DOMAIN ):
1516 """Handle a config flow for HyperHDR Control."""
1617
1718 VERSION = 1
1819
20+ def __init__ (self ) -> None :
21+ """Initialize the config flow."""
22+ self ._host : str | None = None
23+ self ._port : int | None = None
24+ self ._name : str | None = None
25+
26+ async def async_step_zeroconf (self , discovery_info ) -> FlowResult :
27+ """Handle zeroconf discovery."""
28+ self ._host = discovery_info .host
29+ self ._port = discovery_info .port or DEFAULT_PORT
30+ self ._name = discovery_info .name .replace ("._hyperhdr-http._tcp.local." , "" )
31+
32+ # Check if already configured
33+ await self .async_set_unique_id (f"{ self ._host } :{ self ._port } " )
34+ self ._abort_if_unique_id_configured ()
35+
36+ # Set title for confirmation
37+ self .context ["title_placeholders" ] = {"name" : self ._name }
38+
39+ return await self .async_step_confirm ()
40+
41+ async def async_step_confirm (self , user_input = None ) -> FlowResult :
42+ """Handle user-confirmation of discovered node."""
43+ if user_input is not None :
44+ return await self ._test_connection ()
45+
46+ return self .async_show_form (
47+ step_id = "confirm" ,
48+ description_placeholders = {
49+ "name" : self ._name ,
50+ "host" : self ._host ,
51+ "port" : self ._port ,
52+ },
53+ )
54+
1955 async def async_step_user (
2056 self , user_input : dict [str , any ] | None = None
2157 ) -> FlowResult :
2258 """Handle the initial step."""
2359 errors = {}
2460
2561 if user_input is not None :
26- try :
27- # Test the connection
28- async with aiohttp .ClientSession () as session :
29- url = f"http://{ user_input [CONF_HOST ]} :{ user_input [CONF_PORT ]} /json-rpc"
30- test_request = {
31- "command" : "serverinfo"
32- }
33- async with async_timeout .timeout (10 ):
34- async with session .get (url , params = {"request" : str (test_request )}) as response :
35- if response .status == 200 :
36- return self .async_create_entry (
37- title = f"HyperHDR Control ({ user_input [CONF_HOST ]} )" ,
38- data = user_input
39- )
40- except (aiohttp .ClientError , TimeoutError ):
41- errors ["base" ] = "cannot_connect"
62+ self ._host = user_input [CONF_HOST ]
63+ self ._port = user_input [CONF_PORT ]
64+ return await self ._test_connection ()
4265
4366 return self .async_show_form (
4467 step_id = "user" ,
4568 data_schema = vol .Schema (
4669 {
4770 vol .Required (CONF_HOST ): str ,
48- vol .Required (CONF_PORT , default = 8090 ): int ,
71+ vol .Required (CONF_PORT , default = DEFAULT_PORT ): int ,
4972 }
5073 ),
5174 errors = errors ,
52- )
75+ )
76+
77+ async def _test_connection (self ) -> FlowResult :
78+ """Test the connection to HyperHDR."""
79+ try :
80+ async with aiohttp .ClientSession () as session :
81+ url = f"http://{ self ._host } :{ self ._port } /json-rpc"
82+ test_request = {
83+ "command" : "serverinfo"
84+ }
85+ async with async_timeout .timeout (10 ):
86+ async with session .get (url , params = {"request" : str (test_request )}) as response :
87+ if response .status == 200 :
88+ await self .async_set_unique_id (
89+ f"{ self ._host } :{ self ._port } " , raise_on_progress = False
90+ )
91+ return self .async_create_entry (
92+ title = self ._name or f"HyperHDR ({ self ._host } )" ,
93+ data = {
94+ CONF_HOST : self ._host ,
95+ CONF_PORT : self ._port ,
96+ }
97+ )
98+ except (aiohttp .ClientError , TimeoutError ):
99+ pass
100+
101+ return self .async_abort (reason = "cannot_connect" )
0 commit comments