11
11
from googleapiclient .http import HttpRequest
12
12
import voluptuous as vol
13
13
14
- from homeassistant .config_entries import ConfigEntry
14
+ from homeassistant .config_entries import ConfigEntry , OptionsFlowWithConfigEntry
15
15
from homeassistant .const import CONF_ACCESS_TOKEN , CONF_TOKEN
16
+ from homeassistant .core import HomeAssistant , callback
16
17
from homeassistant .data_entry_flow import FlowResult
17
18
from homeassistant .helpers import config_entry_oauth2_flow
18
19
from homeassistant .helpers .selector import (
24
25
from .const import CONF_CHANNELS , DEFAULT_ACCESS , DOMAIN , LOGGER
25
26
26
27
28
+ async def get_resource (hass : HomeAssistant , token : str ) -> Resource :
29
+ """Get Youtube resource async."""
30
+
31
+ def _build_resource () -> Resource :
32
+ return build (
33
+ "youtube" ,
34
+ "v3" ,
35
+ credentials = Credentials (token ),
36
+ )
37
+
38
+ return await hass .async_add_executor_job (_build_resource )
39
+
40
+
27
41
class OAuth2FlowHandler (
28
42
config_entry_oauth2_flow .AbstractOAuth2FlowHandler , domain = DOMAIN
29
43
):
@@ -36,6 +50,14 @@ class OAuth2FlowHandler(
36
50
37
51
reauth_entry : ConfigEntry | None = None
38
52
53
+ @staticmethod
54
+ @callback
55
+ def async_get_options_flow (
56
+ config_entry : ConfigEntry ,
57
+ ) -> YouTubeOptionsFlowHandler :
58
+ """Get the options flow for this handler."""
59
+ return YouTubeOptionsFlowHandler (config_entry )
60
+
39
61
@property
40
62
def logger (self ) -> logging .Logger :
41
63
"""Return logger."""
@@ -69,7 +91,7 @@ async def async_step_reauth_confirm(
69
91
async def async_oauth_create_entry (self , data : dict [str , Any ]) -> FlowResult :
70
92
"""Create an entry for the flow, or update existing entry."""
71
93
try :
72
- service = await self ._get_resource ( data [CONF_TOKEN ][CONF_ACCESS_TOKEN ])
94
+ service = await get_resource ( self .hass , data [CONF_TOKEN ][CONF_ACCESS_TOKEN ])
73
95
# pylint: disable=no-member
74
96
own_channel_request : HttpRequest = service .channels ().list (
75
97
part = "snippet" , mine = True
@@ -116,7 +138,9 @@ async def async_step_channels(
116
138
data = self ._data ,
117
139
options = user_input ,
118
140
)
119
- service = await self ._get_resource (self ._data [CONF_TOKEN ][CONF_ACCESS_TOKEN ])
141
+ service = await get_resource (
142
+ self .hass , self ._data [CONF_TOKEN ][CONF_ACCESS_TOKEN ]
143
+ )
120
144
# pylint: disable=no-member
121
145
subscription_request : HttpRequest = service .subscriptions ().list (
122
146
part = "snippet" , mine = True , maxResults = 50
@@ -140,12 +164,46 @@ async def async_step_channels(
140
164
),
141
165
)
142
166
143
- async def _get_resource (self , token : str ) -> Resource :
144
- def _build_resource () -> Resource :
145
- return build (
146
- "youtube" ,
147
- "v3" ,
148
- credentials = Credentials (token ),
149
- )
150
167
151
- return await self .hass .async_add_executor_job (_build_resource )
168
+ class YouTubeOptionsFlowHandler (OptionsFlowWithConfigEntry ):
169
+ """YouTube Options flow handler."""
170
+
171
+ async def async_step_init (
172
+ self , user_input : dict [str , Any ] | None = None
173
+ ) -> FlowResult :
174
+ """Initialize form."""
175
+ if user_input is not None :
176
+ return self .async_create_entry (
177
+ title = self .config_entry .title ,
178
+ data = user_input ,
179
+ )
180
+ service = await get_resource (
181
+ self .hass , self .config_entry .data [CONF_TOKEN ][CONF_ACCESS_TOKEN ]
182
+ )
183
+ # pylint: disable=no-member
184
+ subscription_request : HttpRequest = service .subscriptions ().list (
185
+ part = "snippet" , mine = True , maxResults = 50
186
+ )
187
+ response = await self .hass .async_add_executor_job (subscription_request .execute )
188
+ selectable_channels = [
189
+ SelectOptionDict (
190
+ value = subscription ["snippet" ]["resourceId" ]["channelId" ],
191
+ label = subscription ["snippet" ]["title" ],
192
+ )
193
+ for subscription in response ["items" ]
194
+ ]
195
+ return self .async_show_form (
196
+ step_id = "init" ,
197
+ data_schema = self .add_suggested_values_to_schema (
198
+ vol .Schema (
199
+ {
200
+ vol .Required (CONF_CHANNELS ): SelectSelector (
201
+ SelectSelectorConfig (
202
+ options = selectable_channels , multiple = True
203
+ )
204
+ ),
205
+ }
206
+ ),
207
+ self .options ,
208
+ ),
209
+ )
0 commit comments