Skip to content

Commit

Permalink
Merge pull request #70 from NabuCasa/dev
Browse files Browse the repository at this point in the history
0.15
  • Loading branch information
balloob authored Jun 14, 2019
2 parents b58090e + 2462a38 commit b81dd87
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 2 deletions.
5 changes: 5 additions & 0 deletions hass_nabucasa/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ def __init__(
subscription_info_url=None,
cloudhook_create_url=None,
remote_api_url=None,
alexa_access_token_url=None,
acme_directory_server=None,
):
"""Create an instance of Cloud."""
Expand All @@ -56,6 +57,7 @@ def __init__(
self.subscription_info_url = subscription_info_url
self.cloudhook_create_url = cloudhook_create_url
self.remote_api_url = remote_api_url
self.alexa_access_token_url = alexa_access_token_url
self.acme_directory_server = acme_directory_server

else:
Expand All @@ -69,6 +71,7 @@ def __init__(
self.subscription_info_url = info["subscription_info_url"]
self.cloudhook_create_url = info["cloudhook_create_url"]
self.remote_api_url = info["remote_api_url"]
self.alexa_access_token_url = info["alexa_access_token_url"]
self.acme_directory_server = info["acme_directory_server"]

@property
Expand Down Expand Up @@ -184,6 +187,8 @@ def load_config():

info = await self.run_executor(load_config)

await self.client.async_initialize(self)

if info is None:
return

Expand Down
9 changes: 8 additions & 1 deletion hass_nabucasa/client.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
"""Client interface for Home Assistant to cloud."""
import asyncio
from pathlib import Path
from typing import Dict, Any
from typing import Dict, Any, TYPE_CHECKING

import aiohttp

if TYPE_CHECKING:
from . import Cloud


class CloudClient:
"""Interface class for Home Assistant."""
Expand Down Expand Up @@ -39,6 +42,10 @@ def remote_autostart(self) -> bool:
"""Return true if we want start a remote connection."""
raise NotImplementedError()

async def async_initialize(self, cloud: "Cloud") -> None:
"""Initialize the client."""
raise NotImplementedError()

async def cleanups(self) -> None:
"""Called on logout."""
raise NotImplementedError()
Expand Down
8 changes: 8 additions & 0 deletions hass_nabucasa/cloud_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,11 @@ async def async_remote_challenge_cleanup(cloud, txt: str):
return await cloud.websession.post(
url, headers={AUTHORIZATION: cloud.id_token}, json={"txt": txt}
)


@_check_token
async def async_alexa_access_token(cloud):
"""Create a cloudhook."""
return await cloud.websession.post(
cloud.alexa_access_token_url, headers={AUTHORIZATION: cloud.id_token}
)
1 change: 1 addition & 0 deletions hass_nabucasa/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
),
"cloudhook_create_url": "https://webhooks-api.nabucasa.com/generate",
"remote_api_url": "https://remote-sni-api.nabucasa.com",
"alexa_access_token_url": "https://alexa-api.nabucasa.com/access_token",
"acme_directory_server": "https://acme-v02.api.letsencrypt.org/directory",
}
}
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from setuptools import setup

VERSION = "0.14"
VERSION = "0.15"

setup(
name="hass-nabucasa",
Expand Down
5 changes: 5 additions & 0 deletions tests/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import tempfile
from typing import Optional, Any

from hass_nabucasa import Cloud
from hass_nabucasa.client import CloudClient


Expand Down Expand Up @@ -73,6 +74,10 @@ def remote_autostart(self) -> bool:
"""Return true if we want start a remote connection."""
return self.prop_remote_autostart

async def async_initialize(self, cloud: "Cloud") -> None:
"""Initialize the client."""
self.cloud = cloud

async def cleanups(self):
"""Need nothing to do."""

Expand Down
10 changes: 10 additions & 0 deletions tests/test_cloud_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,13 @@ async def test_remote_challenge_cleanup(cloud_mock, aioclient_mock):
resp = await cloud_api.async_remote_challenge_cleanup(cloud_mock, "123456")
assert len(aioclient_mock.mock_calls) == 1
assert aioclient_mock.mock_calls[0][2] == {"txt": "123456"}


async def test_get_access_token(cloud_mock, aioclient_mock):
"""Test creating a cloudhook."""
aioclient_mock.post("https://example.com/bla")
cloud_mock.id_token = "mock-id-token"
cloud_mock.alexa_access_token_url = "https://example.com/bla"

resp = await cloud_api.async_alexa_access_token(cloud_mock)
assert len(aioclient_mock.mock_calls) == 1
1 change: 1 addition & 0 deletions tests/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ def test_constructor_loads_info_from_constant(cloud_client):
"subscription_info_url": "test-subscription-info-url",
"cloudhook_create_url": "test-cloudhook_create_url",
"remote_api_url": "test-remote_api_url",
"alexa_access_token_url": "test-alexa-token-url",
"acme_directory_server": "test-acme-directory-server",
}
},
Expand Down

0 comments on commit b81dd87

Please sign in to comment.