Added target support for googlevoice

This commit is contained in:
William Scanlon 2016-01-29 14:14:27 -05:00
parent 9ec44fbe32
commit aaf75c7e45
2 changed files with 14 additions and 19 deletions

View file

@ -9,8 +9,8 @@ https://home-assistant.io/components/notify.free_mobile/
import logging import logging
from homeassistant.helpers import validate_config from homeassistant.helpers import validate_config
from homeassistant.components.notify import ( from homeassistant.components.notify import (
DOMAIN, BaseNotificationService) DOMAIN, ATTR_TARGET, BaseNotificationService)
from homeassistant.const import CONF_USERNAME, CONF_PASSWORD, CONF_PHONE_NUMBER from homeassistant.const import CONF_USERNAME, CONF_PASSWORD
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
REQUIREMENTS = ['https://github.com/w1ll1am23/pygooglevoice-sms/archive/' REQUIREMENTS = ['https://github.com/w1ll1am23/pygooglevoice-sms/archive/'
@ -21,36 +21,32 @@ def get_service(hass, config):
if not validate_config({DOMAIN: config}, if not validate_config({DOMAIN: config},
{DOMAIN: [CONF_USERNAME, {DOMAIN: [CONF_USERNAME,
CONF_PASSWORD, CONF_PASSWORD]},
CONF_PHONE_NUMBER]},
_LOGGER): _LOGGER):
return None return None
return GoogleVoiceSMSNotificationService(config[CONF_USERNAME], return GoogleVoiceSMSNotificationService(config[CONF_USERNAME],
config[CONF_PASSWORD], config[CONF_PHONE_NUMBER]) config[CONF_PASSWORD])
# pylint: disable=too-few-public-methods # pylint: disable=too-few-public-methods
class GoogleVoiceSMSNotificationService(BaseNotificationService): class GoogleVoiceSMSNotificationService(BaseNotificationService):
""" Implements notification service for the Google Voice SMS service. """ """ Implements notification service for the Google Voice SMS service. """
def __init__(self, username, password, phone_number): def __init__(self, username, password):
from googlevoicesms import Voice from googlevoicesms import Voice
self.voice = Voice() self.voice = Voice()
self.number = phone_number
self.username = username self.username = username
self.password = password self.password = password
def send_message(self, message="", **kwargs): def send_message(self, message="", **kwargs):
""" Send a message to the Free Mobile user cell. """ """ Send SMS to specified target user cell. """
resp = self.voice.login(self.username, self.password)
resp = self.voice.send_sms(self.number, message) targets = kwargs.get(ATTR_TARGET)
self.voice.login(self.username, self.password)
for target in targets:
resp = self.voice.send_sms(target, message)
self.voice.logout()
if resp.status_code == 400:
_LOGGER.error("At least one parameter is missing")
elif resp.status_code == 402:
_LOGGER.error("Too much SMS send in a few time")
elif resp.status_code == 403:
_LOGGER.error("Wrong Username/Password")
elif resp.status_code == 500:
_LOGGER.error("Server error, try later")

View file

@ -26,7 +26,6 @@ CONF_PASSWORD = "password"
CONF_API_KEY = "api_key" CONF_API_KEY = "api_key"
CONF_ACCESS_TOKEN = "access_token" CONF_ACCESS_TOKEN = "access_token"
CONF_FILENAME = "filename" CONF_FILENAME = "filename"
CONF_PHONE_NUMBER = "phone_number"
CONF_VALUE_TEMPLATE = "value_template" CONF_VALUE_TEMPLATE = "value_template"