From b4864e6a8acdb40909fe426e01b4092d76125dfa Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Thu, 5 Jun 2025 06:35:10 +0200 Subject: [PATCH] Move matrix services to separate module (#146161) --- homeassistant/components/matrix/__init__.py | 34 ++---------- homeassistant/components/matrix/const.py | 5 ++ homeassistant/components/matrix/services.py | 61 +++++++++++++++++++++ tests/components/matrix/test_matrix_bot.py | 3 +- 4 files changed, 72 insertions(+), 31 deletions(-) create mode 100644 homeassistant/components/matrix/services.py diff --git a/homeassistant/components/matrix/__init__.py b/homeassistant/components/matrix/__init__.py index 5123436a397..85f08bb4d87 100644 --- a/homeassistant/components/matrix/__init__.py +++ b/homeassistant/components/matrix/__init__.py @@ -44,7 +44,8 @@ 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 +from .const import ATTR_FORMAT, ATTR_IMAGES, CONF_ROOMS_REGEX, DOMAIN, FORMAT_HTML +from .services import register_services _LOGGER = logging.getLogger(__name__) @@ -57,17 +58,11 @@ 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) @@ -117,27 +112,12 @@ CONFIG_SCHEMA = vol.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.data[DOMAIN] = MatrixBot( hass, os.path.join(hass.config.path(), SESSION_FILE), config[CONF_HOMESERVER], @@ -147,14 +127,8 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: 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, - ) + register_services(hass) return True diff --git a/homeassistant/components/matrix/const.py b/homeassistant/components/matrix/const.py index bae53f05727..b4c926409e8 100644 --- a/homeassistant/components/matrix/const.py +++ b/homeassistant/components/matrix/const.py @@ -6,3 +6,8 @@ SERVICE_SEND_MESSAGE = "send_message" FORMAT_HTML = "html" FORMAT_TEXT = "text" + +ATTR_FORMAT = "format" # optional message format +ATTR_IMAGES = "images" # optional images + +CONF_ROOMS_REGEX = "^[!|#][^:]*:.*" diff --git a/homeassistant/components/matrix/services.py b/homeassistant/components/matrix/services.py new file mode 100644 index 00000000000..edd312348d6 --- /dev/null +++ b/homeassistant/components/matrix/services.py @@ -0,0 +1,61 @@ +"""The Matrix bot component.""" + +from __future__ import annotations + +from typing import TYPE_CHECKING + +import voluptuous as vol + +from homeassistant.components.notify import ATTR_DATA, ATTR_MESSAGE, ATTR_TARGET +from homeassistant.core import HomeAssistant, ServiceCall +from homeassistant.helpers import config_validation as cv + +from .const import ( + ATTR_FORMAT, + ATTR_IMAGES, + CONF_ROOMS_REGEX, + DOMAIN, + FORMAT_HTML, + FORMAT_TEXT, + SERVICE_SEND_MESSAGE, +) + +if TYPE_CHECKING: + from . import MatrixBot + + +MESSAGE_FORMATS = [FORMAT_HTML, FORMAT_TEXT] +DEFAULT_MESSAGE_FORMAT = FORMAT_TEXT + + +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 _handle_send_message(call: ServiceCall) -> None: + """Handle the send_message service call.""" + matrix_bot: MatrixBot = call.hass.data[DOMAIN] + await matrix_bot.handle_send_message(call) + + +def register_services(hass: HomeAssistant) -> None: + """Set up the Matrix bot component.""" + + hass.services.async_register( + DOMAIN, + SERVICE_SEND_MESSAGE, + _handle_send_message, + schema=SERVICE_SCHEMA_SEND_MESSAGE, + ) diff --git a/tests/components/matrix/test_matrix_bot.py b/tests/components/matrix/test_matrix_bot.py index fcefd0525c8..6c25d570299 100644 --- a/tests/components/matrix/test_matrix_bot.py +++ b/tests/components/matrix/test_matrix_bot.py @@ -1,6 +1,7 @@ """Configure and test MatrixBot.""" -from homeassistant.components.matrix import DOMAIN, SERVICE_SEND_MESSAGE, MatrixBot +from homeassistant.components.matrix import MatrixBot +from homeassistant.components.matrix.const import DOMAIN, SERVICE_SEND_MESSAGE from homeassistant.components.notify import DOMAIN as NOTIFY_DOMAIN from homeassistant.core import HomeAssistant