Skip to content

Commit 613e2fd

Browse files
authored
Simplify google_mail service actions (home-assistant#146511)
1 parent 0e71ef3 commit 613e2fd

File tree

2 files changed

+49
-48
lines changed

2 files changed

+49
-48
lines changed

homeassistant/components/google_mail/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
2727
"""Set up the Google Mail integration."""
2828
hass.data.setdefault(DOMAIN, {})[DATA_HASS_CONFIG] = config
2929

30-
await async_setup_services(hass)
30+
async_setup_services(hass)
3131

3232
return True
3333

homeassistant/components/google_mail/services.py

Lines changed: 48 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from googleapiclient.http import HttpRequest
99
import voluptuous as vol
1010

11-
from homeassistant.core import HomeAssistant, ServiceCall
11+
from homeassistant.core import HomeAssistant, ServiceCall, callback
1212
from homeassistant.helpers import config_validation as cv
1313
from homeassistant.helpers.service import async_extract_config_entry_ids
1414

@@ -46,56 +46,57 @@
4646
)
4747

4848

49-
async def async_setup_services(hass: HomeAssistant) -> None:
50-
"""Set up services for Google Mail integration."""
49+
async def _extract_gmail_config_entries(
50+
call: ServiceCall,
51+
) -> list[GoogleMailConfigEntry]:
52+
return [
53+
entry
54+
for entry_id in await async_extract_config_entry_ids(call.hass, call)
55+
if (entry := call.hass.config_entries.async_get_entry(entry_id))
56+
and entry.domain == DOMAIN
57+
]
58+
59+
60+
async def _gmail_service(call: ServiceCall) -> None:
61+
"""Call Google Mail service."""
62+
for entry in await _extract_gmail_config_entries(call):
63+
try:
64+
auth = entry.runtime_data
65+
except AttributeError as ex:
66+
raise ValueError(f"Config entry not loaded: {entry.entry_id}") from ex
67+
service = await auth.get_resource()
5168

52-
async def extract_gmail_config_entries(
53-
call: ServiceCall,
54-
) -> list[GoogleMailConfigEntry]:
55-
return [
56-
entry
57-
for entry_id in await async_extract_config_entry_ids(hass, call)
58-
if (entry := hass.config_entries.async_get_entry(entry_id))
59-
and entry.domain == DOMAIN
60-
]
61-
62-
async def gmail_service(call: ServiceCall) -> None:
63-
"""Call Google Mail service."""
64-
for entry in await extract_gmail_config_entries(call):
65-
try:
66-
auth = entry.runtime_data
67-
except AttributeError as ex:
68-
raise ValueError(f"Config entry not loaded: {entry.entry_id}") from ex
69-
service = await auth.get_resource()
70-
71-
_settings = {
72-
"enableAutoReply": call.data[ATTR_ENABLED],
73-
"responseSubject": call.data.get(ATTR_TITLE),
74-
}
75-
if contacts := call.data.get(ATTR_RESTRICT_CONTACTS):
76-
_settings["restrictToContacts"] = contacts
77-
if domain := call.data.get(ATTR_RESTRICT_DOMAIN):
78-
_settings["restrictToDomain"] = domain
79-
if _date := call.data.get(ATTR_START):
80-
_dt = datetime.combine(_date, datetime.min.time())
81-
_settings["startTime"] = _dt.timestamp() * 1000
82-
if _date := call.data.get(ATTR_END):
83-
_dt = datetime.combine(_date, datetime.min.time())
84-
_settings["endTime"] = (_dt + timedelta(days=1)).timestamp() * 1000
85-
if call.data[ATTR_PLAIN_TEXT]:
86-
_settings["responseBodyPlainText"] = call.data[ATTR_MESSAGE]
87-
else:
88-
_settings["responseBodyHtml"] = call.data[ATTR_MESSAGE]
89-
settings: HttpRequest = (
90-
service.users()
91-
.settings()
92-
.updateVacation(userId=ATTR_ME, body=_settings)
93-
)
94-
await hass.async_add_executor_job(settings.execute)
69+
_settings = {
70+
"enableAutoReply": call.data[ATTR_ENABLED],
71+
"responseSubject": call.data.get(ATTR_TITLE),
72+
}
73+
if contacts := call.data.get(ATTR_RESTRICT_CONTACTS):
74+
_settings["restrictToContacts"] = contacts
75+
if domain := call.data.get(ATTR_RESTRICT_DOMAIN):
76+
_settings["restrictToDomain"] = domain
77+
if _date := call.data.get(ATTR_START):
78+
_dt = datetime.combine(_date, datetime.min.time())
79+
_settings["startTime"] = _dt.timestamp() * 1000
80+
if _date := call.data.get(ATTR_END):
81+
_dt = datetime.combine(_date, datetime.min.time())
82+
_settings["endTime"] = (_dt + timedelta(days=1)).timestamp() * 1000
83+
if call.data[ATTR_PLAIN_TEXT]:
84+
_settings["responseBodyPlainText"] = call.data[ATTR_MESSAGE]
85+
else:
86+
_settings["responseBodyHtml"] = call.data[ATTR_MESSAGE]
87+
settings: HttpRequest = (
88+
service.users().settings().updateVacation(userId=ATTR_ME, body=_settings)
89+
)
90+
await call.hass.async_add_executor_job(settings.execute)
91+
92+
93+
@callback
94+
def async_setup_services(hass: HomeAssistant) -> None:
95+
"""Set up services for Google Mail integration."""
9596

9697
hass.services.async_register(
9798
domain=DOMAIN,
9899
service=SERVICE_SET_VACATION,
99100
schema=SERVICE_VACATION_SCHEMA,
100-
service_func=gmail_service,
101+
service_func=_gmail_service,
101102
)

0 commit comments

Comments
 (0)