core/homeassistant/components/notify/googlevoice.py

63 lines
2.0 KiB
Python
Raw Normal View History

2016-01-29 02:07:59 +00:00
"""
homeassistant.components.notify.googlevoice
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Google Voice SMS platform for notify component.
For more details about this platform, please refer to the documentation at
https://home-assistant.io/components/notify.free_mobile/
"""
import logging
2016-02-19 05:27:50 +00:00
2016-01-29 02:07:59 +00:00
from homeassistant.components.notify import (
2016-02-19 05:27:50 +00:00
ATTR_TARGET, DOMAIN, BaseNotificationService)
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
from homeassistant.helpers import validate_config
2016-01-29 02:07:59 +00:00
_LOGGER = logging.getLogger(__name__)
REQUIREMENTS = ['https://github.com/w1ll1am23/pygooglevoice-sms/archive/'
2016-01-30 02:53:06 +00:00
'7c5ee9969b97a7992fc86a753fe9f20e3ffa3f7c.zip#'
'pygooglevoice-sms==0.0.1']
2016-01-29 02:07:59 +00:00
2016-01-29 21:48:01 +00:00
2016-01-29 02:07:59 +00:00
def get_service(hass, config):
""" Get the Google Voice SMS notification service. """
if not validate_config({DOMAIN: config},
{DOMAIN: [CONF_USERNAME,
2016-01-29 19:14:27 +00:00
CONF_PASSWORD]},
2016-01-29 02:07:59 +00:00
_LOGGER):
return None
return GoogleVoiceSMSNotificationService(config[CONF_USERNAME],
2016-01-29 21:48:01 +00:00
config[CONF_PASSWORD])
2016-01-29 02:07:59 +00:00
# pylint: disable=too-few-public-methods
class GoogleVoiceSMSNotificationService(BaseNotificationService):
""" Implements notification service for the Google Voice SMS service. """
2016-01-29 19:14:27 +00:00
def __init__(self, username, password):
2016-01-29 02:19:15 +00:00
from googlevoicesms import Voice
2016-01-29 02:07:59 +00:00
self.voice = Voice()
self.username = username
self.password = password
def send_message(self, message="", **kwargs):
2016-01-29 19:14:27 +00:00
""" Send SMS to specified target user cell. """
targets = kwargs.get(ATTR_TARGET)
2016-01-30 03:04:02 +00:00
2016-01-30 03:09:59 +00:00
if not targets:
_LOGGER.info('At least 1 target is required')
return
2016-01-30 03:04:02 +00:00
if not isinstance(targets, list):
targets = [targets]
2016-01-29 19:14:27 +00:00
self.voice.login(self.username, self.password)
2016-01-29 21:48:01 +00:00
2016-01-29 19:14:27 +00:00
for target in targets:
2016-01-29 21:48:01 +00:00
self.voice.send_sms(target, message)
2016-01-29 19:14:27 +00:00
2016-01-29 21:48:01 +00:00
self.voice.logout()