Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
10 changes: 8 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,15 @@
vol.Optional('parse_str'): cv.boolean,
vol.Optional('link_target'): cv.string
})
MESSAGE_DATA_SCHEMA = vol.Schema({
vol.Optional('image_file'): cv.string,
vol.Optional('image_url'): 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
55 changes: 49 additions & 6 deletions homeassistant/components/hangouts/hangouts_bot.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
"""The Hangouts Bot."""
import io
import logging

import asyncio
import aiohttp
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 @@ -131,7 +134,8 @@ async def _async_handle_conversation_message(self,
is_error and conv_id in self._error_suppressed_conv_ids):
await self._async_send_message(
[{'text': message, 'parse_str': True}],
[{CONF_CONVERSATION_ID: conv_id}])
[{CONF_CONVERSATION_ID: conv_id}],
None)

async def _async_process(self, intents, text):
"""Detect a matching intent."""
Expand Down Expand Up @@ -185,7 +189,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 +218,48 @@ async def _async_send_message(self, message, targets):
segment_type=hangouts_pb2.
SEGMENT_TYPE_LINE_BREAK))

image_file = None
if data:
if data.get('image_url'):
uri = data.get('image_url')
try:
websession = async_get_clientsession(self.hass)
async with websession.get(uri, timeout=5) as response:
if response.status != 200:
_LOGGER.error(
'Fetch image failed, %s, %s',
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, %s',
type(error)
)
image_file = None
elif data.get('image_file'):
uri = data.get('image_file')
if self.hass.config.is_allowed_path(uri):
try:
image_file = open(uri, 'rb')
except IOError as error:
_LOGGER.error(
'Image file I/O error(%s): %s',
error.errno,
error.strerror
)
else:
_LOGGER.error('Path "%s" not allowed', uri)

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 +284,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_file' / 'image_url']
example: '{ "image_file": "file" }' or '{ "image_url": "url" }'