Files
home-assistant-core/homeassistant/components/hangouts/__init__.py
T
Marcel Hoppe ef0eab0f40 Hangouts (#16049)
* add a component for hangouts

* add a notify component for hangouts

* add an extra message as title

* add support to listen to all conversations hangouts has

* move hangouts to package and add parameter documentation

* update .coveragerc and requirements_all.txt

* makes linter happy again

* bugfix

* add conversations parameter to command words

* Move the resolution of conversation names to conversations in own a function

* typo

* rename group of exclusion form 'id' to 'id or name'

* refactoring and use config_flow

* makes linter happy again

* remove unused imports

* fix not working regex commands

* fix translations

* cleanup

* remove step_init

* remove logging entry

* clean up events

* move constant

* remove unsed import

* add new files to .converagerc

* isort imports

* add hangouts_utils to ignored packages

* upadte doc and format

* fix I/O not in executor jon

* rename SERVICE_UPDATE_USERS_AND_CONVERSATIONS to SERVICE_UPDATE

* move EVENT_HANGOUTS_{CONNECTED,DISCONNECTED} to dispatcher

* add config flow tests

* Update tox.ini
2018-08-24 10:39:35 +02:00

88 lines
2.6 KiB
Python

"""
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
from homeassistant.const import EVENT_HOMEASSISTANT_STOP
from homeassistant.helpers import dispatcher
from .config_flow import configured_hangouts
from .const import (
CONF_BOT, CONF_COMMANDS, CONF_REFRESH_TOKEN, DOMAIN,
EVENT_HANGOUTS_CONNECTED, EVENT_HANGOUTS_CONVERSATIONS_CHANGED,
MESSAGE_SCHEMA, SERVICE_SEND_MESSAGE,
SERVICE_UPDATE)
REQUIREMENTS = ['hangups==0.4.5']
_LOGGER = logging.getLogger(__name__)
async def async_setup(hass, config):
"""Set up the Hangouts bot component."""
config = config.get(DOMAIN, [])
hass.data[DOMAIN] = {CONF_COMMANDS: config[CONF_COMMANDS]}
if configured_hangouts(hass) is None:
hass.async_add_job(hass.config_entries.flow.async_init(
DOMAIN, context={'source': config_entries.SOURCE_IMPORT}
))
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),
hass.data[DOMAIN][CONF_COMMANDS])
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,
bot.async_update_conversation_commands)
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({}))
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