1313)
1414import voluptuous as vol
1515
16- from homeassistant .config_entries import ConfigFlow , ConfigFlowResult
16+ from homeassistant .config_entries import (
17+ ConfigEntry ,
18+ ConfigFlow ,
19+ ConfigFlowResult ,
20+ ConfigSubentryFlow ,
21+ SubentryFlowResult ,
22+ )
1723from homeassistant .const import (
1824 CONF_API_KEY ,
1925 CONF_LATITUDE ,
2026 CONF_LOCATION ,
2127 CONF_LONGITUDE ,
22- CONF_METHOD ,
2328)
29+ from homeassistant .core import callback
2430from homeassistant .helpers .aiohttp_client import async_get_clientsession
25- from homeassistant .helpers .selector import (
26- LocationSelector ,
27- SelectSelector ,
28- SelectSelectorConfig ,
29- )
31+ from homeassistant .helpers .selector import LocationSelector
3032
31- from .const import CONF_STATION_NUMBER , DOMAIN
33+ from .const import CONF_STATION_NUMBER , DOMAIN , SUBENTRY_TYPE_STATION
3234
3335_LOGGER = logging .getLogger (__name__ )
3436
@@ -54,18 +56,23 @@ async def get_by_station_number(
5456class WAQIConfigFlow (ConfigFlow , domain = DOMAIN ):
5557 """Handle a config flow for World Air Quality Index (WAQI)."""
5658
57- VERSION = 1
59+ VERSION = 2
5860
59- def __init__ (self ) -> None :
60- """Initialize config flow."""
61- self .data : dict [str , Any ] = {}
61+ @classmethod
62+ @callback
63+ def async_get_supported_subentry_types (
64+ cls , config_entry : ConfigEntry
65+ ) -> dict [str , type [ConfigSubentryFlow ]]:
66+ """Return subentries supported by this handler."""
67+ return {SUBENTRY_TYPE_STATION : StationFlowHandler }
6268
6369 async def async_step_user (
6470 self , user_input : dict [str , Any ] | None = None
6571 ) -> ConfigFlowResult :
6672 """Handle the initial step."""
6773 errors : dict [str , str ] = {}
6874 if user_input is not None :
75+ self ._async_abort_entries_match ({CONF_API_KEY : user_input [CONF_API_KEY ]})
6976 client = WAQIClient (session = async_get_clientsession (self .hass ))
7077 client .authenticate (user_input [CONF_API_KEY ])
7178 try :
@@ -78,35 +85,40 @@ async def async_step_user(
7885 _LOGGER .exception ("Unexpected exception" )
7986 errors ["base" ] = "unknown"
8087 else :
81- self .data = user_input
82- if user_input [CONF_METHOD ] == CONF_MAP :
83- return await self .async_step_map ()
84- return await self .async_step_station_number ()
88+ return self .async_create_entry (
89+ title = "World Air Quality Index" ,
90+ data = {
91+ CONF_API_KEY : user_input [CONF_API_KEY ],
92+ },
93+ )
8594
8695 return self .async_show_form (
8796 step_id = "user" ,
88- data_schema = vol .Schema (
89- {
90- vol .Required (CONF_API_KEY ): str ,
91- vol .Required (CONF_METHOD ): SelectSelector (
92- SelectSelectorConfig (
93- options = [CONF_MAP , CONF_STATION_NUMBER ],
94- translation_key = "method" ,
95- )
96- ),
97- }
98- ),
97+ data_schema = vol .Schema ({vol .Required (CONF_API_KEY ): str }),
9998 errors = errors ,
10099 )
101100
101+
102+ class StationFlowHandler (ConfigSubentryFlow ):
103+ """Handle subentry flow."""
104+
105+ async def async_step_user (
106+ self , user_input : dict [str , Any ] | None = None
107+ ) -> SubentryFlowResult :
108+ """User flow to create a sensor subentry."""
109+ return self .async_show_menu (
110+ step_id = "user" ,
111+ menu_options = ["map" , "station_number" ],
112+ )
113+
102114 async def async_step_map (
103115 self , user_input : dict [str , Any ] | None = None
104- ) -> ConfigFlowResult :
116+ ) -> SubentryFlowResult :
105117 """Add measuring station via map."""
106118 errors : dict [str , str ] = {}
107119 if user_input is not None :
108120 client = WAQIClient (session = async_get_clientsession (self .hass ))
109- client .authenticate (self .data [CONF_API_KEY ])
121+ client .authenticate (self ._get_entry (). data [CONF_API_KEY ])
110122 try :
111123 measuring_station = await client .get_by_coordinates (
112124 user_input [CONF_LOCATION ][CONF_LATITUDE ],
@@ -124,9 +136,7 @@ async def async_step_map(
124136 data_schema = self .add_suggested_values_to_schema (
125137 vol .Schema (
126138 {
127- vol .Required (
128- CONF_LOCATION ,
129- ): LocationSelector (),
139+ vol .Required (CONF_LOCATION ): LocationSelector (),
130140 }
131141 ),
132142 {
@@ -141,12 +151,12 @@ async def async_step_map(
141151
142152 async def async_step_station_number (
143153 self , user_input : dict [str , Any ] | None = None
144- ) -> ConfigFlowResult :
154+ ) -> SubentryFlowResult :
145155 """Add measuring station via station number."""
146156 errors : dict [str , str ] = {}
147157 if user_input is not None :
148158 client = WAQIClient (session = async_get_clientsession (self .hass ))
149- client .authenticate (self .data [CONF_API_KEY ])
159+ client .authenticate (self ._get_entry (). data [CONF_API_KEY ])
150160 station_number = user_input [CONF_STATION_NUMBER ]
151161 measuring_station , errors = await get_by_station_number (
152162 client , abs (station_number )
@@ -160,25 +170,22 @@ async def async_step_station_number(
160170 return await self ._async_create_entry (measuring_station )
161171 return self .async_show_form (
162172 step_id = CONF_STATION_NUMBER ,
163- data_schema = vol .Schema (
164- {
165- vol .Required (
166- CONF_STATION_NUMBER ,
167- ): int ,
168- }
169- ),
173+ data_schema = vol .Schema ({vol .Required (CONF_STATION_NUMBER ): int }),
170174 errors = errors ,
171175 )
172176
173177 async def _async_create_entry (
174178 self , measuring_station : WAQIAirQuality
175- ) -> ConfigFlowResult :
176- await self .async_set_unique_id (str (measuring_station .station_id ))
177- self ._abort_if_unique_id_configured ()
179+ ) -> SubentryFlowResult :
180+ station_id = str (measuring_station .station_id )
181+ for entry in self .hass .config_entries .async_entries (DOMAIN ):
182+ for subentry in entry .subentries .values ():
183+ if subentry .unique_id == station_id :
184+ return self .async_abort (reason = "already_configured" )
178185 return self .async_create_entry (
179186 title = measuring_station .city .name ,
180187 data = {
181- CONF_API_KEY : self .data [CONF_API_KEY ],
182188 CONF_STATION_NUMBER : measuring_station .station_id ,
183189 },
190+ unique_id = station_id ,
184191 )
0 commit comments