From bf4e98d1ae9accb232b02ceb1a1a28bf376a3c66 Mon Sep 17 00:00:00 2001
From: Aaron Bach <bachya1208@gmail.com>
Date: Wed, 2 Dec 2020 20:45:08 -0700
Subject: [PATCH] Fix Slack "invalid_blocks_format" bug (#43875)

* Fix Slack "invalid_blocks_format" bug

* Fix optional params

* Fix one more optional param

* Update manifest
---
 CODEOWNERS                                   |  1 +
 homeassistant/components/slack/manifest.json |  2 +-
 homeassistant/components/slack/notify.py     | 27 ++++++++++++--------
 3 files changed, 19 insertions(+), 11 deletions(-)

diff --git a/CODEOWNERS b/CODEOWNERS
index c6deb8e9f8f..27614c3d49d 100644
--- a/CODEOWNERS
+++ b/CODEOWNERS
@@ -404,6 +404,7 @@ homeassistant/components/simplisafe/* @bachya
 homeassistant/components/sinch/* @bendikrb
 homeassistant/components/sisyphus/* @jkeljo
 homeassistant/components/sky_hub/* @rogerselwyn
+homeassistant/components/slack/* @bachya
 homeassistant/components/slide/* @ualex73
 homeassistant/components/sma/* @kellerza
 homeassistant/components/smappee/* @bsmappee
diff --git a/homeassistant/components/slack/manifest.json b/homeassistant/components/slack/manifest.json
index ad45abbe3c0..e183dd455f1 100644
--- a/homeassistant/components/slack/manifest.json
+++ b/homeassistant/components/slack/manifest.json
@@ -3,5 +3,5 @@
   "name": "Slack",
   "documentation": "https://www.home-assistant.io/integrations/slack",
   "requirements": ["slackclient==2.5.0"],
-  "codeowners": []
+  "codeowners": ["@bachya"]
 }
diff --git a/homeassistant/components/slack/notify.py b/homeassistant/components/slack/notify.py
index 88317b31585..90caad62a58 100644
--- a/homeassistant/components/slack/notify.py
+++ b/homeassistant/components/slack/notify.py
@@ -198,17 +198,21 @@ class SlackNotificationService(BaseNotificationService):
             _LOGGER.error("Error while uploading file message: %s", err)
 
     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."""
-        message_dict = {
-            "blocks": blocks,
-            "link_names": True,
-            "text": message,
-            "username": username,
-        }
+        message_dict = {"link_names": True, "text": message}
+
+        if username:
+            message_dict["username"] = username
 
-        icon = icon or self._icon
         if icon:
             if icon.lower().startswith(("http://", "https://")):
                 icon_type = "url"
@@ -217,6 +221,9 @@ class SlackNotificationService(BaseNotificationService):
 
             message_dict[f"icon_{icon_type}"] = icon
 
+        if blocks:
+            message_dict["blocks"] = blocks
+
         tasks = {
             target: self._client.chat_postMessage(**message_dict, channel=target)
             for target in targets
@@ -256,15 +263,15 @@ class SlackNotificationService(BaseNotificationService):
             elif ATTR_BLOCKS in data:
                 blocks = data[ATTR_BLOCKS]
             else:
-                blocks = {}
+                blocks = None
 
             return await self._async_send_text_only_message(
                 targets,
                 message,
                 title,
-                blocks,
                 username=data.get(ATTR_USERNAME, self._username),
                 icon=data.get(ATTR_ICON, self._icon),
+                blocks=blocks,
             )
 
         # Message Type 2: A message that uploads a remote file
-- 
GitLab