Merge remote-tracking branch 'balloob/dev' into dev

This commit is contained in:
Ryan Kraus 2015-11-15 20:04:21 -05:00
commit 8aafb89a64
4 changed files with 91 additions and 12 deletions

View File

@ -23,6 +23,9 @@ DEPENDENCIES = []
ATTR_TITLE = "title" ATTR_TITLE = "title"
ATTR_TITLE_DEFAULT = "Home Assistant" ATTR_TITLE_DEFAULT = "Home Assistant"
# Target of the notification (user, device, etc)
ATTR_TARGET = 'target'
# Text to notify user of # Text to notify user of
ATTR_MESSAGE = "message" ATTR_MESSAGE = "message"
@ -69,8 +72,9 @@ def setup(hass, config):
return return
title = call.data.get(ATTR_TITLE, ATTR_TITLE_DEFAULT) title = call.data.get(ATTR_TITLE, ATTR_TITLE_DEFAULT)
target = call.data.get(ATTR_TARGET)
notify_service.send_message(message, title=title) notify_service.send_message(message, title=title, target=target)
# register service # register service
service_call_handler = partial(notify_message, notify_service) service_call_handler = partial(notify_message, notify_service)

View File

@ -8,15 +8,18 @@ https://home-assistant.io/components/notify.pushbullet/
""" """
import logging import logging
from homeassistant.components.notify import ATTR_TITLE, BaseNotificationService from homeassistant.components.notify import (
ATTR_TITLE, ATTR_TARGET, BaseNotificationService)
from homeassistant.const import CONF_API_KEY from homeassistant.const import CONF_API_KEY
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
REQUIREMENTS = ['pushbullet.py==0.8.1'] REQUIREMENTS = ['pushbullet.py==0.9.0']
# pylint: disable=unused-argument
def get_service(hass, config): def get_service(hass, config):
""" Get the PushBullet notification service. """ """ Get the PushBullet notification service. """
from pushbullet import PushBullet
from pushbullet import InvalidKeyError from pushbullet import InvalidKeyError
if CONF_API_KEY not in config: if CONF_API_KEY not in config:
@ -24,27 +27,95 @@ def get_service(hass, config):
return None return None
try: try:
return PushBulletNotificationService(config[CONF_API_KEY]) pushbullet = PushBullet(config[CONF_API_KEY])
except InvalidKeyError: except InvalidKeyError:
_LOGGER.error( _LOGGER.error(
"Wrong API key supplied. " "Wrong API key supplied. "
"Get it at https://www.pushbullet.com/account") "Get it at https://www.pushbullet.com/account")
return None return None
return PushBulletNotificationService(pushbullet)
# pylint: disable=too-few-public-methods # pylint: disable=too-few-public-methods
class PushBulletNotificationService(BaseNotificationService): class PushBulletNotificationService(BaseNotificationService):
""" Implements notification service for Pushbullet. """ """ Implements notification service for Pushbullet. """
def __init__(self, api_key): def __init__(self, pb):
from pushbullet import Pushbullet self.pushbullet = pb
self.pbtargets = {}
self.refresh()
self.pushbullet = Pushbullet(api_key) def refresh(self):
'''
Refresh devices, contacts, channels, etc
def send_message(self, message="", **kwargs): pbtargets stores all targets available from this pushbullet instance
""" Send a message to a user. """ into a dict. These are PB objects!. It sacrifices a bit of memory
for faster processing at send_message
'''
self.pushbullet.refresh()
self.pbtargets = {
'device':
{tgt.nickname: tgt for tgt in self.pushbullet.devices},
'contact':
{tgt.email: tgt for tgt in self.pushbullet.contacts},
'channel':
{tgt.channel_tag: tgt for tgt in self.pushbullet.channels},
}
def send_message(self, message=None, **kwargs):
"""
Send a message to a specified target.
If no target specified, a 'normal' push will be sent to all devices
linked to the PB account.
"""
targets = kwargs.get(ATTR_TARGET)
title = kwargs.get(ATTR_TITLE) title = kwargs.get(ATTR_TITLE)
refreshed = False
self.pushbullet.push_note(title, message) if not targets:
# Backward compatebility, notify all devices in own account
self.pushbullet.push_note(title, message)
_LOGGER.info('Sent notification to self')
return
# Make list if not so
if not isinstance(targets, list):
targets = [targets]
# Main loop, Process all targets specified
for target in targets:
# Allow for untargeted push, combined with other types
if target in ['device', 'device/']:
self.pushbullet.push_note(title, message)
_LOGGER.info('Sent notification to self')
continue
try:
ttype, tname = target.split('/', 1)
except ValueError:
_LOGGER.error('Invalid target syntax: %s', target)
continue
# Refresh if name not found. While awaiting periodic refresh
# solution in component, poor mans refresh ;)
if ttype not in self.pbtargets:
_LOGGER.error('Invalid target syntax: %s', target)
continue
if tname not in self.pbtargets[ttype] and not refreshed:
self.refresh()
refreshed = True
# Attempt push_note on a dict value. Keys are types & target
# name. Dict pbtargets has all *actual* targets.
try:
self.pbtargets[ttype][tname].push_note(title, message)
except KeyError:
_LOGGER.error('No such target: %s.%s', ttype, tname)
continue
except self.pushbullet.errors.PushError:
_LOGGER.error('Notify failed to: %s.%s', ttype, tname)
continue
_LOGGER.info('Sent notification to %s.%s', ttype, tname)

View File

@ -9,3 +9,7 @@ notify:
title: title:
description: Optional title for your notification. description: Optional title for your notification.
example: 'Your Garage Door Friend' example: 'Your Garage Door Friend'
target:
description: Target of the notification. Optional depending on the platform
example: platform specific

View File

@ -29,7 +29,7 @@ tellcore-py==1.1.2
python-nmap==0.4.3 python-nmap==0.4.3
# PushBullet (notify.pushbullet) # PushBullet (notify.pushbullet)
pushbullet.py==0.8.1 pushbullet.py==0.9.0
# Nest Thermostat (thermostat.nest) # Nest Thermostat (thermostat.nest)
python-nest==2.6.0 python-nest==2.6.0