core/homeassistant/components/ciscospark/notify.py

53 lines
1.5 KiB
Python
Raw Normal View History

"""Cisco Spark platform for notify component."""
import logging
from ciscosparkapi import CiscoSparkAPI, SparkApiError
import voluptuous as vol
2019-07-31 19:25:30 +00:00
from homeassistant.components.notify import (
ATTR_TITLE,
PLATFORM_SCHEMA,
BaseNotificationService,
)
from homeassistant.const import CONF_TOKEN
import homeassistant.helpers.config_validation as cv
_LOGGER = logging.getLogger(__name__)
2019-07-31 19:25:30 +00:00
CONF_ROOMID = "roomid"
2019-07-31 19:25:30 +00:00
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
{vol.Required(CONF_TOKEN): cv.string, vol.Required(CONF_ROOMID): cv.string}
)
def get_service(hass, config, discovery_info=None):
"""Get the CiscoSpark notification service."""
return CiscoSparkNotificationService(
2019-07-31 19:25:30 +00:00
config.get(CONF_TOKEN), config.get(CONF_ROOMID)
)
class CiscoSparkNotificationService(BaseNotificationService):
"""The Cisco Spark Notification Service."""
def __init__(self, token, default_room):
"""Initialize the service."""
2019-07-31 19:25:30 +00:00
self._default_room = default_room
self._token = token
self._spark = CiscoSparkAPI(access_token=self._token)
def send_message(self, message="", **kwargs):
"""Send a message to a user."""
2019-07-31 19:25:30 +00:00
try:
title = ""
if kwargs.get(ATTR_TITLE) is not None:
title = kwargs.get(ATTR_TITLE) + ": "
2019-07-31 19:25:30 +00:00
self._spark.messages.create(roomId=self._default_room, text=title + message)
except SparkApiError as api_error:
2019-07-31 19:25:30 +00:00
_LOGGER.error(
"Could not send CiscoSpark notification. Error: %s", api_error
)