mirror of
https://github.com/home-assistant/core
synced 2024-09-06 10:29:55 +02:00
Add support for Discord embed in messages (#44986)
Co-authored-by: Joakim Sørensen <hi@ludeeus.dev>
This commit is contained in:
parent
c16fd0a1ac
commit
ab710f2154
@ -16,10 +16,15 @@ import homeassistant.helpers.config_validation as cv
|
|||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({vol.Required(CONF_TOKEN): cv.string})
|
ATTR_EMBED = "embed"
|
||||||
|
ATTR_EMBED_AUTHOR = "author"
|
||||||
|
ATTR_EMBED_FIELDS = "fields"
|
||||||
|
ATTR_EMBED_FOOTER = "footer"
|
||||||
|
ATTR_EMBED_THUMBNAIL = "thumbnail"
|
||||||
ATTR_IMAGES = "images"
|
ATTR_IMAGES = "images"
|
||||||
|
|
||||||
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({vol.Required(CONF_TOKEN): cv.string})
|
||||||
|
|
||||||
|
|
||||||
def get_service(hass, config, discovery_info=None):
|
def get_service(hass, config, discovery_info=None):
|
||||||
"""Get the Discord notification service."""
|
"""Get the Discord notification service."""
|
||||||
@ -43,16 +48,21 @@ class DiscordNotificationService(BaseNotificationService):
|
|||||||
|
|
||||||
async def async_send_message(self, message, **kwargs):
|
async def async_send_message(self, message, **kwargs):
|
||||||
"""Login to Discord, send message to channel(s) and log out."""
|
"""Login to Discord, send message to channel(s) and log out."""
|
||||||
|
|
||||||
discord.VoiceClient.warn_nacl = False
|
discord.VoiceClient.warn_nacl = False
|
||||||
discord_bot = discord.Client()
|
discord_bot = discord.Client()
|
||||||
images = None
|
images = None
|
||||||
|
embedding = None
|
||||||
|
|
||||||
if ATTR_TARGET not in kwargs:
|
if ATTR_TARGET not in kwargs:
|
||||||
_LOGGER.error("No target specified")
|
_LOGGER.error("No target specified")
|
||||||
return None
|
return None
|
||||||
|
|
||||||
data = kwargs.get(ATTR_DATA) or {}
|
data = kwargs.get(ATTR_DATA) or {}
|
||||||
|
|
||||||
|
if ATTR_EMBED in data:
|
||||||
|
embedding = data[ATTR_EMBED]
|
||||||
|
fields = embedding.get(ATTR_EMBED_FIELDS)
|
||||||
|
|
||||||
if ATTR_IMAGES in data:
|
if ATTR_IMAGES in data:
|
||||||
images = []
|
images = []
|
||||||
|
|
||||||
@ -86,7 +96,20 @@ class DiscordNotificationService(BaseNotificationService):
|
|||||||
files = []
|
files = []
|
||||||
for image in images:
|
for image in images:
|
||||||
files.append(discord.File(image))
|
files.append(discord.File(image))
|
||||||
await channel.send(message, files=files)
|
if embedding:
|
||||||
|
embed = discord.Embed(**embedding)
|
||||||
|
if fields:
|
||||||
|
for field_num, field_name in enumerate(fields):
|
||||||
|
embed.add_field(**fields[field_num])
|
||||||
|
if ATTR_EMBED_FOOTER in embedding:
|
||||||
|
embed.set_footer(**embedding[ATTR_EMBED_FOOTER])
|
||||||
|
if ATTR_EMBED_AUTHOR in embedding:
|
||||||
|
embed.set_author(**embedding[ATTR_EMBED_AUTHOR])
|
||||||
|
if ATTR_EMBED_THUMBNAIL in embedding:
|
||||||
|
embed.set_thumbnail(**embedding[ATTR_EMBED_THUMBNAIL])
|
||||||
|
await channel.send(message, files=files, embed=embed)
|
||||||
|
else:
|
||||||
|
await channel.send(message, files=files)
|
||||||
except (discord.errors.HTTPException, discord.errors.NotFound) as error:
|
except (discord.errors.HTTPException, discord.errors.NotFound) as error:
|
||||||
_LOGGER.warning("Communication error: %s", error)
|
_LOGGER.warning("Communication error: %s", error)
|
||||||
await discord_bot.logout()
|
await discord_bot.logout()
|
||||||
|
Loading…
Reference in New Issue
Block a user