Simplify google_mail service actions (#146511)

This commit is contained in:
epenet 2025-06-11 18:19:57 +02:00 committed by GitHub
parent 0e71ef3861
commit 613e2fd4b3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 49 additions and 48 deletions

View File

@ -27,7 +27,7 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
"""Set up the Google Mail integration.""" """Set up the Google Mail integration."""
hass.data.setdefault(DOMAIN, {})[DATA_HASS_CONFIG] = config hass.data.setdefault(DOMAIN, {})[DATA_HASS_CONFIG] = config
await async_setup_services(hass) async_setup_services(hass)
return True return True

View File

@ -8,7 +8,7 @@ from typing import TYPE_CHECKING
from googleapiclient.http import HttpRequest from googleapiclient.http import HttpRequest
import voluptuous as vol import voluptuous as vol
from homeassistant.core import HomeAssistant, ServiceCall from homeassistant.core import HomeAssistant, ServiceCall, callback
from homeassistant.helpers import config_validation as cv from homeassistant.helpers import config_validation as cv
from homeassistant.helpers.service import async_extract_config_entry_ids from homeassistant.helpers.service import async_extract_config_entry_ids
@ -46,22 +46,20 @@ SERVICE_VACATION_SCHEMA = vol.All(
) )
async def async_setup_services(hass: HomeAssistant) -> None: async def _extract_gmail_config_entries(
"""Set up services for Google Mail integration."""
async def extract_gmail_config_entries(
call: ServiceCall, call: ServiceCall,
) -> list[GoogleMailConfigEntry]: ) -> list[GoogleMailConfigEntry]:
return [ return [
entry entry
for entry_id in await async_extract_config_entry_ids(hass, call) for entry_id in await async_extract_config_entry_ids(call.hass, call)
if (entry := hass.config_entries.async_get_entry(entry_id)) if (entry := call.hass.config_entries.async_get_entry(entry_id))
and entry.domain == DOMAIN and entry.domain == DOMAIN
] ]
async def gmail_service(call: ServiceCall) -> None:
async def _gmail_service(call: ServiceCall) -> None:
"""Call Google Mail service.""" """Call Google Mail service."""
for entry in await extract_gmail_config_entries(call): for entry in await _extract_gmail_config_entries(call):
try: try:
auth = entry.runtime_data auth = entry.runtime_data
except AttributeError as ex: except AttributeError as ex:
@ -87,15 +85,18 @@ async def async_setup_services(hass: HomeAssistant) -> None:
else: else:
_settings["responseBodyHtml"] = call.data[ATTR_MESSAGE] _settings["responseBodyHtml"] = call.data[ATTR_MESSAGE]
settings: HttpRequest = ( settings: HttpRequest = (
service.users() service.users().settings().updateVacation(userId=ATTR_ME, body=_settings)
.settings()
.updateVacation(userId=ATTR_ME, body=_settings)
) )
await hass.async_add_executor_job(settings.execute) await call.hass.async_add_executor_job(settings.execute)
@callback
def async_setup_services(hass: HomeAssistant) -> None:
"""Set up services for Google Mail integration."""
hass.services.async_register( hass.services.async_register(
domain=DOMAIN, domain=DOMAIN,
service=SERVICE_SET_VACATION, service=SERVICE_SET_VACATION,
schema=SERVICE_VACATION_SCHEMA, schema=SERVICE_VACATION_SCHEMA,
service_func=gmail_service, service_func=_gmail_service,
) )