|
8 | 8 | from googleapiclient.http import HttpRequest |
9 | 9 | import voluptuous as vol |
10 | 10 |
|
11 | | -from homeassistant.core import HomeAssistant, ServiceCall |
| 11 | +from homeassistant.core import HomeAssistant, ServiceCall, callback |
12 | 12 | from homeassistant.helpers import config_validation as cv |
13 | 13 | from homeassistant.helpers.service import async_extract_config_entry_ids |
14 | 14 |
|
|
46 | 46 | ) |
47 | 47 |
|
48 | 48 |
|
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() |
51 | 68 |
|
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.""" |
95 | 96 |
|
96 | 97 | hass.services.async_register( |
97 | 98 | domain=DOMAIN, |
98 | 99 | service=SERVICE_SET_VACATION, |
99 | 100 | schema=SERVICE_VACATION_SCHEMA, |
100 | | - service_func=gmail_service, |
| 101 | + service_func=_gmail_service, |
101 | 102 | ) |
0 commit comments