-
-
Notifications
You must be signed in to change notification settings - Fork 33.4k
/
Copy path__init__.py
544 lines (470 loc) · 19.3 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
"""The Matrix bot component."""
from __future__ import annotations
import asyncio
from collections.abc import Sequence
import logging
import mimetypes
import os
import re
from typing import Final, NewType, Required, TypedDict
import aiofiles.os
from nio import AsyncClient, Event, MatrixRoom
from nio.events.room_events import RoomMessageText
from nio.responses import (
ErrorResponse,
JoinError,
JoinResponse,
LoginError,
Response,
RoomResolveAliasResponse,
UploadError,
UploadResponse,
WhoamiError,
WhoamiResponse,
)
from PIL import Image
import voluptuous as vol
from homeassistant.components.notify import ATTR_DATA, ATTR_MESSAGE, ATTR_TARGET
from homeassistant.const import (
CONF_NAME,
CONF_PASSWORD,
CONF_USERNAME,
CONF_VERIFY_SSL,
EVENT_HOMEASSISTANT_START,
EVENT_HOMEASSISTANT_STOP,
)
from homeassistant.core import Event as HassEvent, HomeAssistant, ServiceCall
from homeassistant.exceptions import ConfigEntryAuthFailed, HomeAssistantError
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.json import save_json
from homeassistant.helpers.typing import ConfigType
from homeassistant.util.json import JsonObjectType, load_json_object
from .const import DOMAIN, FORMAT_HTML, FORMAT_TEXT, SERVICE_SEND_MESSAGE
_LOGGER = logging.getLogger(__name__)
SESSION_FILE = ".matrix.conf"
CONF_HOMESERVER: Final = "homeserver"
CONF_ROOMS: Final = "rooms"
CONF_COMMANDS: Final = "commands"
CONF_WORD: Final = "word"
CONF_EXPRESSION: Final = "expression"
CONF_USERNAME_REGEX = "^@[^:]*:.*"
CONF_ROOMS_REGEX = "^[!|#][^:]*:.*"
EVENT_MATRIX_COMMAND = "matrix_command"
DEFAULT_CONTENT_TYPE = "application/octet-stream"
MESSAGE_FORMATS = [FORMAT_HTML, FORMAT_TEXT]
DEFAULT_MESSAGE_FORMAT = FORMAT_TEXT
ATTR_FORMAT = "format" # optional message format
ATTR_IMAGES = "images" # optional images
WordCommand = NewType("WordCommand", str)
ExpressionCommand = NewType("ExpressionCommand", re.Pattern)
RoomAlias = NewType("RoomAlias", str) # Starts with "#"
RoomID = NewType("RoomID", str) # Starts with "!"
RoomAnyID = RoomID | RoomAlias
class ConfigCommand(TypedDict, total=False):
"""Corresponds to a single COMMAND_SCHEMA."""
name: Required[str] # CONF_NAME
rooms: list[RoomID] # CONF_ROOMS
word: WordCommand # CONF_WORD
expression: ExpressionCommand # CONF_EXPRESSION
COMMAND_SCHEMA = vol.All(
vol.Schema(
{
vol.Exclusive(CONF_WORD, "trigger"): cv.string,
vol.Exclusive(CONF_EXPRESSION, "trigger"): cv.is_regex,
vol.Required(CONF_NAME): cv.string,
vol.Optional(CONF_ROOMS): vol.All(
cv.ensure_list, [cv.matches_regex(CONF_ROOMS_REGEX)]
),
}
),
cv.has_at_least_one_key(CONF_WORD, CONF_EXPRESSION),
)
CONFIG_SCHEMA = vol.Schema(
{
DOMAIN: vol.Schema(
{
vol.Required(CONF_HOMESERVER): cv.url,
vol.Optional(CONF_VERIFY_SSL, default=True): cv.boolean,
vol.Required(CONF_USERNAME): cv.matches_regex(CONF_USERNAME_REGEX),
vol.Required(CONF_PASSWORD): cv.string,
vol.Optional(CONF_ROOMS, default=[]): vol.All(
cv.ensure_list, [cv.matches_regex(CONF_ROOMS_REGEX)]
),
vol.Optional(CONF_COMMANDS, default=[]): [COMMAND_SCHEMA],
}
)
},
extra=vol.ALLOW_EXTRA,
)
SERVICE_SCHEMA_SEND_MESSAGE = vol.Schema(
{
vol.Required(ATTR_MESSAGE): cv.string,
vol.Optional(ATTR_DATA, default={}): {
vol.Optional(ATTR_FORMAT, default=DEFAULT_MESSAGE_FORMAT): vol.In(
MESSAGE_FORMATS
),
vol.Optional(ATTR_IMAGES): vol.All(cv.ensure_list, [cv.string]),
},
vol.Required(ATTR_TARGET): vol.All(
cv.ensure_list, [cv.matches_regex(CONF_ROOMS_REGEX)]
),
}
)
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
"""Set up the Matrix bot component."""
config = config[DOMAIN]
matrix_bot = MatrixBot(
hass,
os.path.join(hass.config.path(), SESSION_FILE),
config[CONF_HOMESERVER],
config[CONF_VERIFY_SSL],
config[CONF_USERNAME],
config[CONF_PASSWORD],
config[CONF_ROOMS],
config[CONF_COMMANDS],
)
hass.data[DOMAIN] = matrix_bot
hass.services.async_register(
DOMAIN,
SERVICE_SEND_MESSAGE,
matrix_bot.handle_send_message,
schema=SERVICE_SCHEMA_SEND_MESSAGE,
)
return True
class MatrixBot:
"""The Matrix Bot."""
_client: AsyncClient
def __init__(
self,
hass: HomeAssistant,
config_file: str,
homeserver: str,
verify_ssl: bool,
username: str,
password: str,
listening_rooms: list[RoomAnyID],
commands: list[ConfigCommand],
) -> None:
"""Set up the client."""
self.hass = hass
self._session_filepath = config_file
self._access_tokens: JsonObjectType = {}
self._homeserver = homeserver
self._verify_tls = verify_ssl
self._mx_id = username
self._password = password
self._client = AsyncClient(
homeserver=self._homeserver, user=self._mx_id, ssl=self._verify_tls
)
self._listening_rooms: dict[RoomAnyID, RoomID] = {}
self._word_commands: dict[RoomID, dict[WordCommand, ConfigCommand]] = {}
self._expression_commands: dict[RoomID, list[ConfigCommand]] = {}
self._unparsed_commands = commands
async def stop_client(event: HassEvent) -> None:
"""Run once when Home Assistant stops."""
if self._client is not None:
await self._client.close()
self.hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, stop_client)
async def handle_startup(event: HassEvent) -> None:
"""Run once when Home Assistant finished startup."""
self._access_tokens = await self._get_auth_tokens()
await self._login()
await self._resolve_room_aliases(listening_rooms)
self._load_commands(commands)
await self._join_rooms()
# Sync once so that we don't respond to past events.
_LOGGER.debug("Starting initial sync for %s", self._mx_id)
await self._client.sync(timeout=30_000)
_LOGGER.debug("Finished initial sync for %s", self._mx_id)
self._client.add_event_callback(self._handle_room_message, RoomMessageText)
_LOGGER.debug("Starting sync_forever for %s", self._mx_id)
self.hass.async_create_background_task(
self._client.sync_forever(
timeout=30_000,
loop_sleep_time=1_000,
), # milliseconds.
name=f"{self.__class__.__name__}: sync_forever for '{self._mx_id}'",
)
self.hass.bus.async_listen_once(EVENT_HOMEASSISTANT_START, handle_startup)
def _load_commands(self, commands: list[ConfigCommand]) -> None:
for command in commands:
# Set the command for all listening_rooms, unless otherwise specified.
if rooms := command.get(CONF_ROOMS):
command[CONF_ROOMS] = [self._listening_rooms[room] for room in rooms]
else:
command[CONF_ROOMS] = list(self._listening_rooms.values())
# COMMAND_SCHEMA guarantees that exactly one of CONF_WORD and CONF_EXPRESSION are set.
if (word_command := command.get(CONF_WORD)) is not None:
for room_id in command[CONF_ROOMS]:
self._word_commands.setdefault(room_id, {})
self._word_commands[room_id][word_command] = command
else:
for room_id in command[CONF_ROOMS]:
self._expression_commands.setdefault(room_id, [])
self._expression_commands[room_id].append(command)
async def _handle_room_message(self, room: MatrixRoom, message: Event) -> None:
"""Handle a message sent to a Matrix room."""
# Corresponds to message type 'm.text' and NOT other RoomMessage subtypes, like 'm.notice' and 'm.emote'.
if not isinstance(message, RoomMessageText):
return
# Don't respond to our own messages.
if message.sender == self._mx_id:
return
_LOGGER.debug("Handling message: %s", message.body)
room_id = RoomID(room.room_id)
if message.body.startswith("!"):
# Could trigger a single-word command.
pieces = message.body.split()
word = WordCommand(pieces[0].lstrip("!"))
if command := self._word_commands.get(room_id, {}).get(word):
message_data = {
"command": command[CONF_NAME],
"sender": message.sender,
"room": room_id,
"args": pieces[1:],
}
self.hass.bus.async_fire(EVENT_MATRIX_COMMAND, message_data)
# After single-word commands, check all regex commands in the room.
for command in self._expression_commands.get(room_id, []):
match = command[CONF_EXPRESSION].match(message.body)
if not match:
continue
message_data = {
"command": command[CONF_NAME],
"sender": message.sender,
"room": room_id,
"args": match.groupdict(),
}
self.hass.bus.async_fire(EVENT_MATRIX_COMMAND, message_data)
async def _resolve_room_alias(
self, room_alias_or_id: RoomAnyID
) -> dict[RoomAnyID, RoomID]:
"""Resolve a single RoomAlias if needed."""
if room_alias_or_id.startswith("!"):
room_id = RoomID(room_alias_or_id)
_LOGGER.debug("Will listen to room_id '%s'", room_id)
elif room_alias_or_id.startswith("#"):
room_alias = RoomAlias(room_alias_or_id)
resolve_response = await self._client.room_resolve_alias(room_alias)
if isinstance(resolve_response, RoomResolveAliasResponse):
room_id = RoomID(resolve_response.room_id)
_LOGGER.debug(
"Will listen to room_alias '%s' as room_id '%s'",
room_alias_or_id,
room_id,
)
else:
_LOGGER.error(
"Could not resolve '%s' to a room_id: '%s'",
room_alias_or_id,
resolve_response,
)
return {}
# The config schema guarantees it's a valid room alias or id, so room_id is always set.
return {room_alias_or_id: room_id}
async def _resolve_room_aliases(self, listening_rooms: list[RoomAnyID]) -> None:
"""Resolve any RoomAliases into RoomIDs for the purpose of client interactions."""
resolved_rooms = [
self.hass.async_create_task(
self._resolve_room_alias(room_alias_or_id), eager_start=False
)
for room_alias_or_id in listening_rooms
]
for resolved_room in asyncio.as_completed(resolved_rooms):
self._listening_rooms |= await resolved_room
async def _join_room(self, room_id: RoomID, room_alias_or_id: RoomAnyID) -> None:
"""Join a room or do nothing if already joined."""
join_response = await self._client.join(room_id)
if isinstance(join_response, JoinResponse):
_LOGGER.debug("Joined or already in room '%s'", room_alias_or_id)
elif isinstance(join_response, JoinError):
_LOGGER.error(
"Could not join room '%s': %s",
room_alias_or_id,
join_response,
)
async def _join_rooms(self) -> None:
"""Join the Matrix rooms that we listen for commands in."""
rooms = [
self.hass.async_create_task(
self._join_room(room_id, room_alias_or_id), eager_start=False
)
for room_alias_or_id, room_id in self._listening_rooms.items()
]
await asyncio.wait(rooms)
async def _get_auth_tokens(self) -> JsonObjectType:
"""Read sorted authentication tokens from disk."""
try:
return await self.hass.async_add_executor_job(
load_json_object, self._session_filepath
)
except HomeAssistantError as ex:
_LOGGER.warning(
"Loading authentication tokens from file '%s' failed: %s",
self._session_filepath,
str(ex),
)
return {}
async def _store_auth_token(self, token: str) -> None:
"""Store authentication token to session and persistent storage."""
self._access_tokens[self._mx_id] = token
await self.hass.async_add_executor_job(
save_json,
self._session_filepath,
self._access_tokens,
True, # private=True
)
async def _login(self) -> None:
"""Log in to the Matrix homeserver.
Attempts to use the stored access token.
If that fails, then tries using the password.
If that also fails, raises LocalProtocolError.
"""
# If we have an access token
if (token := self._access_tokens.get(self._mx_id)) is not None:
_LOGGER.debug("Restoring login from stored access token")
self._client.restore_login(
user_id=self._client.user_id,
device_id=self._client.device_id,
access_token=token,
)
response = await self._client.whoami()
if isinstance(response, WhoamiError):
_LOGGER.warning(
"Restoring login from access token failed: %s, %s",
response.status_code,
response.message,
)
self._client.access_token = (
"" # Force a soft-logout if the homeserver didn't.
)
elif isinstance(response, WhoamiResponse):
_LOGGER.debug(
"Successfully restored login from access token: user_id '%s', device_id '%s'",
response.user_id,
response.device_id,
)
# If the token login did not succeed
if not self._client.logged_in:
response = await self._client.login(password=self._password)
_LOGGER.debug("Logging in using password")
if isinstance(response, LoginError):
_LOGGER.warning(
"Login by password failed: %s, %s",
response.status_code,
response.message,
)
if not self._client.logged_in:
raise ConfigEntryAuthFailed(
"Login failed, both token and username/password are invalid"
)
await self._store_auth_token(self._client.access_token)
async def _handle_room_send(
self, target_room: RoomAnyID, message_type: str, content: dict
) -> None:
"""Wrap _client.room_send and handle ErrorResponses."""
response: Response = await self._client.room_send(
room_id=self._listening_rooms.get(target_room, target_room),
message_type=message_type,
content=content,
)
if isinstance(response, ErrorResponse):
_LOGGER.error(
"Unable to deliver message to room '%s': %s",
target_room,
response,
)
else:
_LOGGER.debug("Message delivered to room '%s'", target_room)
async def _handle_multi_room_send(
self, target_rooms: Sequence[RoomAnyID], message_type: str, content: dict
) -> None:
"""Wrap _handle_room_send for multiple target_rooms."""
await asyncio.wait(
self.hass.async_create_task(
self._handle_room_send(
target_room=target_room,
message_type=message_type,
content=content,
),
eager_start=False,
)
for target_room in target_rooms
)
async def _send_image(
self, image_path: str, target_rooms: Sequence[RoomAnyID]
) -> None:
"""Upload an image, then send it to all target_rooms."""
_is_allowed_path = await self.hass.async_add_executor_job(
self.hass.config.is_allowed_path, image_path
)
if not _is_allowed_path:
_LOGGER.error("Path not allowed: %s", image_path)
return
# Get required image metadata.
image = await self.hass.async_add_executor_job(Image.open, image_path)
(width, height) = image.size
mime_type = mimetypes.guess_type(image_path)[0]
file_stat = await aiofiles.os.stat(image_path)
_LOGGER.debug("Uploading file from path, %s", image_path)
async with aiofiles.open(image_path, "r+b") as image_file:
response, _ = await self._client.upload(
image_file,
content_type=mime_type,
filename=os.path.basename(image_path),
filesize=file_stat.st_size,
)
if isinstance(response, UploadError):
_LOGGER.error("Unable to upload image to the homeserver: %s", response)
return
if isinstance(response, UploadResponse):
_LOGGER.debug("Successfully uploaded image to the homeserver")
else:
_LOGGER.error(
"Unknown response received when uploading image to homeserver: %s",
response,
)
return
content = {
"body": os.path.basename(image_path),
"info": {
"size": file_stat.st_size,
"mimetype": mime_type,
"w": width,
"h": height,
},
"msgtype": "m.image",
"url": response.content_uri,
}
await self._handle_multi_room_send(
target_rooms=target_rooms, message_type="m.room.message", content=content
)
async def _send_message(
self, message: str, target_rooms: list[RoomAnyID], data: dict | None
) -> None:
"""Send a message to the Matrix server."""
content = {"msgtype": "m.text", "body": message}
if data is not None and data.get(ATTR_FORMAT) == FORMAT_HTML:
content |= {"format": "org.matrix.custom.html", "formatted_body": message}
await self._handle_multi_room_send(
target_rooms=target_rooms, message_type="m.room.message", content=content
)
if (
data is not None
and (image_paths := data.get(ATTR_IMAGES, []))
and len(target_rooms) > 0
):
image_tasks = [
self.hass.async_create_task(
self._send_image(image_path, target_rooms), eager_start=False
)
for image_path in image_paths
]
await asyncio.wait(image_tasks)
async def handle_send_message(self, service: ServiceCall) -> None:
"""Handle the send_message service."""
await self._send_message(
service.data[ATTR_MESSAGE],
service.data[ATTR_TARGET],
service.data.get(ATTR_DATA),
)