Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 33 additions & 5 deletions homeassistant/components/hangouts/hangouts_bot.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
"""The Hangouts Bot."""
import io
import logging

import asyncio
import aiohttp
import async_timeout
Comment thread
quazzie marked this conversation as resolved.
Outdated
from urllib.parse import urlparse
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from homeassistant.helpers import dispatcher, intent

from .const import (
ATTR_MESSAGE, ATTR_TARGET, CONF_CONVERSATIONS, DOMAIN,
ATTR_MESSAGE, ATTR_TARGET, ATTR_DATA, CONF_CONVERSATIONS, DOMAIN,
EVENT_HANGOUTS_CONNECTED, EVENT_HANGOUTS_CONVERSATIONS_CHANGED,
EVENT_HANGOUTS_DISCONNECTED, EVENT_HANGOUTS_MESSAGE_RECEIVED,
CONF_MATCHERS, CONF_CONVERSATION_ID,
Expand Down Expand Up @@ -185,7 +190,7 @@ async def async_handle_hass_stop(self, _):
"""Run once when Home Assistant stops."""
await self.async_disconnect()

async def _async_send_message(self, message, targets):
async def _async_send_message(self, message, targets, data):
conversations = []
for target in targets:
conversation = None
Expand Down Expand Up @@ -214,10 +219,32 @@ async def _async_send_message(self, message, targets):
segment_type=hangouts_pb2.
SEGMENT_TYPE_LINE_BREAK))

image_file = None
if data.get('image') is not None:
uri = data.get('image')
Comment thread
quazzie marked this conversation as resolved.
Outdated
validate = urlparse(uri)
Comment thread
quazzie marked this conversation as resolved.
Outdated
if validate.scheme:
Comment thread
quazzie marked this conversation as resolved.
Outdated
try:
websession = async_get_clientsession(self.hass)
Comment thread
quazzie marked this conversation as resolved.
Outdated
with async_timeout.timeout(5, loop=self.hass.loop):
Comment thread
quazzie marked this conversation as resolved.
Outdated
response = await websession.get(uri)
if response.status != 200:
_LOGGER.error('Fetch image failed, {}, {}'.format(response.status, response))
Comment thread
quazzie marked this conversation as resolved.
Outdated
image_file = None
Comment thread
quazzie marked this conversation as resolved.
Outdated
else:
image_data = await response.read()
Comment thread
quazzie marked this conversation as resolved.
Outdated
image_file = io.BytesIO(image_data)
Comment thread
quazzie marked this conversation as resolved.
Outdated
image_file.name = "image.png"
Comment thread
quazzie marked this conversation as resolved.
Outdated
except (asyncio.TimeoutError, aiohttp.ClientError) as error:
_LOGGER.error('Failed to fetch image, {}'.format(type(error)))
Comment thread
quazzie marked this conversation as resolved.
Outdated
image_file = None
Comment thread
quazzie marked this conversation as resolved.
Outdated
else:
Comment thread
quazzie marked this conversation as resolved.
Outdated
image_file = open(uri, 'rb')

Comment thread
quazzie marked this conversation as resolved.
Outdated
if not messages:
return False
for conv in conversations:
await conv.send_message(messages)
await conv.send_message(messages, image_file)

async def _async_list_conversations(self):
import hangups
Expand All @@ -242,7 +269,8 @@ async def _async_list_conversations(self):
async def async_handle_send_message(self, service):
"""Handle the send_message service."""
await self._async_send_message(service.data[ATTR_MESSAGE],
service.data[ATTR_TARGET])
service.data[ATTR_TARGET],
service.data[ATTR_DATA])

async def async_handle_update_users_and_conversations(self, _=None):
"""Handle the update_users_and_conversations service."""
Expand Down
6 changes: 5 additions & 1 deletion homeassistant/components/hangouts/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,8 @@ send_message:
example: '[{"id": "UgxrXzVrARmjx_C6AZx4AaABAagBo-6UCw"}, {"name": "Test Conversation"}]'
message:
description: List of message segments, only the "text" field is required in every segment. [Required]
example: '[{"text":"test", "is_bold": false, "is_italic": false, "is_strikethrough": false, "is_underline": false, "parse_str": false, "link_target": "http://google.com"}, ...]'
example: '[{"text":"test", "is_bold": false, "is_italic": false, "is_strikethrough": false, "is_underline": false, "parse_str": false, "link_target": "http://google.com"}, ...]'
data:
description: Other options ['image']
example: '{ "image": "file or url" }'