2018-08-24 08:39:35 +00:00
|
|
|
"""
|
|
|
|
The hangouts bot component.
|
|
|
|
|
|
|
|
For more details about this platform, please refer to the documentation at
|
|
|
|
https://home-assistant.io/components/hangouts/
|
|
|
|
"""
|
|
|
|
import logging
|
|
|
|
|
|
|
|
import voluptuous as vol
|
|
|
|
|
|
|
|
from homeassistant import config_entries
|
2018-09-12 11:27:21 +00:00
|
|
|
from homeassistant.components.hangouts.intents import HelpIntent
|
2018-08-24 08:39:35 +00:00
|
|
|
from homeassistant.const import EVENT_HOMEASSISTANT_STOP
|
2018-09-12 11:27:21 +00:00
|
|
|
from homeassistant.helpers import intent
|
2018-08-24 08:39:35 +00:00
|
|
|
from homeassistant.helpers import dispatcher
|
2018-08-27 22:20:12 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2018-08-24 08:39:35 +00:00
|
|
|
|
|
|
|
from .const import (
|
2018-08-27 22:20:12 +00:00
|
|
|
CONF_BOT, CONF_INTENTS, CONF_REFRESH_TOKEN, DOMAIN,
|
2018-08-24 08:39:35 +00:00
|
|
|
EVENT_HANGOUTS_CONNECTED, EVENT_HANGOUTS_CONVERSATIONS_CHANGED,
|
|
|
|
MESSAGE_SCHEMA, SERVICE_SEND_MESSAGE,
|
2018-08-27 22:20:12 +00:00
|
|
|
SERVICE_UPDATE, CONF_SENTENCES, CONF_MATCHERS,
|
2018-09-12 11:27:21 +00:00
|
|
|
CONF_ERROR_SUPPRESSED_CONVERSATIONS, INTENT_SCHEMA, TARGETS_SCHEMA,
|
|
|
|
CONF_DEFAULT_CONVERSATIONS, EVENT_HANGOUTS_CONVERSATIONS_RESOLVED,
|
|
|
|
INTENT_HELP)
|
2018-08-27 22:20:12 +00:00
|
|
|
|
|
|
|
# We need an import from .config_flow, without it .config_flow is never loaded.
|
|
|
|
from .config_flow import HangoutsFlowHandler # noqa: F401
|
|
|
|
|
2018-10-16 18:09:34 +00:00
|
|
|
REQUIREMENTS = ['hangups==0.4.6']
|
2018-08-24 08:39:35 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2018-08-27 22:20:12 +00:00
|
|
|
CONFIG_SCHEMA = vol.Schema({
|
|
|
|
DOMAIN: vol.Schema({
|
|
|
|
vol.Optional(CONF_INTENTS, default={}): vol.Schema({
|
|
|
|
cv.string: INTENT_SCHEMA
|
|
|
|
}),
|
2018-09-12 11:27:21 +00:00
|
|
|
vol.Optional(CONF_DEFAULT_CONVERSATIONS, default=[]):
|
|
|
|
[TARGETS_SCHEMA],
|
2018-08-27 22:20:12 +00:00
|
|
|
vol.Optional(CONF_ERROR_SUPPRESSED_CONVERSATIONS, default=[]):
|
|
|
|
[TARGETS_SCHEMA]
|
|
|
|
})
|
|
|
|
}, extra=vol.ALLOW_EXTRA)
|
|
|
|
|
2018-08-24 08:39:35 +00:00
|
|
|
|
|
|
|
async def async_setup(hass, config):
|
|
|
|
"""Set up the Hangouts bot component."""
|
2018-08-27 22:20:12 +00:00
|
|
|
from homeassistant.components.conversation import create_matcher
|
|
|
|
|
|
|
|
config = config.get(DOMAIN)
|
|
|
|
if config is None:
|
2018-08-28 13:44:06 +00:00
|
|
|
hass.data[DOMAIN] = {
|
|
|
|
CONF_INTENTS: {},
|
2018-09-12 11:27:21 +00:00
|
|
|
CONF_DEFAULT_CONVERSATIONS: [],
|
2018-08-28 13:44:06 +00:00
|
|
|
CONF_ERROR_SUPPRESSED_CONVERSATIONS: [],
|
|
|
|
}
|
2018-08-27 22:20:12 +00:00
|
|
|
return True
|
|
|
|
|
2018-08-28 13:44:06 +00:00
|
|
|
hass.data[DOMAIN] = {
|
|
|
|
CONF_INTENTS: config[CONF_INTENTS],
|
2018-09-12 11:27:21 +00:00
|
|
|
CONF_DEFAULT_CONVERSATIONS: config[CONF_DEFAULT_CONVERSATIONS],
|
2018-08-28 13:44:06 +00:00
|
|
|
CONF_ERROR_SUPPRESSED_CONVERSATIONS:
|
|
|
|
config[CONF_ERROR_SUPPRESSED_CONVERSATIONS],
|
|
|
|
}
|
2018-08-24 08:39:35 +00:00
|
|
|
|
2018-09-12 11:27:21 +00:00
|
|
|
if (hass.data[DOMAIN][CONF_INTENTS] and
|
|
|
|
INTENT_HELP not in hass.data[DOMAIN][CONF_INTENTS]):
|
|
|
|
hass.data[DOMAIN][CONF_INTENTS][INTENT_HELP] = {
|
|
|
|
CONF_SENTENCES: ['HELP']}
|
|
|
|
|
2018-08-27 22:20:12 +00:00
|
|
|
for data in hass.data[DOMAIN][CONF_INTENTS].values():
|
|
|
|
matchers = []
|
|
|
|
for sentence in data[CONF_SENTENCES]:
|
|
|
|
matchers.append(create_matcher(sentence))
|
|
|
|
|
|
|
|
data[CONF_MATCHERS] = matchers
|
|
|
|
|
2018-08-28 13:44:06 +00:00
|
|
|
hass.async_create_task(hass.config_entries.flow.async_init(
|
2018-08-27 22:20:12 +00:00
|
|
|
DOMAIN, context={'source': config_entries.SOURCE_IMPORT}
|
|
|
|
))
|
2018-08-24 08:39:35 +00:00
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
async def async_setup_entry(hass, config):
|
|
|
|
"""Set up a config entry."""
|
|
|
|
from hangups.auth import GoogleAuthError
|
|
|
|
|
|
|
|
try:
|
|
|
|
from .hangouts_bot import HangoutsBot
|
|
|
|
|
|
|
|
bot = HangoutsBot(
|
|
|
|
hass,
|
|
|
|
config.data.get(CONF_REFRESH_TOKEN),
|
2018-08-27 22:20:12 +00:00
|
|
|
hass.data[DOMAIN][CONF_INTENTS],
|
2018-09-12 11:27:21 +00:00
|
|
|
hass.data[DOMAIN][CONF_DEFAULT_CONVERSATIONS],
|
2018-08-27 22:20:12 +00:00
|
|
|
hass.data[DOMAIN][CONF_ERROR_SUPPRESSED_CONVERSATIONS])
|
2018-08-24 08:39:35 +00:00
|
|
|
hass.data[DOMAIN][CONF_BOT] = bot
|
|
|
|
except GoogleAuthError as exception:
|
|
|
|
_LOGGER.error("Hangouts failed to log in: %s", str(exception))
|
|
|
|
return False
|
|
|
|
|
|
|
|
dispatcher.async_dispatcher_connect(
|
|
|
|
hass,
|
|
|
|
EVENT_HANGOUTS_CONNECTED,
|
|
|
|
bot.async_handle_update_users_and_conversations)
|
|
|
|
|
|
|
|
dispatcher.async_dispatcher_connect(
|
|
|
|
hass,
|
|
|
|
EVENT_HANGOUTS_CONVERSATIONS_CHANGED,
|
2018-09-12 11:27:21 +00:00
|
|
|
bot.async_resolve_conversations)
|
|
|
|
|
2018-08-27 22:20:12 +00:00
|
|
|
dispatcher.async_dispatcher_connect(
|
|
|
|
hass,
|
2018-09-12 11:27:21 +00:00
|
|
|
EVENT_HANGOUTS_CONVERSATIONS_RESOLVED,
|
|
|
|
bot.async_update_conversation_commands)
|
2018-08-24 08:39:35 +00:00
|
|
|
|
|
|
|
hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP,
|
|
|
|
bot.async_handle_hass_stop)
|
|
|
|
|
|
|
|
await bot.async_connect()
|
|
|
|
|
|
|
|
hass.services.async_register(DOMAIN, SERVICE_SEND_MESSAGE,
|
|
|
|
bot.async_handle_send_message,
|
|
|
|
schema=MESSAGE_SCHEMA)
|
|
|
|
hass.services.async_register(DOMAIN,
|
|
|
|
SERVICE_UPDATE,
|
|
|
|
bot.
|
|
|
|
async_handle_update_users_and_conversations,
|
|
|
|
schema=vol.Schema({}))
|
|
|
|
|
2018-09-12 11:27:21 +00:00
|
|
|
intent.async_register(hass, HelpIntent(hass))
|
|
|
|
|
2018-08-24 08:39:35 +00:00
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
async def async_unload_entry(hass, _):
|
|
|
|
"""Unload a config entry."""
|
|
|
|
bot = hass.data[DOMAIN].pop(CONF_BOT)
|
|
|
|
await bot.async_disconnect()
|
|
|
|
return True
|