Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move hangouts imports at top-level #29055

Merged
merged 1 commit into from Nov 25, 2019
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 4 additions & 6 deletions homeassistant/components/hangouts/__init__.py
@@ -1,16 +1,16 @@
"""Support for Hangouts."""
import logging

from hangups.auth import GoogleAuthError
import voluptuous as vol

from homeassistant import config_entries
from homeassistant.components.conversation.util import create_matcher
from homeassistant.const import EVENT_HOMEASSISTANT_STOP
from homeassistant.helpers import dispatcher, intent
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.
from .intents import HelpIntent
from .config_flow import HangoutsFlowHandler # noqa: F401
from .const import (
CONF_BOT,
Expand All @@ -32,6 +32,8 @@
SERVICE_UPDATE,
TARGETS_SCHEMA,
)
from .hangouts_bot import HangoutsBot
from .intents import HelpIntent

_LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -96,11 +98,7 @@ async def async_setup(hass, config):

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),
Expand Down
22 changes: 9 additions & 13 deletions homeassistant/components/hangouts/config_flow.py
@@ -1,18 +1,25 @@
"""Config flow to configure Google Hangouts."""
import functools
import voluptuous as vol

from hangups import get_auth
import voluptuous as vol

from homeassistant import config_entries
from homeassistant.const import CONF_EMAIL, CONF_PASSWORD
from homeassistant.core import callback

from .const import (
CONF_2FA,
CONF_REFRESH_TOKEN,
CONF_AUTH_CODE,
CONF_REFRESH_TOKEN,
DOMAIN as HANGOUTS_DOMAIN,
)
from .hangups_utils import (
Google2FAError,
GoogleAuthError,
HangoutsCredentials,
HangoutsRefreshToken,
)


@callback
Expand Down Expand Up @@ -44,14 +51,6 @@ async def async_step_user(self, user_input=None):
return self.async_abort(reason="already_configured")

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_password = user_input[CONF_PASSWORD]
user_auth_code = user_input.get(CONF_AUTH_CODE)
Expand Down Expand Up @@ -99,9 +98,6 @@ async def async_step_2fa(self, user_input=None):
errors = {}

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])
try:
await self.hass.async_add_executor_job(
Expand Down
14 changes: 3 additions & 11 deletions homeassistant/components/hangouts/hangouts_bot.py
Expand Up @@ -4,6 +4,8 @@
import logging

import aiohttp
import hangups
from hangups import ChatMessageEvent, ChatMessageSegment, Client, get_auth, hangouts_pb2

from homeassistant.helpers import dispatcher, intent
from homeassistant.helpers.aiohttp_client import async_get_clientsession
Expand All @@ -24,6 +26,7 @@
EVENT_HANGOUTS_MESSAGE_RECEIVED,
INTENT_HELP,
)
from .hangups_utils import HangoutsCredentials, HangoutsRefreshToken

_LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -126,8 +129,6 @@ def async_resolve_conversations(self, _):
)

async def _async_handle_conversation_event(self, event):
from hangups import ChatMessageEvent

if isinstance(event, ChatMessageEvent):
dispatcher.async_dispatcher_send(
self.hass,
Expand Down Expand Up @@ -196,11 +197,6 @@ async def _async_process(self, intents, text, conv_id):

async def async_connect(self):
"""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(
get_auth,
HangoutsCredentials(None, None, None),
Expand Down Expand Up @@ -252,8 +248,6 @@ async def _async_send_message(self, message, targets, data):
if not conversations:
return False

from hangups import ChatMessageSegment, hangouts_pb2

messages = []
for segment in message:
if messages:
Expand Down Expand Up @@ -306,8 +300,6 @@ async def _async_send_message(self, message, targets, data):
await conv.send_message(messages, image_file)

async def _async_list_conversations(self):
import hangups

(
self._user_list,
self._conversation_list,
Expand Down
52 changes: 34 additions & 18 deletions tests/components/hangouts/test_config_flow.py
Expand Up @@ -4,6 +4,10 @@

from homeassistant import data_entry_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):
Expand All @@ -12,12 +16,12 @@ async def test_flow_works(hass, aioclient_mock):

flow.hass = hass

with patch("hangups.get_auth"):
with patch("homeassistant.components.hangouts.config_flow.get_auth"):
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["title"] == "test@test.com"
assert result["title"] == EMAIL


async def test_flow_works_with_authcode(hass, aioclient_mock):
Expand All @@ -26,16 +30,16 @@ async def test_flow_works_with_authcode(hass, aioclient_mock):

flow.hass = hass

with patch("hangups.get_auth"):
with patch("homeassistant.components.hangouts.config_flow.get_auth"):
result = await flow.async_step_user(
{
"email": "test@test.com",
"password": "1232456",
CONF_EMAIL: EMAIL,
CONF_PASSWORD: PASSWORD,
"authorization_code": "c29tZXJhbmRvbXN0cmluZw==",
}
)
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):
Expand All @@ -46,17 +50,20 @@ async def test_flow_works_with_2fa(hass, aioclient_mock):

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(
{"email": "test@test.com", "password": "1232456"}
{CONF_EMAIL: EMAIL, CONF_PASSWORD: PASSWORD}
)
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
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})
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):
Expand All @@ -68,11 +75,11 @@ async def test_flow_with_unknown_2fa(hass, aioclient_mock):
flow.hass = hass

with patch(
"hangups.get_auth",
"homeassistant.components.hangouts.config_flow.get_auth",
side_effect=GoogleAuthError("Unknown verification code input"),
):
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["errors"]["base"] == "invalid_2fa_method"
Expand All @@ -86,9 +93,12 @@ async def test_flow_invalid_login(hass, aioclient_mock):

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(
{"email": "test@test.com", "password": "1232456"}
{CONF_EMAIL: EMAIL, CONF_PASSWORD: PASSWORD}
)
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
assert result["errors"]["base"] == "invalid_login"
Expand All @@ -102,14 +112,20 @@ async def test_flow_invalid_2fa(hass, aioclient_mock):

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(
{"email": "test@test.com", "password": "1232456"}
{CONF_EMAIL: EMAIL, CONF_PASSWORD: PASSWORD}
)
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
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})

assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
Expand Down