2019-04-03 15:40:03 +00:00
|
|
|
"""Telegram platform for notify component."""
|
2015-10-09 12:04:29 +00:00
|
|
|
import logging
|
2016-09-05 11:27:10 +00:00
|
|
|
|
2016-07-29 13:20:23 +00:00
|
|
|
import voluptuous as vol
|
2015-10-09 12:04:29 +00:00
|
|
|
|
2017-05-10 04:42:17 +00:00
|
|
|
from homeassistant.const import ATTR_LOCATION
|
2015-10-09 12:04:29 +00:00
|
|
|
|
2019-03-28 03:36:13 +00:00
|
|
|
from homeassistant.components.notify import (
|
2019-03-21 05:56:46 +00:00
|
|
|
ATTR_DATA, ATTR_MESSAGE, ATTR_TARGET, ATTR_TITLE, PLATFORM_SCHEMA,
|
|
|
|
BaseNotificationService)
|
|
|
|
|
2015-10-09 12:04:29 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
2015-10-26 20:11:24 +00:00
|
|
|
|
2017-05-10 04:42:17 +00:00
|
|
|
DOMAIN = 'telegram_bot'
|
|
|
|
DEPENDENCIES = [DOMAIN]
|
2015-10-09 12:04:29 +00:00
|
|
|
|
2017-02-09 22:05:28 +00:00
|
|
|
ATTR_KEYBOARD = 'keyboard'
|
2017-05-10 04:42:17 +00:00
|
|
|
ATTR_INLINE_KEYBOARD = 'inline_keyboard'
|
|
|
|
ATTR_PHOTO = 'photo'
|
2017-11-11 23:13:35 +00:00
|
|
|
ATTR_VIDEO = 'video'
|
2016-09-28 07:08:24 +00:00
|
|
|
ATTR_DOCUMENT = 'document'
|
2016-07-29 13:20:23 +00:00
|
|
|
|
|
|
|
CONF_CHAT_ID = 'chat_id'
|
|
|
|
|
2016-09-05 11:27:10 +00:00
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
2017-05-22 00:02:22 +00:00
|
|
|
vol.Required(CONF_CHAT_ID): vol.Coerce(int),
|
2016-07-29 13:20:23 +00:00
|
|
|
})
|
2016-07-13 04:48:33 +00:00
|
|
|
|
2015-10-09 12:04:29 +00:00
|
|
|
|
2017-01-15 02:53:14 +00:00
|
|
|
def get_service(hass, config, discovery_info=None):
|
2016-03-08 10:46:32 +00:00
|
|
|
"""Get the Telegram notification service."""
|
2017-05-10 04:42:17 +00:00
|
|
|
chat_id = config.get(CONF_CHAT_ID)
|
|
|
|
return TelegramNotificationService(hass, chat_id)
|
2015-10-09 12:04:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TelegramNotificationService(BaseNotificationService):
|
2016-03-08 10:46:32 +00:00
|
|
|
"""Implement the notification service for Telegram."""
|
2015-10-09 12:04:29 +00:00
|
|
|
|
2017-05-10 04:42:17 +00:00
|
|
|
def __init__(self, hass, chat_id):
|
2016-03-08 10:46:32 +00:00
|
|
|
"""Initialize the service."""
|
2015-10-09 12:04:29 +00:00
|
|
|
self._chat_id = chat_id
|
2017-05-10 04:42:17 +00:00
|
|
|
self.hass = hass
|
2015-10-09 12:04:29 +00:00
|
|
|
|
|
|
|
def send_message(self, message="", **kwargs):
|
2016-03-08 10:46:32 +00:00
|
|
|
"""Send a message to a user."""
|
2018-10-05 21:09:55 +00:00
|
|
|
service_data = {ATTR_TARGET: kwargs.get(ATTR_TARGET, self._chat_id)}
|
2017-05-10 04:42:17 +00:00
|
|
|
if ATTR_TITLE in kwargs:
|
|
|
|
service_data.update({ATTR_TITLE: kwargs.get(ATTR_TITLE)})
|
|
|
|
if message:
|
|
|
|
service_data.update({ATTR_MESSAGE: message})
|
2016-07-29 13:20:23 +00:00
|
|
|
data = kwargs.get(ATTR_DATA)
|
|
|
|
|
2017-05-10 04:42:17 +00:00
|
|
|
# Get keyboard info
|
|
|
|
if data is not None and ATTR_KEYBOARD in data:
|
|
|
|
keys = data.get(ATTR_KEYBOARD)
|
|
|
|
keys = keys if isinstance(keys, list) else [keys]
|
|
|
|
service_data.update(keyboard=keys)
|
|
|
|
elif data is not None and ATTR_INLINE_KEYBOARD in data:
|
|
|
|
keys = data.get(ATTR_INLINE_KEYBOARD)
|
|
|
|
keys = keys if isinstance(keys, list) else [keys]
|
|
|
|
service_data.update(inline_keyboard=keys)
|
|
|
|
|
2017-11-11 23:13:35 +00:00
|
|
|
# Send a photo, video, document, or location
|
2016-07-29 13:20:23 +00:00
|
|
|
if data is not None and ATTR_PHOTO in data:
|
|
|
|
photos = data.get(ATTR_PHOTO, None)
|
|
|
|
photos = photos if isinstance(photos, list) else [photos]
|
|
|
|
for photo_data in photos:
|
2017-05-10 04:42:17 +00:00
|
|
|
service_data.update(photo_data)
|
|
|
|
self.hass.services.call(
|
|
|
|
DOMAIN, 'send_photo', service_data=service_data)
|
2016-07-29 13:20:23 +00:00
|
|
|
return
|
2018-07-23 08:16:05 +00:00
|
|
|
if data is not None and ATTR_VIDEO in data:
|
2017-11-11 23:13:35 +00:00
|
|
|
videos = data.get(ATTR_VIDEO, None)
|
|
|
|
videos = videos if isinstance(videos, list) else [videos]
|
|
|
|
for video_data in videos:
|
|
|
|
service_data.update(video_data)
|
|
|
|
self.hass.services.call(
|
|
|
|
DOMAIN, 'send_video', service_data=service_data)
|
|
|
|
return
|
2018-07-23 08:16:05 +00:00
|
|
|
if data is not None and ATTR_LOCATION in data:
|
2017-05-10 04:42:17 +00:00
|
|
|
service_data.update(data.get(ATTR_LOCATION))
|
|
|
|
return self.hass.services.call(
|
|
|
|
DOMAIN, 'send_location', service_data=service_data)
|
2018-07-23 08:16:05 +00:00
|
|
|
if data is not None and ATTR_DOCUMENT in data:
|
2017-05-10 04:42:17 +00:00
|
|
|
service_data.update(data.get(ATTR_DOCUMENT))
|
|
|
|
return self.hass.services.call(
|
|
|
|
DOMAIN, 'send_document', service_data=service_data)
|
2016-09-01 13:35:46 +00:00
|
|
|
|
2017-04-26 08:50:08 +00:00
|
|
|
# Send message
|
2017-05-10 04:42:17 +00:00
|
|
|
_LOGGER.debug('TELEGRAM NOTIFIER calling %s.send_message with %s',
|
|
|
|
DOMAIN, service_data)
|
|
|
|
return self.hass.services.call(
|
|
|
|
DOMAIN, 'send_message', service_data=service_data)
|