diff --git a/hass_nabucasa/remote.py b/hass_nabucasa/remote.py index 93373356b..cad00d7f8 100644 --- a/hass_nabucasa/remote.py +++ b/hass_nabucasa/remote.py @@ -38,6 +38,7 @@ class SniTunToken: aes_key = attr.ib(type=bytes) aes_iv = attr.ib(type=bytes) valid = attr.ib(type=datetime) + throttling = attr.ib(type=int) @attr.s @@ -235,6 +236,7 @@ async def _refresh_snitun_token(self) -> None: aes_key, aes_iv, utils.utc_from_timestamp(data["valid"]), + data["throttling"] ) async def connect(self) -> None: @@ -250,7 +252,8 @@ async def connect(self) -> None: try: await self._refresh_snitun_token() await self._snitun.connect( - self._token.fernet, self._token.aes_key, self._token.aes_iv + self._token.fernet, self._token.aes_key, self._token.aes_iv, + throttling=self._token.throttling ) self.cloud.client.dispatcher_message(const.DISPATCH_REMOTE_CONNECT) diff --git a/setup.py b/setup.py index f951f41d3..82ea8f825 100644 --- a/setup.py +++ b/setup.py @@ -1,6 +1,6 @@ from setuptools import setup -VERSION = "0.7" +VERSION = "0.8" setup( name="hass-nabucasa", @@ -30,7 +30,7 @@ packages=["hass_nabucasa"], install_requires=[ "warrant==0.6.1", - "snitun==0.13", + "snitun==0.15", "acme==0.32.0", "cryptography>=2.5", "attrs>=18.2.0", diff --git a/tests/common.py b/tests/common.py index 02aad87c7..161bfc27b 100644 --- a/tests/common.py +++ b/tests/common.py @@ -181,10 +181,12 @@ async def stop(self): """Stop snitun.""" self.call_stop = True - async def connect(self, token: bytes, aes_key: bytes, aes_iv: bytes): + async def connect( + self, token: bytes, aes_key: bytes, aes_iv: bytes, throttling=None + ): """Connect snitun.""" self.call_connect = True - self.connect_args = [token, aes_key, aes_iv] + self.connect_args = [token, aes_key, aes_iv, throttling] async def disconnect(self): """Disconnect snitun.""" diff --git a/tests/test_cloud_api.py b/tests/test_cloud_api.py index e0fb957bf..9cdb2a0d5 100644 --- a/tests/test_cloud_api.py +++ b/tests/test_cloud_api.py @@ -56,7 +56,12 @@ async def test_remote_token(cloud_mock, aioclient_mock): """Test creating a cloudhook.""" aioclient_mock.post( "https://example.com/bla/snitun_token", - json={"token": "123456", "server": "rest-remote.nabu.casa", "valid": 12345}, + json={ + "token": "123456", + "server": "rest-remote.nabu.casa", + "valid": 12345, + "throttling": 400, + }, ) cloud_mock.id_token = "mock-id-token" cloud_mock.remote_api_url = "https://example.com/bla" @@ -67,6 +72,7 @@ async def test_remote_token(cloud_mock, aioclient_mock): "token": "123456", "server": "rest-remote.nabu.casa", "valid": 12345, + "throttling": 400, } assert aioclient_mock.mock_calls[0][2] == {"aes_iv": "6976", "aes_key": "616573"} diff --git a/tests/test_remote.py b/tests/test_remote.py index 311b975e9..cda55835b 100644 --- a/tests/test_remote.py +++ b/tests/test_remote.py @@ -70,6 +70,7 @@ async def test_load_backend_exists_cert( "token": "test-token", "server": "rest-remote.nabu.casa", "valid": valid.timestamp(), + "throttling": 400 }, ) @@ -93,6 +94,7 @@ async def test_load_backend_exists_cert( assert snitun_mock.call_connect assert snitun_mock.connect_args[0] == b"test-token" + assert snitun_mock.connect_args[3] == 400 assert remote.is_connected assert remote._acme_task @@ -124,6 +126,7 @@ async def test_load_backend_not_exists_cert( "token": "test-token", "server": "rest-remote.nabu.casa", "valid": valid.timestamp(), + "throttling": 400 }, ) @@ -147,6 +150,7 @@ async def test_load_backend_not_exists_cert( assert snitun_mock.call_connect assert snitun_mock.connect_args[0] == b"test-token" + assert snitun_mock.connect_args[3] == 400 assert remote._acme_task assert remote._reconnect_task @@ -174,6 +178,7 @@ async def test_load_and_unload_backend( "token": "test-token", "server": "rest-remote.nabu.casa", "valid": valid.timestamp(), + "throttling": 400 }, ) @@ -231,6 +236,7 @@ async def test_load_backend_exists_wrong_cert( "token": "test-token", "server": "rest-remote.nabu.casa", "valid": valid.timestamp(), + "throttling": 400 }, ) @@ -254,6 +260,7 @@ async def test_load_backend_exists_wrong_cert( assert snitun_mock.call_connect assert snitun_mock.connect_args[0] == b"test-token" + assert snitun_mock.connect_args[3] == 400 async def test_call_disconnect( @@ -278,6 +285,7 @@ async def test_call_disconnect( "token": "test-token", "server": "rest-remote.nabu.casa", "valid": valid.timestamp(), + "throttling": 400 }, ) @@ -314,6 +322,7 @@ async def test_load_backend_no_autostart( "token": "test-token", "server": "rest-remote.nabu.casa", "valid": valid.timestamp(), + "throttling": 400 }, ) @@ -331,6 +340,7 @@ async def test_load_backend_no_autostart( assert snitun_mock.call_connect assert snitun_mock.connect_args[0] == b"test-token" + assert snitun_mock.connect_args[3] == 400 assert cloud_mock.client.mock_dispatcher[-1][0] == DISPATCH_REMOTE_CONNECT @@ -358,6 +368,7 @@ async def test_get_certificate_details( "token": "test-token", "server": "rest-remote.nabu.casa", "valid": valid.timestamp(), + "throttling": 400 }, ) @@ -398,6 +409,7 @@ async def test_certificate_task_no_backend( "token": "test-token", "server": "rest-remote.nabu.casa", "valid": valid.timestamp(), + "throttling": 400 }, ) @@ -434,6 +446,7 @@ async def test_certificate_task_renew_cert( "token": "test-token", "server": "rest-remote.nabu.casa", "valid": valid.timestamp(), + "throttling": 400 }, )