Fix Slack "invalid_blocks_format" bug (#43875)

* Fix Slack "invalid_blocks_format" bug

* Fix optional params

* Fix one more optional param

* Update manifest
This commit is contained in:
Aaron Bach 2020-12-02 20:45:08 -07:00 committed by GitHub
parent 4c7e17c5c6
commit 69a438e2fc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 19 additions and 11 deletions

View File

@ -404,6 +404,7 @@ homeassistant/components/simplisafe/* @bachya
homeassistant/components/sinch/* @bendikrb homeassistant/components/sinch/* @bendikrb
homeassistant/components/sisyphus/* @jkeljo homeassistant/components/sisyphus/* @jkeljo
homeassistant/components/sky_hub/* @rogerselwyn homeassistant/components/sky_hub/* @rogerselwyn
homeassistant/components/slack/* @bachya
homeassistant/components/slide/* @ualex73 homeassistant/components/slide/* @ualex73
homeassistant/components/sma/* @kellerza homeassistant/components/sma/* @kellerza
homeassistant/components/smappee/* @bsmappee homeassistant/components/smappee/* @bsmappee

View File

@ -3,5 +3,5 @@
"name": "Slack", "name": "Slack",
"documentation": "https://www.home-assistant.io/integrations/slack", "documentation": "https://www.home-assistant.io/integrations/slack",
"requirements": ["slackclient==2.5.0"], "requirements": ["slackclient==2.5.0"],
"codeowners": [] "codeowners": ["@bachya"]
} }

View File

@ -198,17 +198,21 @@ class SlackNotificationService(BaseNotificationService):
_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( async def _async_send_text_only_message(
self, targets, message, title, blocks, username, icon self,
targets,
message,
title,
*,
username=None,
icon=None,
blocks=None,
): ):
"""Send a text-only message.""" """Send a text-only message."""
message_dict = { message_dict = {"link_names": True, "text": message}
"blocks": blocks,
"link_names": True, if username:
"text": message, message_dict["username"] = username
"username": username,
}
icon = icon or self._icon
if icon: if icon:
if icon.lower().startswith(("http://", "https://")): if icon.lower().startswith(("http://", "https://")):
icon_type = "url" icon_type = "url"
@ -217,6 +221,9 @@ class SlackNotificationService(BaseNotificationService):
message_dict[f"icon_{icon_type}"] = icon message_dict[f"icon_{icon_type}"] = icon
if blocks:
message_dict["blocks"] = blocks
tasks = { tasks = {
target: self._client.chat_postMessage(**message_dict, channel=target) target: self._client.chat_postMessage(**message_dict, channel=target)
for target in targets for target in targets
@ -256,15 +263,15 @@ class SlackNotificationService(BaseNotificationService):
elif ATTR_BLOCKS in data: elif ATTR_BLOCKS in data:
blocks = data[ATTR_BLOCKS] blocks = data[ATTR_BLOCKS]
else: else:
blocks = {} blocks = None
return await self._async_send_text_only_message( return await self._async_send_text_only_message(
targets, targets,
message, message,
title, title,
blocks,
username=data.get(ATTR_USERNAME, self._username), username=data.get(ATTR_USERNAME, self._username),
icon=data.get(ATTR_ICON, self._icon), icon=data.get(ATTR_ICON, self._icon),
blocks=blocks,
) )
# Message Type 2: A message that uploads a remote file # Message Type 2: A message that uploads a remote file