Move hangouts imports at top-level (#29055)

This commit is contained in:
Quentame 2019-11-26 00:44:11 +01:00 committed by Paulus Schoutsen
parent 1fde0d18ed
commit 112a3f5e9f
4 changed files with 50 additions and 48 deletions

View file

@ -1,16 +1,16 @@
"""Support for Hangouts.""" """Support for Hangouts."""
import logging import logging
from hangups.auth import GoogleAuthError
import voluptuous as vol import voluptuous as vol
from homeassistant import config_entries from homeassistant import config_entries
from homeassistant.components.conversation.util import create_matcher
from homeassistant.const import EVENT_HOMEASSISTANT_STOP from homeassistant.const import EVENT_HOMEASSISTANT_STOP
from homeassistant.helpers import dispatcher, intent from homeassistant.helpers import dispatcher, intent
import homeassistant.helpers.config_validation as cv import homeassistant.helpers.config_validation as cv
from homeassistant.components.conversation.util import create_matcher
# We need an import from .config_flow, without it .config_flow is never loaded. # We need an import from .config_flow, without it .config_flow is never loaded.
from .intents import HelpIntent
from .config_flow import HangoutsFlowHandler # noqa: F401 from .config_flow import HangoutsFlowHandler # noqa: F401
from .const import ( from .const import (
CONF_BOT, CONF_BOT,
@ -32,6 +32,8 @@ from .const import (
SERVICE_UPDATE, SERVICE_UPDATE,
TARGETS_SCHEMA, TARGETS_SCHEMA,
) )
from .hangouts_bot import HangoutsBot
from .intents import HelpIntent
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
@ -96,11 +98,7 @@ async def async_setup(hass, config):
async def async_setup_entry(hass, config): async def async_setup_entry(hass, config):
"""Set up a config entry.""" """Set up a config entry."""
from hangups.auth import GoogleAuthError
try: try:
from .hangouts_bot import HangoutsBot
bot = HangoutsBot( bot = HangoutsBot(
hass, hass,
config.data.get(CONF_REFRESH_TOKEN), config.data.get(CONF_REFRESH_TOKEN),

View file

@ -1,7 +1,8 @@
"""Config flow to configure Google Hangouts.""" """Config flow to configure Google Hangouts."""
import functools import functools
import voluptuous as vol
from hangups import get_auth
import voluptuous as vol
from homeassistant import config_entries from homeassistant import config_entries
from homeassistant.const import CONF_EMAIL, CONF_PASSWORD from homeassistant.const import CONF_EMAIL, CONF_PASSWORD
@ -9,10 +10,16 @@ from homeassistant.core import callback
from .const import ( from .const import (
CONF_2FA, CONF_2FA,
CONF_REFRESH_TOKEN,
CONF_AUTH_CODE, CONF_AUTH_CODE,
CONF_REFRESH_TOKEN,
DOMAIN as HANGOUTS_DOMAIN, DOMAIN as HANGOUTS_DOMAIN,
) )
from .hangups_utils import (
Google2FAError,
GoogleAuthError,
HangoutsCredentials,
HangoutsRefreshToken,
)
@callback @callback
@ -44,14 +51,6 @@ class HangoutsFlowHandler(config_entries.ConfigFlow):
return self.async_abort(reason="already_configured") return self.async_abort(reason="already_configured")
if user_input is not None: if user_input is not None:
from hangups import get_auth
from .hangups_utils import (
HangoutsCredentials,
HangoutsRefreshToken,
GoogleAuthError,
Google2FAError,
)
user_email = user_input[CONF_EMAIL] user_email = user_input[CONF_EMAIL]
user_password = user_input[CONF_PASSWORD] user_password = user_input[CONF_PASSWORD]
user_auth_code = user_input.get(CONF_AUTH_CODE) user_auth_code = user_input.get(CONF_AUTH_CODE)
@ -99,9 +98,6 @@ class HangoutsFlowHandler(config_entries.ConfigFlow):
errors = {} errors = {}
if user_input is not None: if user_input is not None:
from hangups import get_auth
from .hangups_utils import GoogleAuthError
self._credentials.set_verification_code(user_input[CONF_2FA]) self._credentials.set_verification_code(user_input[CONF_2FA])
try: try:
await self.hass.async_add_executor_job( await self.hass.async_add_executor_job(

View file

@ -4,6 +4,8 @@ import io
import logging import logging
import aiohttp import aiohttp
import hangups
from hangups import ChatMessageEvent, ChatMessageSegment, Client, get_auth, hangouts_pb2
from homeassistant.helpers import dispatcher, intent from homeassistant.helpers import dispatcher, intent
from homeassistant.helpers.aiohttp_client import async_get_clientsession from homeassistant.helpers.aiohttp_client import async_get_clientsession
@ -24,6 +26,7 @@ from .const import (
EVENT_HANGOUTS_MESSAGE_RECEIVED, EVENT_HANGOUTS_MESSAGE_RECEIVED,
INTENT_HELP, INTENT_HELP,
) )
from .hangups_utils import HangoutsCredentials, HangoutsRefreshToken
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
@ -126,8 +129,6 @@ class HangoutsBot:
) )
async def _async_handle_conversation_event(self, event): async def _async_handle_conversation_event(self, event):
from hangups import ChatMessageEvent
if isinstance(event, ChatMessageEvent): if isinstance(event, ChatMessageEvent):
dispatcher.async_dispatcher_send( dispatcher.async_dispatcher_send(
self.hass, self.hass,
@ -196,11 +197,6 @@ class HangoutsBot:
async def async_connect(self): async def async_connect(self):
"""Login to the Google Hangouts.""" """Login to the Google Hangouts."""
from .hangups_utils import HangoutsRefreshToken, HangoutsCredentials
from hangups import Client
from hangups import get_auth
session = await self.hass.async_add_executor_job( session = await self.hass.async_add_executor_job(
get_auth, get_auth,
HangoutsCredentials(None, None, None), HangoutsCredentials(None, None, None),
@ -252,8 +248,6 @@ class HangoutsBot:
if not conversations: if not conversations:
return False return False
from hangups import ChatMessageSegment, hangouts_pb2
messages = [] messages = []
for segment in message: for segment in message:
if messages: if messages:
@ -306,8 +300,6 @@ class HangoutsBot:
await conv.send_message(messages, image_file) await conv.send_message(messages, image_file)
async def _async_list_conversations(self): async def _async_list_conversations(self):
import hangups
( (
self._user_list, self._user_list,
self._conversation_list, self._conversation_list,

View file

@ -4,6 +4,10 @@ from unittest.mock import patch
from homeassistant import data_entry_flow from homeassistant import data_entry_flow
from homeassistant.components.hangouts import config_flow from homeassistant.components.hangouts import config_flow
from homeassistant.const import CONF_EMAIL, CONF_PASSWORD
EMAIL = "test@test.com"
PASSWORD = "1232456"
async def test_flow_works(hass, aioclient_mock): async def test_flow_works(hass, aioclient_mock):
@ -12,12 +16,12 @@ async def test_flow_works(hass, aioclient_mock):
flow.hass = hass flow.hass = hass
with patch("hangups.get_auth"): with patch("homeassistant.components.hangouts.config_flow.get_auth"):
result = await flow.async_step_user( result = await flow.async_step_user(
{"email": "test@test.com", "password": "1232456"} {CONF_EMAIL: EMAIL, CONF_PASSWORD: PASSWORD}
) )
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
assert result["title"] == "test@test.com" assert result["title"] == EMAIL
async def test_flow_works_with_authcode(hass, aioclient_mock): async def test_flow_works_with_authcode(hass, aioclient_mock):
@ -26,16 +30,16 @@ async def test_flow_works_with_authcode(hass, aioclient_mock):
flow.hass = hass flow.hass = hass
with patch("hangups.get_auth"): with patch("homeassistant.components.hangouts.config_flow.get_auth"):
result = await flow.async_step_user( result = await flow.async_step_user(
{ {
"email": "test@test.com", CONF_EMAIL: EMAIL,
"password": "1232456", CONF_PASSWORD: PASSWORD,
"authorization_code": "c29tZXJhbmRvbXN0cmluZw==", "authorization_code": "c29tZXJhbmRvbXN0cmluZw==",
} }
) )
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
assert result["title"] == "test@test.com" assert result["title"] == EMAIL
async def test_flow_works_with_2fa(hass, aioclient_mock): async def test_flow_works_with_2fa(hass, aioclient_mock):
@ -46,17 +50,20 @@ async def test_flow_works_with_2fa(hass, aioclient_mock):
flow.hass = hass flow.hass = hass
with patch("hangups.get_auth", side_effect=Google2FAError): with patch(
"homeassistant.components.hangouts.config_flow.get_auth",
side_effect=Google2FAError,
):
result = await flow.async_step_user( result = await flow.async_step_user(
{"email": "test@test.com", "password": "1232456"} {CONF_EMAIL: EMAIL, CONF_PASSWORD: PASSWORD}
) )
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
assert result["step_id"] == "2fa" assert result["step_id"] == "2fa"
with patch("hangups.get_auth"): with patch("homeassistant.components.hangouts.config_flow.get_auth"):
result = await flow.async_step_2fa({"2fa": 123456}) result = await flow.async_step_2fa({"2fa": 123456})
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
assert result["title"] == "test@test.com" assert result["title"] == EMAIL
async def test_flow_with_unknown_2fa(hass, aioclient_mock): async def test_flow_with_unknown_2fa(hass, aioclient_mock):
@ -68,11 +75,11 @@ async def test_flow_with_unknown_2fa(hass, aioclient_mock):
flow.hass = hass flow.hass = hass
with patch( with patch(
"hangups.get_auth", "homeassistant.components.hangouts.config_flow.get_auth",
side_effect=GoogleAuthError("Unknown verification code input"), side_effect=GoogleAuthError("Unknown verification code input"),
): ):
result = await flow.async_step_user( result = await flow.async_step_user(
{"email": "test@test.com", "password": "1232456"} {CONF_EMAIL: EMAIL, CONF_PASSWORD: PASSWORD}
) )
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
assert result["errors"]["base"] == "invalid_2fa_method" assert result["errors"]["base"] == "invalid_2fa_method"
@ -86,9 +93,12 @@ async def test_flow_invalid_login(hass, aioclient_mock):
flow.hass = hass flow.hass = hass
with patch("hangups.get_auth", side_effect=GoogleAuthError): with patch(
"homeassistant.components.hangouts.config_flow.get_auth",
side_effect=GoogleAuthError,
):
result = await flow.async_step_user( result = await flow.async_step_user(
{"email": "test@test.com", "password": "1232456"} {CONF_EMAIL: EMAIL, CONF_PASSWORD: PASSWORD}
) )
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
assert result["errors"]["base"] == "invalid_login" assert result["errors"]["base"] == "invalid_login"
@ -102,14 +112,20 @@ async def test_flow_invalid_2fa(hass, aioclient_mock):
flow.hass = hass flow.hass = hass
with patch("hangups.get_auth", side_effect=Google2FAError): with patch(
"homeassistant.components.hangouts.config_flow.get_auth",
side_effect=Google2FAError,
):
result = await flow.async_step_user( result = await flow.async_step_user(
{"email": "test@test.com", "password": "1232456"} {CONF_EMAIL: EMAIL, CONF_PASSWORD: PASSWORD}
) )
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
assert result["step_id"] == "2fa" assert result["step_id"] == "2fa"
with patch("hangups.get_auth", side_effect=Google2FAError): with patch(
"homeassistant.components.hangouts.config_flow.get_auth",
side_effect=Google2FAError,
):
result = await flow.async_step_2fa({"2fa": 123456}) result = await flow.async_step_2fa({"2fa": 123456})
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM assert result["type"] == data_entry_flow.RESULT_TYPE_FORM