Add ability to specify user(s) when sending DMs using the Twitter integration (#71310)
parent
33bf94c4b2
commit
bb58ad0f54
|
@ -12,6 +12,7 @@ import voluptuous as vol
|
||||||
|
|
||||||
from homeassistant.components.notify import (
|
from homeassistant.components.notify import (
|
||||||
ATTR_DATA,
|
ATTR_DATA,
|
||||||
|
ATTR_TARGET,
|
||||||
PLATFORM_SCHEMA,
|
PLATFORM_SCHEMA,
|
||||||
BaseNotificationService,
|
BaseNotificationService,
|
||||||
)
|
)
|
||||||
|
@ -63,7 +64,7 @@ class TwitterNotificationService(BaseNotificationService):
|
||||||
username,
|
username,
|
||||||
):
|
):
|
||||||
"""Initialize the service."""
|
"""Initialize the service."""
|
||||||
self.user = username
|
self.default_user = username
|
||||||
self.hass = hass
|
self.hass = hass
|
||||||
self.api = TwitterAPI(
|
self.api = TwitterAPI(
|
||||||
consumer_key, consumer_secret, access_token_key, access_token_secret
|
consumer_key, consumer_secret, access_token_key, access_token_secret
|
||||||
|
@ -72,6 +73,7 @@ class TwitterNotificationService(BaseNotificationService):
|
||||||
def send_message(self, message="", **kwargs):
|
def send_message(self, message="", **kwargs):
|
||||||
"""Tweet a message, optionally with media."""
|
"""Tweet a message, optionally with media."""
|
||||||
data = kwargs.get(ATTR_DATA)
|
data = kwargs.get(ATTR_DATA)
|
||||||
|
targets = kwargs.get(ATTR_TARGET)
|
||||||
|
|
||||||
media = None
|
media = None
|
||||||
if data:
|
if data:
|
||||||
|
@ -79,15 +81,19 @@ class TwitterNotificationService(BaseNotificationService):
|
||||||
if not self.hass.config.is_allowed_path(media):
|
if not self.hass.config.is_allowed_path(media):
|
||||||
_LOGGER.warning("'%s' is not a whitelisted directory", media)
|
_LOGGER.warning("'%s' is not a whitelisted directory", media)
|
||||||
return
|
return
|
||||||
|
|
||||||
|
if targets:
|
||||||
|
for target in targets:
|
||||||
|
callback = partial(self.send_message_callback, message, target)
|
||||||
|
self.upload_media_then_callback(callback, media)
|
||||||
|
else:
|
||||||
|
callback = partial(self.send_message_callback, message, self.default_user)
|
||||||
|
self.upload_media_then_callback(callback, media)
|
||||||
|
|
||||||
callback = partial(self.send_message_callback, message)
|
def send_message_callback(self, message, user, media_id=None):
|
||||||
|
|
||||||
self.upload_media_then_callback(callback, media)
|
|
||||||
|
|
||||||
def send_message_callback(self, message, media_id=None):
|
|
||||||
"""Tweet a message, optionally with media."""
|
"""Tweet a message, optionally with media."""
|
||||||
if self.user:
|
if user:
|
||||||
user_resp = self.api.request("users/lookup", {"screen_name": self.user})
|
user_resp = self.api.request("users/lookup", {"screen_name": user})
|
||||||
user_id = user_resp.json()[0]["id"]
|
user_id = user_resp.json()[0]["id"]
|
||||||
if user_resp.status_code != HTTPStatus.OK:
|
if user_resp.status_code != HTTPStatus.OK:
|
||||||
self.log_error_resp(user_resp)
|
self.log_error_resp(user_resp)
|
||||||
|
|
Loading…
Reference in New Issue