Skip to content

Commit 307bf3e

Browse files
authored
Don't keep fetching snitun token when sub expired (#168)
1 parent f405e1f commit 307bf3e

File tree

3 files changed

+22
-3
lines changed

3 files changed

+22
-3
lines changed

hass_nabucasa/remote.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ class RemoteNotConnected(RemoteError):
3333
"""Raise if a request need connection and we are not ready."""
3434

3535

36+
class SubscriptionExpired(RemoteError):
37+
"""Raise if we cannot connect because subscription expired."""
38+
39+
3640
@attr.s
3741
class SniTunToken:
3842
"""Handle snitun token."""
@@ -241,13 +245,17 @@ async def _refresh_snitun_token(self) -> None:
241245
_LOGGER.debug("Don't need refresh snitun token")
242246
return
243247

248+
if self.cloud.subscription_expired:
249+
raise SubscriptionExpired()
250+
244251
# Generate session token
245252
aes_key, aes_iv = generate_aes_keyset()
246253
try:
247254
async with async_timeout.timeout(30):
248255
resp = await cloud_api.async_remote_token(self.cloud, aes_key, aes_iv)
249-
assert resp.status == 200
250-
except (asyncio.TimeoutError, AssertionError):
256+
if resp.status != 200:
257+
raise RemoteBackendError()
258+
except asyncio.TimeoutError:
251259
raise RemoteBackendError() from None
252260

253261
data = await resp.json()
@@ -283,6 +291,8 @@ async def connect(self) -> None:
283291
_LOGGER.error("Connection problem to snitun server")
284292
except RemoteBackendError:
285293
_LOGGER.error("Can't refresh the snitun token")
294+
except SubscriptionExpired:
295+
pass
286296
except AttributeError:
287297
pass # Ignore because HA shutdown on snitun token refresh
288298
finally:

tests/conftest.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ def _executor(call, *args):
4343
def auth_cloud_mock(cloud_mock):
4444
"""Return an authenticated cloud instance."""
4545
cloud_mock.auth.async_check_token.side_effect = mock_coro
46+
cloud_mock.subscription_expired = False
4647
return cloud_mock
4748

4849

tests/test_remote.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
DISPATCH_REMOTE_CONNECT,
1212
DISPATCH_REMOTE_DISCONNECT,
1313
)
14-
from hass_nabucasa.remote import RemoteUI
14+
from hass_nabucasa.remote import RemoteUI, SubscriptionExpired
1515
from hass_nabucasa.utils import utcnow
1616

1717
from .common import MockAcme, MockSnitun, mock_coro
@@ -468,3 +468,11 @@ async def test_certificate_task_renew_cert(
468468
await remote.load_backend()
469469
await asyncio.sleep(0.1)
470470
assert acme_mock.call_issue
471+
472+
473+
async def test_refresh_token_no_sub(auth_cloud_mock):
474+
"""Test that we rais SubscriptionExpired if expired sub."""
475+
auth_cloud_mock.subscription_expired = True
476+
477+
with pytest.raises(SubscriptionExpired):
478+
await RemoteUI(auth_cloud_mock)._refresh_snitun_token()

0 commit comments

Comments
 (0)