mirror of
https://github.com/home-assistant/core.git
synced 2025-07-17 18:27:09 +00:00
Allow Slack notifications to change username/icon on the fly (#39091)
* Allow Slack notifications to change username/icon on the fly * Code review
This commit is contained in:
parent
ed68b15a31
commit
4a7c181e91
@ -27,10 +27,12 @@ _LOGGER = logging.getLogger(__name__)
|
|||||||
ATTR_BLOCKS = "blocks"
|
ATTR_BLOCKS = "blocks"
|
||||||
ATTR_BLOCKS_TEMPLATE = "blocks_template"
|
ATTR_BLOCKS_TEMPLATE = "blocks_template"
|
||||||
ATTR_FILE = "file"
|
ATTR_FILE = "file"
|
||||||
|
ATTR_ICON = "icon"
|
||||||
ATTR_PASSWORD = "password"
|
ATTR_PASSWORD = "password"
|
||||||
ATTR_PATH = "path"
|
ATTR_PATH = "path"
|
||||||
ATTR_URL = "url"
|
ATTR_URL = "url"
|
||||||
ATTR_USERNAME = "username"
|
ATTR_USERNAME = "username"
|
||||||
|
ATTR_USERNAME = "username"
|
||||||
|
|
||||||
CONF_DEFAULT_CHANNEL = "default_channel"
|
CONF_DEFAULT_CHANNEL = "default_channel"
|
||||||
|
|
||||||
@ -51,7 +53,12 @@ DATA_FILE_SCHEMA = vol.Schema(
|
|||||||
)
|
)
|
||||||
|
|
||||||
DATA_TEXT_ONLY_SCHEMA = vol.Schema(
|
DATA_TEXT_ONLY_SCHEMA = vol.Schema(
|
||||||
{vol.Optional(ATTR_BLOCKS): list, vol.Optional(ATTR_BLOCKS_TEMPLATE): list}
|
{
|
||||||
|
vol.Optional(ATTR_USERNAME): cv.string,
|
||||||
|
vol.Optional(ATTR_ICON): cv.string,
|
||||||
|
vol.Optional(ATTR_BLOCKS): list,
|
||||||
|
vol.Optional(ATTR_BLOCKS_TEMPLATE): list,
|
||||||
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
DATA_SCHEMA = vol.All(
|
DATA_SCHEMA = vol.All(
|
||||||
@ -191,11 +198,10 @@ class SlackNotificationService(BaseNotificationService):
|
|||||||
except ClientError as err:
|
except ClientError as err:
|
||||||
_LOGGER.error("Error while uploading file message: %s", err)
|
_LOGGER.error("Error while uploading file message: %s", err)
|
||||||
|
|
||||||
async def _async_send_text_only_message(self, targets, message, title, blocks):
|
async def _async_send_text_only_message(
|
||||||
|
self, targets, message, title, blocks, username, icon
|
||||||
|
):
|
||||||
"""Send a text-only message."""
|
"""Send a text-only message."""
|
||||||
username = self._username
|
|
||||||
icon = self._icon
|
|
||||||
|
|
||||||
if self._icon.lower().startswith(("http://", "https://")):
|
if self._icon.lower().startswith(("http://", "https://")):
|
||||||
icon_type = "url"
|
icon_type = "url"
|
||||||
else:
|
else:
|
||||||
@ -204,10 +210,10 @@ class SlackNotificationService(BaseNotificationService):
|
|||||||
tasks = {
|
tasks = {
|
||||||
target: self._client.chat_postMessage(
|
target: self._client.chat_postMessage(
|
||||||
**{
|
**{
|
||||||
"channel": target,
|
|
||||||
"text": message,
|
|
||||||
"blocks": blocks,
|
"blocks": blocks,
|
||||||
|
"channel": target,
|
||||||
"link_names": True,
|
"link_names": True,
|
||||||
|
"text": message,
|
||||||
"username": username,
|
"username": username,
|
||||||
f"icon_{icon_type}": icon,
|
f"icon_{icon_type}": icon,
|
||||||
}
|
}
|
||||||
@ -252,7 +258,12 @@ class SlackNotificationService(BaseNotificationService):
|
|||||||
blocks = {}
|
blocks = {}
|
||||||
|
|
||||||
return await self._async_send_text_only_message(
|
return await self._async_send_text_only_message(
|
||||||
targets, message, title, blocks
|
targets,
|
||||||
|
message,
|
||||||
|
title,
|
||||||
|
blocks,
|
||||||
|
username=data.get(ATTR_USERNAME, self._username),
|
||||||
|
icon=data.get(ATTR_ICON, self._icon),
|
||||||
)
|
)
|
||||||
|
|
||||||
# Message Type 2: A message that uploads a remote file
|
# Message Type 2: A message that uploads a remote file
|
||||||
|
Loading…
x
Reference in New Issue
Block a user