mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 13:17:32 +00:00
Simplify google_mail service actions (#146511)
This commit is contained in:
parent
0e71ef3861
commit
613e2fd4b3
@ -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
|
||||||
|
|
||||||
|
@ -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,56 +46,57 @@ SERVICE_VACATION_SCHEMA = vol.All(
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_services(hass: HomeAssistant) -> None:
|
async def _extract_gmail_config_entries(
|
||||||
|
call: ServiceCall,
|
||||||
|
) -> list[GoogleMailConfigEntry]:
|
||||||
|
return [
|
||||||
|
entry
|
||||||
|
for entry_id in await async_extract_config_entry_ids(call.hass, call)
|
||||||
|
if (entry := call.hass.config_entries.async_get_entry(entry_id))
|
||||||
|
and entry.domain == DOMAIN
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
|
async def _gmail_service(call: ServiceCall) -> None:
|
||||||
|
"""Call Google Mail service."""
|
||||||
|
for entry in await _extract_gmail_config_entries(call):
|
||||||
|
try:
|
||||||
|
auth = entry.runtime_data
|
||||||
|
except AttributeError as ex:
|
||||||
|
raise ValueError(f"Config entry not loaded: {entry.entry_id}") from ex
|
||||||
|
service = await auth.get_resource()
|
||||||
|
|
||||||
|
_settings = {
|
||||||
|
"enableAutoReply": call.data[ATTR_ENABLED],
|
||||||
|
"responseSubject": call.data.get(ATTR_TITLE),
|
||||||
|
}
|
||||||
|
if contacts := call.data.get(ATTR_RESTRICT_CONTACTS):
|
||||||
|
_settings["restrictToContacts"] = contacts
|
||||||
|
if domain := call.data.get(ATTR_RESTRICT_DOMAIN):
|
||||||
|
_settings["restrictToDomain"] = domain
|
||||||
|
if _date := call.data.get(ATTR_START):
|
||||||
|
_dt = datetime.combine(_date, datetime.min.time())
|
||||||
|
_settings["startTime"] = _dt.timestamp() * 1000
|
||||||
|
if _date := call.data.get(ATTR_END):
|
||||||
|
_dt = datetime.combine(_date, datetime.min.time())
|
||||||
|
_settings["endTime"] = (_dt + timedelta(days=1)).timestamp() * 1000
|
||||||
|
if call.data[ATTR_PLAIN_TEXT]:
|
||||||
|
_settings["responseBodyPlainText"] = call.data[ATTR_MESSAGE]
|
||||||
|
else:
|
||||||
|
_settings["responseBodyHtml"] = call.data[ATTR_MESSAGE]
|
||||||
|
settings: HttpRequest = (
|
||||||
|
service.users().settings().updateVacation(userId=ATTR_ME, body=_settings)
|
||||||
|
)
|
||||||
|
await call.hass.async_add_executor_job(settings.execute)
|
||||||
|
|
||||||
|
|
||||||
|
@callback
|
||||||
|
def async_setup_services(hass: HomeAssistant) -> None:
|
||||||
"""Set up services for Google Mail integration."""
|
"""Set up services for Google Mail integration."""
|
||||||
|
|
||||||
async def extract_gmail_config_entries(
|
|
||||||
call: ServiceCall,
|
|
||||||
) -> list[GoogleMailConfigEntry]:
|
|
||||||
return [
|
|
||||||
entry
|
|
||||||
for entry_id in await async_extract_config_entry_ids(hass, call)
|
|
||||||
if (entry := hass.config_entries.async_get_entry(entry_id))
|
|
||||||
and entry.domain == DOMAIN
|
|
||||||
]
|
|
||||||
|
|
||||||
async def gmail_service(call: ServiceCall) -> None:
|
|
||||||
"""Call Google Mail service."""
|
|
||||||
for entry in await extract_gmail_config_entries(call):
|
|
||||||
try:
|
|
||||||
auth = entry.runtime_data
|
|
||||||
except AttributeError as ex:
|
|
||||||
raise ValueError(f"Config entry not loaded: {entry.entry_id}") from ex
|
|
||||||
service = await auth.get_resource()
|
|
||||||
|
|
||||||
_settings = {
|
|
||||||
"enableAutoReply": call.data[ATTR_ENABLED],
|
|
||||||
"responseSubject": call.data.get(ATTR_TITLE),
|
|
||||||
}
|
|
||||||
if contacts := call.data.get(ATTR_RESTRICT_CONTACTS):
|
|
||||||
_settings["restrictToContacts"] = contacts
|
|
||||||
if domain := call.data.get(ATTR_RESTRICT_DOMAIN):
|
|
||||||
_settings["restrictToDomain"] = domain
|
|
||||||
if _date := call.data.get(ATTR_START):
|
|
||||||
_dt = datetime.combine(_date, datetime.min.time())
|
|
||||||
_settings["startTime"] = _dt.timestamp() * 1000
|
|
||||||
if _date := call.data.get(ATTR_END):
|
|
||||||
_dt = datetime.combine(_date, datetime.min.time())
|
|
||||||
_settings["endTime"] = (_dt + timedelta(days=1)).timestamp() * 1000
|
|
||||||
if call.data[ATTR_PLAIN_TEXT]:
|
|
||||||
_settings["responseBodyPlainText"] = call.data[ATTR_MESSAGE]
|
|
||||||
else:
|
|
||||||
_settings["responseBodyHtml"] = call.data[ATTR_MESSAGE]
|
|
||||||
settings: HttpRequest = (
|
|
||||||
service.users()
|
|
||||||
.settings()
|
|
||||||
.updateVacation(userId=ATTR_ME, body=_settings)
|
|
||||||
)
|
|
||||||
await hass.async_add_executor_job(settings.execute)
|
|
||||||
|
|
||||||
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,
|
||||||
)
|
)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user