From bb58ad0f54db39d8d1e9b0de0181f3586cfc71d7 Mon Sep 17 00:00:00 2001 From: Daniel Dietzler <36593685+danieldietzler@users.noreply.github.com> Date: Thu, 4 Aug 2022 22:08:24 +0200 Subject: [PATCH] Add ability to specify user(s) when sending DMs using the Twitter integration (#71310) --- homeassistant/components/twitter/notify.py | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/homeassistant/components/twitter/notify.py b/homeassistant/components/twitter/notify.py index fd97303a360..d64f8ec3fbf 100644 --- a/homeassistant/components/twitter/notify.py +++ b/homeassistant/components/twitter/notify.py @@ -12,6 +12,7 @@ import voluptuous as vol from homeassistant.components.notify import ( ATTR_DATA, + ATTR_TARGET, PLATFORM_SCHEMA, BaseNotificationService, ) @@ -63,7 +64,7 @@ class TwitterNotificationService(BaseNotificationService): username, ): """Initialize the service.""" - self.user = username + self.default_user = username self.hass = hass self.api = TwitterAPI( consumer_key, consumer_secret, access_token_key, access_token_secret @@ -72,6 +73,7 @@ class TwitterNotificationService(BaseNotificationService): def send_message(self, message="", **kwargs): """Tweet a message, optionally with media.""" data = kwargs.get(ATTR_DATA) + targets = kwargs.get(ATTR_TARGET) media = None if data: @@ -79,15 +81,19 @@ class TwitterNotificationService(BaseNotificationService): if not self.hass.config.is_allowed_path(media): _LOGGER.warning("'%s' is not a whitelisted directory", media) 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) - - self.upload_media_then_callback(callback, media) - - def send_message_callback(self, message, media_id=None): + def send_message_callback(self, message, user, media_id=None): """Tweet a message, optionally with media.""" - if self.user: - user_resp = self.api.request("users/lookup", {"screen_name": self.user}) + if user: + user_resp = self.api.request("users/lookup", {"screen_name": user}) user_id = user_resp.json()[0]["id"] if user_resp.status_code != HTTPStatus.OK: self.log_error_resp(user_resp)