Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 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
9 changes: 7 additions & 2 deletions homeassistant/components/hangouts/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@

import voluptuous as vol

from homeassistant.components.notify import ATTR_MESSAGE, ATTR_TARGET
from homeassistant.components.notify \
import ATTR_MESSAGE, ATTR_TARGET, ATTR_DATA
import homeassistant.helpers.config_validation as cv

_LOGGER = logging.getLogger('homeassistant.components.hangouts')
Expand Down Expand Up @@ -53,10 +54,14 @@
vol.Optional('parse_str'): cv.boolean,
vol.Optional('link_target'): cv.string
})
MESSAGE_DATA_SCHEMA = vol.Schema({
vol.Optional('image'): cv.string
})

MESSAGE_SCHEMA = vol.Schema({
vol.Required(ATTR_TARGET): [TARGETS_SCHEMA],
vol.Required(ATTR_MESSAGE): [MESSAGE_SEGMENT_SCHEMA]
vol.Required(ATTR_MESSAGE): [MESSAGE_SEGMENT_SCHEMA],
vol.Optional(ATTR_DATA): MESSAGE_DATA_SCHEMA
})

INTENT_SCHEMA = vol.All(
Expand Down
46 changes: 41 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,40 @@ 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')
validate = urlparse(uri)
if validate.scheme:
try:
websession = async_get_clientsession(self.hass)
with async_timeout.timeout(5, loop=self.hass.loop):
response = await websession.get(uri)
Comment thread
quazzie marked this conversation as resolved.
Outdated
if response.status != 200:
_LOGGER.error(
'Fetch image failed, {}, {}'.format(
response.status, response
)
)
image_file = None
else:
image_data = await response.read()
image_file = io.BytesIO(image_data)
image_file.name = "image.png"
except (asyncio.TimeoutError, aiohttp.ClientError) as error:
_LOGGER.error(
'Failed to fetch image, {}'.format(
type(error)
)
)
image_file = None
else:
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 +277,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" }'