mirror of
https://github.com/home-assistant/core.git
synced 2025-04-23 00:37:53 +00:00
New platform for Microsoft Teams (#27981)
* New Microsoft Teams notification service * Updated codeowners * Updated requirements_all * Changed from WEBHOOK_ID to URL * Moved try/except block
This commit is contained in:
parent
44bf9e9ddc
commit
852cbad965
@ -418,6 +418,7 @@ omit =
|
||||
homeassistant/components/mpchc/media_player.py
|
||||
homeassistant/components/mpd/media_player.py
|
||||
homeassistant/components/mqtt_room/sensor.py
|
||||
homeassistant/components/msteams/notify.py
|
||||
homeassistant/components/mvglive/sensor.py
|
||||
homeassistant/components/mychevy/*
|
||||
homeassistant/components/mycroft/*
|
||||
|
@ -190,6 +190,7 @@ homeassistant/components/monoprice/* @etsinko
|
||||
homeassistant/components/moon/* @fabaff
|
||||
homeassistant/components/mpd/* @fabaff
|
||||
homeassistant/components/mqtt/* @home-assistant/core
|
||||
homeassistant/components/msteams/* @peroyvind
|
||||
homeassistant/components/mysensors/* @MartinHjelmare
|
||||
homeassistant/components/mystrom/* @fabaff
|
||||
homeassistant/components/neato/* @dshokouhi @Santobert
|
||||
|
1
homeassistant/components/msteams/__init__.py
Normal file
1
homeassistant/components/msteams/__init__.py
Normal file
@ -0,0 +1 @@
|
||||
"""The Microsoft Teams component."""
|
8
homeassistant/components/msteams/manifest.json
Normal file
8
homeassistant/components/msteams/manifest.json
Normal file
@ -0,0 +1,8 @@
|
||||
{
|
||||
"domain": "msteams",
|
||||
"name": "Microsoft Teams",
|
||||
"documentation": "https://www.home-assistant.io/integrations/msteams",
|
||||
"requirements": ["pymsteams==0.1.12"],
|
||||
"dependencies": [],
|
||||
"codeowners": ["@peroyvind"]
|
||||
}
|
67
homeassistant/components/msteams/notify.py
Normal file
67
homeassistant/components/msteams/notify.py
Normal file
@ -0,0 +1,67 @@
|
||||
"""Microsoft Teams platform for notify component."""
|
||||
import logging
|
||||
|
||||
import pymsteams
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant.components.notify import (
|
||||
ATTR_DATA,
|
||||
ATTR_TITLE,
|
||||
ATTR_TITLE_DEFAULT,
|
||||
PLATFORM_SCHEMA,
|
||||
BaseNotificationService,
|
||||
)
|
||||
from homeassistant.const import CONF_URL
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
ATTR_FILE_URL = "image_url"
|
||||
|
||||
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({vol.Required(CONF_URL): cv.url})
|
||||
|
||||
|
||||
def get_service(hass, config, discovery_info=None):
|
||||
"""Get the Microsoft Teams notification service."""
|
||||
webhook_url = config.get(CONF_URL)
|
||||
|
||||
try:
|
||||
return MSTeamsNotificationService(webhook_url)
|
||||
|
||||
except RuntimeError as err:
|
||||
_LOGGER.exception("Error in creating a new Microsoft Teams message: %s", err)
|
||||
return None
|
||||
|
||||
|
||||
class MSTeamsNotificationService(BaseNotificationService):
|
||||
"""Implement the notification service for Microsoft Teams."""
|
||||
|
||||
def __init__(self, webhook_url):
|
||||
"""Initialize the service."""
|
||||
self._webhook_url = webhook_url
|
||||
self.teams_message = pymsteams.connectorcard(self._webhook_url)
|
||||
|
||||
def send_message(self, message=None, **kwargs):
|
||||
"""Send a message to the webhook."""
|
||||
title = kwargs.get(ATTR_TITLE, ATTR_TITLE_DEFAULT)
|
||||
data = kwargs.get(ATTR_DATA)
|
||||
|
||||
self.teams_message.title(title)
|
||||
|
||||
self.teams_message.text(message)
|
||||
|
||||
if data is not None:
|
||||
file_url = data.get(ATTR_FILE_URL)
|
||||
|
||||
if file_url is not None:
|
||||
if not file_url.startswith("http"):
|
||||
_LOGGER.error("URL should start with http or https")
|
||||
return
|
||||
|
||||
message_section = pymsteams.cardsection()
|
||||
message_section.addImage(file_url)
|
||||
self.teams_message.addSection(message_section)
|
||||
try:
|
||||
self.teams_message.send()
|
||||
except RuntimeError as err:
|
||||
_LOGGER.error("Could not send notification. Error: %s", err)
|
@ -1318,6 +1318,9 @@ pymodbus==1.5.2
|
||||
# homeassistant.components.monoprice
|
||||
pymonoprice==0.3
|
||||
|
||||
# homeassistant.components.msteams
|
||||
pymsteams==0.1.12
|
||||
|
||||
# homeassistant.components.yamaha_musiccast
|
||||
pymusiccast==0.1.6
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user