core/homeassistant/components/notify/pushbullet.py

142 lines
4.8 KiB
Python
Raw Normal View History

"""
2017-03-27 08:35:27 +00:00
Pushbullet platform for notify component.
2015-05-13 17:18:30 +00:00
2015-10-13 20:30:21 +00:00
For more details about this platform, please refer to the documentation at
2015-11-09 17:33:11 +00:00
https://home-assistant.io/components/notify.pushbullet/
"""
import logging
import voluptuous as vol
from homeassistant.components.notify import (
ATTR_DATA, ATTR_TARGET, ATTR_TITLE, ATTR_TITLE_DEFAULT,
PLATFORM_SCHEMA, BaseNotificationService)
from homeassistant.const import CONF_API_KEY
import homeassistant.helpers.config_validation as cv
REQUIREMENTS = ['pushbullet.py==0.10.0']
_LOGGER = logging.getLogger(__name__)
ATTR_URL = 'url'
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Required(CONF_API_KEY): cv.string,
})
# pylint: disable=unused-argument
def get_service(hass, config, discovery_info=None):
2017-03-27 08:35:27 +00:00
"""Get the Pushbullet notification service."""
from pushbullet import PushBullet
2015-11-09 06:15:34 +00:00
from pushbullet import InvalidKeyError
try:
pushbullet = PushBullet(config[CONF_API_KEY])
2015-01-11 16:09:25 +00:00
except InvalidKeyError:
_LOGGER.error("Wrong API key supplied")
2015-11-09 06:15:34 +00:00
return None
return PushBulletNotificationService(pushbullet)
class PushBulletNotificationService(BaseNotificationService):
2016-03-08 10:46:32 +00:00
"""Implement the notification service for Pushbullet."""
def __init__(self, pb):
2016-03-08 10:46:32 +00:00
"""Initialize the service."""
self.pushbullet = pb
self.pbtargets = {}
self.refresh()
def refresh(self):
2016-03-08 10:46:32 +00:00
"""Refresh devices, contacts, etc.
2017-03-27 08:35:27 +00:00
pbtargets stores all targets available from this Pushbullet instance
into a dict. These are Pushbullet objects!. It sacrifices a bit of
memory for faster processing at send_message.
As of sept 2015, contacts were replaced by chats. This is not
2015-11-25 07:56:50 +00:00
implemented in the module yet.
"""
2015-11-15 20:49:42 +00:00
self.pushbullet.refresh()
self.pbtargets = {
'device': {
tgt.nickname.lower(): tgt for tgt in self.pushbullet.devices},
'channel': {
tgt.channel_tag.lower(): tgt for
tgt in self.pushbullet.channels},
}
def send_message(self, message=None, **kwargs):
2016-03-08 10:46:32 +00:00
"""Send a message to a specified target.
2015-11-15 20:49:42 +00:00
If no target specified, a 'normal' push will be sent to all devices
2017-03-27 08:35:27 +00:00
linked to the Pushbullet account.
Email is special, these are assumed to always exist. We use a special
2015-11-25 07:56:50 +00:00
call which doesn't require a push object.
2015-11-15 20:49:42 +00:00
"""
targets = kwargs.get(ATTR_TARGET)
title = kwargs.get(ATTR_TITLE, ATTR_TITLE_DEFAULT)
data = kwargs.get(ATTR_DATA)
url = None
if data:
url = data.get(ATTR_URL, None)
2015-11-15 23:46:16 +00:00
refreshed = False
if not targets:
2017-03-27 08:35:27 +00:00
# Backward compatibility, notify all devices in own account
if url:
self.pushbullet.push_link(title, url, body=message)
else:
self.pushbullet.push_note(title, message)
2017-03-27 08:35:27 +00:00
_LOGGER.info("Sent notification to self")
2015-11-15 23:46:16 +00:00
return
2017-03-27 08:35:27 +00:00
# Main loop, process all targets specified
2015-11-15 23:46:16 +00:00
for target in targets:
try:
ttype, tname = target.split('/', 1)
except ValueError:
2017-03-27 08:35:27 +00:00
_LOGGER.error("Invalid target syntax: %s", target)
continue
2015-11-15 23:46:16 +00:00
# Target is email, send directly, don't use a target object
# This also seems works to send to all devices in own account
if ttype == 'email':
if url:
2017-03-27 08:35:27 +00:00
self.pushbullet.push_link(
title, url, body=message, email=tname)
else:
self.pushbullet.push_note(title, message, email=tname)
2017-03-27 08:35:27 +00:00
_LOGGER.info("Sent notification to email %s", tname)
continue
# Refresh if name not found. While awaiting periodic refresh
# solution in component, poor mans refresh ;)
if ttype not in self.pbtargets:
2017-03-27 08:35:27 +00:00
_LOGGER.error("Invalid target syntax: %s", target)
continue
2015-11-28 23:41:30 +00:00
tname = tname.lower()
if tname not in self.pbtargets[ttype] and not refreshed:
2015-11-15 23:46:16 +00:00
self.refresh()
refreshed = True
# Attempt push_note on a dict value. Keys are types & target
# name. Dict pbtargets has all *actual* targets.
try:
if url:
2017-03-27 08:35:27 +00:00
self.pbtargets[ttype][tname].push_link(
title, url, body=message)
else:
self.pbtargets[ttype][tname].push_note(title, message)
2017-03-27 08:35:27 +00:00
_LOGGER.info("Sent notification to %s/%s", ttype, tname)
2015-11-15 23:46:16 +00:00
except KeyError:
2017-03-27 08:35:27 +00:00
_LOGGER.error("No such target: %s/%s", ttype, tname)
2015-11-15 23:46:16 +00:00
continue
except self.pushbullet.errors.PushError:
2017-03-27 08:35:27 +00:00
_LOGGER.error("Notify failed to: %s/%s", ttype, tname)
2015-11-15 23:46:16 +00:00
continue