mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 21:27:38 +00:00
Allow direct messaging to user (#5521)
This commit is contained in:
parent
b7e477fbba
commit
a83b61ad58
@ -11,7 +11,7 @@ import voluptuous as vol
|
|||||||
import homeassistant.helpers.config_validation as cv
|
import homeassistant.helpers.config_validation as cv
|
||||||
from homeassistant.components.notify import (
|
from homeassistant.components.notify import (
|
||||||
PLATFORM_SCHEMA, BaseNotificationService)
|
PLATFORM_SCHEMA, BaseNotificationService)
|
||||||
from homeassistant.const import CONF_ACCESS_TOKEN
|
from homeassistant.const import CONF_ACCESS_TOKEN, CONF_USERNAME
|
||||||
|
|
||||||
REQUIREMENTS = ['TwitterAPI==2.4.3']
|
REQUIREMENTS = ['TwitterAPI==2.4.3']
|
||||||
|
|
||||||
@ -22,10 +22,11 @@ CONF_CONSUMER_SECRET = 'consumer_secret'
|
|||||||
CONF_ACCESS_TOKEN_SECRET = 'access_token_secret'
|
CONF_ACCESS_TOKEN_SECRET = 'access_token_secret'
|
||||||
|
|
||||||
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
||||||
vol.Required(CONF_CONSUMER_KEY): cv.string,
|
|
||||||
vol.Required(CONF_CONSUMER_SECRET): cv.string,
|
|
||||||
vol.Required(CONF_ACCESS_TOKEN): cv.string,
|
vol.Required(CONF_ACCESS_TOKEN): cv.string,
|
||||||
vol.Required(CONF_ACCESS_TOKEN_SECRET): cv.string,
|
vol.Required(CONF_ACCESS_TOKEN_SECRET): cv.string,
|
||||||
|
vol.Required(CONF_CONSUMER_KEY): cv.string,
|
||||||
|
vol.Required(CONF_CONSUMER_SECRET): cv.string,
|
||||||
|
vol.Optional(CONF_USERNAME): cv.string,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
||||||
@ -33,7 +34,8 @@ def get_service(hass, config, discovery_info=None):
|
|||||||
"""Get the Twitter notification service."""
|
"""Get the Twitter notification service."""
|
||||||
return TwitterNotificationService(
|
return TwitterNotificationService(
|
||||||
config[CONF_CONSUMER_KEY], config[CONF_CONSUMER_SECRET],
|
config[CONF_CONSUMER_KEY], config[CONF_CONSUMER_SECRET],
|
||||||
config[CONF_ACCESS_TOKEN], config[CONF_ACCESS_TOKEN_SECRET]
|
config[CONF_ACCESS_TOKEN], config[CONF_ACCESS_TOKEN_SECRET],
|
||||||
|
config.get(CONF_USERNAME)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@ -41,15 +43,21 @@ class TwitterNotificationService(BaseNotificationService):
|
|||||||
"""Implementation of a notification service for the Twitter service."""
|
"""Implementation of a notification service for the Twitter service."""
|
||||||
|
|
||||||
def __init__(self, consumer_key, consumer_secret, access_token_key,
|
def __init__(self, consumer_key, consumer_secret, access_token_key,
|
||||||
access_token_secret):
|
access_token_secret, username):
|
||||||
"""Initialize the service."""
|
"""Initialize the service."""
|
||||||
from TwitterAPI import TwitterAPI
|
from TwitterAPI import TwitterAPI
|
||||||
|
self.user = username
|
||||||
self.api = TwitterAPI(consumer_key, consumer_secret, access_token_key,
|
self.api = TwitterAPI(consumer_key, consumer_secret, access_token_key,
|
||||||
access_token_secret)
|
access_token_secret)
|
||||||
|
|
||||||
def send_message(self, message="", **kwargs):
|
def send_message(self, message="", **kwargs):
|
||||||
"""Tweet some message."""
|
"""Tweet a message."""
|
||||||
resp = self.api.request('statuses/update', {'status': message})
|
if self.user:
|
||||||
|
resp = self.api.request(
|
||||||
|
'direct_messages/new', {'text': message, 'user': self.user})
|
||||||
|
else:
|
||||||
|
resp = self.api.request('statuses/update', {'status': message})
|
||||||
|
|
||||||
if resp.status_code != 200:
|
if resp.status_code != 200:
|
||||||
import json
|
import json
|
||||||
obj = json.loads(resp.text)
|
obj = json.loads(resp.text)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user