From a7d49e40c0cc62b5eb81e02dcd1d0b80075c1842 Mon Sep 17 00:00:00 2001 From: Finbarr Brady Date: Wed, 3 Apr 2019 07:25:02 +0100 Subject: [PATCH] Rebrand Cisco Spark notify to be Cisco Webex Teams (#21938) * Rebrand Cisco Spark notify to be Cisco Webex Teams * Remove property from class * Switch to use html for api * Update notify.py * Rename CONF_ROOMID to CONF_ROOM_ID * updated * Fix lint errors * Update notify.py * Update notify.py * Also validate room ID * Update notify.py * Update .coveragerc * Update notify.py --- .coveragerc | 1 + .../components/cisco_webex_teams/__init__.py | 1 + .../components/cisco_webex_teams/notify.py | 60 +++++++++++++++++++ requirements_all.txt | 3 + 4 files changed, 65 insertions(+) create mode 100644 homeassistant/components/cisco_webex_teams/__init__.py create mode 100644 homeassistant/components/cisco_webex_teams/notify.py diff --git a/.coveragerc b/.coveragerc index cae3bf423a9..25e5cf8bb03 100644 --- a/.coveragerc +++ b/.coveragerc @@ -87,6 +87,7 @@ omit = homeassistant/components/channels/media_player.py homeassistant/components/cisco_ios/device_tracker.py homeassistant/components/cisco_mobility_express/device_tracker.py + homeassistant/components/cisco_webex_teams/notify.py homeassistant/components/ciscospark/notify.py homeassistant/components/citybikes/sensor.py homeassistant/components/clementine/media_player.py diff --git a/homeassistant/components/cisco_webex_teams/__init__.py b/homeassistant/components/cisco_webex_teams/__init__.py new file mode 100644 index 00000000000..0a8714806a1 --- /dev/null +++ b/homeassistant/components/cisco_webex_teams/__init__.py @@ -0,0 +1 @@ +"""Component to integrate the Cisco Webex Teams cloud.""" diff --git a/homeassistant/components/cisco_webex_teams/notify.py b/homeassistant/components/cisco_webex_teams/notify.py new file mode 100644 index 00000000000..f893d4071b0 --- /dev/null +++ b/homeassistant/components/cisco_webex_teams/notify.py @@ -0,0 +1,60 @@ +"""Cisco Webex Teams notify component.""" +import logging + +import voluptuous as vol + +from homeassistant.components.notify import ( + PLATFORM_SCHEMA, BaseNotificationService, ATTR_TITLE) +from homeassistant.const import (CONF_TOKEN) +import homeassistant.helpers.config_validation as cv + +REQUIREMENTS = ['webexteamssdk==1.1.1'] + +_LOGGER = logging.getLogger(__name__) + +CONF_ROOM_ID = 'room_id' + +PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({ + vol.Required(CONF_TOKEN): cv.string, + vol.Required(CONF_ROOM_ID): cv.string, +}) + + +def get_service(hass, config, discovery_info=None): + """Get the CiscoWebexTeams notification service.""" + from webexteamssdk import WebexTeamsAPI, exceptions + client = WebexTeamsAPI(access_token=config[CONF_TOKEN]) + try: + # Validate the token & room_id + client.rooms.get(config[CONF_ROOM_ID]) + except exceptions.ApiError as error: + _LOGGER.error(error) + return None + + return CiscoWebexTeamsNotificationService( + client, + config[CONF_ROOM_ID]) + + +class CiscoWebexTeamsNotificationService(BaseNotificationService): + """The Cisco Webex Teams Notification Service.""" + + def __init__(self, client, room): + """Initialize the service.""" + self.room = room + self.client = client + + def send_message(self, message="", **kwargs): + """Send a message to a user.""" + from webexteamssdk import ApiError + title = "" + if kwargs.get(ATTR_TITLE) is not None: + title = "{}{}".format(kwargs.get(ATTR_TITLE), "
") + + try: + self.client.messages.create(roomId=self.room, + html="{}{}".format(title, message)) + except ApiError as api_error: + _LOGGER.error("Could not send CiscoWebexTeams notification. " + "Error: %s", + api_error) diff --git a/requirements_all.txt b/requirements_all.txt index 6d136a11fb1..8ed77fdfb3e 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -1783,6 +1783,9 @@ watchdog==0.8.3 # homeassistant.components.waterfurnace waterfurnace==1.1.0 +# homeassistant.components.cisco_webex_teams.notify +webexteamssdk==1.1.1 + # homeassistant.components.gpmdp.media_player websocket-client==0.54.0