@@ -78,86 +78,85 @@ def _read_file_contents(
7878 return results
7979
8080
81- @callback
82- def async_setup_services (hass : HomeAssistant ) -> None :
83- """Register Google Photos services."""
84-
85- async def async_handle_upload (call : ServiceCall ) -> ServiceResponse :
86- """Generate content from text and optionally images."""
87- config_entry : GooglePhotosConfigEntry | None = (
88- hass .config_entries .async_get_entry (call .data [CONF_CONFIG_ENTRY_ID ])
81+ async def _async_handle_upload (call : ServiceCall ) -> ServiceResponse :
82+ """Generate content from text and optionally images."""
83+ config_entry : GooglePhotosConfigEntry | None = (
84+ call .hass .config_entries .async_get_entry (call .data [CONF_CONFIG_ENTRY_ID ])
85+ )
86+ if not config_entry :
87+ raise ServiceValidationError (
88+ translation_domain = DOMAIN ,
89+ translation_key = "integration_not_found" ,
90+ translation_placeholders = {"target" : DOMAIN },
8991 )
90- if not config_entry :
91- raise ServiceValidationError (
92- translation_domain = DOMAIN ,
93- translation_key = "integration_not_found" ,
94- translation_placeholders = {"target" : DOMAIN },
95- )
96- scopes = config_entry .data ["token" ]["scope" ].split (" " )
97- if UPLOAD_SCOPE not in scopes :
98- raise HomeAssistantError (
99- translation_domain = DOMAIN ,
100- translation_key = "missing_upload_permission" ,
101- translation_placeholders = {"target" : DOMAIN },
102- )
103- coordinator = config_entry .runtime_data
104- client_api = coordinator .client
105- upload_tasks = []
106- file_results = await hass .async_add_executor_job (
107- _read_file_contents , hass , call .data [CONF_FILENAME ]
92+ scopes = config_entry .data ["token" ]["scope" ].split (" " )
93+ if UPLOAD_SCOPE not in scopes :
94+ raise HomeAssistantError (
95+ translation_domain = DOMAIN ,
96+ translation_key = "missing_upload_permission" ,
97+ translation_placeholders = {"target" : DOMAIN },
10898 )
99+ coordinator = config_entry .runtime_data
100+ client_api = coordinator .client
101+ upload_tasks = []
102+ file_results = await call .hass .async_add_executor_job (
103+ _read_file_contents , call .hass , call .data [CONF_FILENAME ]
104+ )
109105
110- album = call .data [CONF_ALBUM ]
111- try :
112- album_id = await coordinator .get_or_create_album (album )
113- except GooglePhotosApiError as err :
114- raise HomeAssistantError (
115- translation_domain = DOMAIN ,
116- translation_key = "create_album_error" ,
117- translation_placeholders = {"message" : str (err )},
118- ) from err
119-
120- for mime_type , content in file_results :
121- upload_tasks .append (client_api .upload_content (content , mime_type ))
122- try :
123- upload_results = await asyncio .gather (* upload_tasks )
124- except GooglePhotosApiError as err :
125- raise HomeAssistantError (
126- translation_domain = DOMAIN ,
127- translation_key = "upload_error" ,
128- translation_placeholders = {"message" : str (err )},
129- ) from err
130- try :
131- upload_result = await client_api .create_media_items (
132- [
133- NewMediaItem (
134- SimpleMediaItem (upload_token = upload_result .upload_token )
135- )
136- for upload_result in upload_results
137- ],
138- album_id = album_id ,
139- )
140- except GooglePhotosApiError as err :
141- raise HomeAssistantError (
142- translation_domain = DOMAIN ,
143- translation_key = "api_error" ,
144- translation_placeholders = {"message" : str (err )},
145- ) from err
146- if call .return_response :
147- return {
148- "media_items" : [
149- {"media_item_id" : item_result .media_item .id }
150- for item_result in upload_result .new_media_item_results
151- if item_result .media_item and item_result .media_item .id
152- ],
153- "album_id" : album_id ,
154- }
155- return None
106+ album = call .data [CONF_ALBUM ]
107+ try :
108+ album_id = await coordinator .get_or_create_album (album )
109+ except GooglePhotosApiError as err :
110+ raise HomeAssistantError (
111+ translation_domain = DOMAIN ,
112+ translation_key = "create_album_error" ,
113+ translation_placeholders = {"message" : str (err )},
114+ ) from err
115+
116+ for mime_type , content in file_results :
117+ upload_tasks .append (client_api .upload_content (content , mime_type ))
118+ try :
119+ upload_results = await asyncio .gather (* upload_tasks )
120+ except GooglePhotosApiError as err :
121+ raise HomeAssistantError (
122+ translation_domain = DOMAIN ,
123+ translation_key = "upload_error" ,
124+ translation_placeholders = {"message" : str (err )},
125+ ) from err
126+ try :
127+ upload_result = await client_api .create_media_items (
128+ [
129+ NewMediaItem (SimpleMediaItem (upload_token = upload_result .upload_token ))
130+ for upload_result in upload_results
131+ ],
132+ album_id = album_id ,
133+ )
134+ except GooglePhotosApiError as err :
135+ raise HomeAssistantError (
136+ translation_domain = DOMAIN ,
137+ translation_key = "api_error" ,
138+ translation_placeholders = {"message" : str (err )},
139+ ) from err
140+ if call .return_response :
141+ return {
142+ "media_items" : [
143+ {"media_item_id" : item_result .media_item .id }
144+ for item_result in upload_result .new_media_item_results
145+ if item_result .media_item and item_result .media_item .id
146+ ],
147+ "album_id" : album_id ,
148+ }
149+ return None
150+
151+
152+ @callback
153+ def async_setup_services (hass : HomeAssistant ) -> None :
154+ """Register Google Photos services."""
156155
157156 hass .services .async_register (
158157 DOMAIN ,
159158 UPLOAD_SERVICE ,
160- async_handle_upload ,
159+ _async_handle_upload ,
161160 schema = UPLOAD_SERVICE_SCHEMA ,
162161 supports_response = SupportsResponse .OPTIONAL ,
163162 )
0 commit comments