Simplify cloud request connection handling (#56243)
Co-authored-by: Pascal Vizeli <pascal.vizeli@syshack.ch>
This commit is contained in:
parent
ac053388b4
commit
e34c985534
9 changed files with 41 additions and 16 deletions
|
@ -1,4 +1,6 @@
|
|||
"""Component to integrate the Home Assistant cloud."""
|
||||
import asyncio
|
||||
|
||||
from hass_nabucasa import Cloud
|
||||
import voluptuous as vol
|
||||
|
||||
|
@ -193,13 +195,13 @@ async def async_setup(hass, config):
|
|||
|
||||
hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, _shutdown)
|
||||
|
||||
_remote_handle_prefs_updated(cloud)
|
||||
|
||||
async def _service_handler(service):
|
||||
"""Handle service for cloud."""
|
||||
if service.service == SERVICE_REMOTE_CONNECT:
|
||||
await cloud.remote.connect()
|
||||
await prefs.async_update(remote_enabled=True)
|
||||
elif service.service == SERVICE_REMOTE_DISCONNECT:
|
||||
await cloud.remote.disconnect()
|
||||
await prefs.async_update(remote_enabled=False)
|
||||
|
||||
hass.helpers.service.async_register_admin_service(
|
||||
|
@ -234,3 +236,28 @@ async def async_setup(hass, config):
|
|||
account_link.async_setup(hass)
|
||||
|
||||
return True
|
||||
|
||||
|
||||
@callback
|
||||
def _remote_handle_prefs_updated(cloud: Cloud) -> None:
|
||||
"""Handle remote preferences updated."""
|
||||
cur_pref = cloud.client.prefs.remote_enabled
|
||||
lock = asyncio.Lock()
|
||||
|
||||
# Sync remote connection with prefs
|
||||
async def remote_prefs_updated(prefs: CloudPreferences) -> None:
|
||||
"""Update remote status."""
|
||||
nonlocal cur_pref
|
||||
|
||||
async with lock:
|
||||
if prefs.remote_enabled == cur_pref:
|
||||
return
|
||||
|
||||
cur_pref = prefs.remote_enabled
|
||||
|
||||
if cur_pref:
|
||||
await cloud.remote.connect()
|
||||
else:
|
||||
await cloud.remote.disconnect()
|
||||
|
||||
cloud.client.prefs.async_listen_updates(remote_prefs_updated)
|
||||
|
|
|
@ -172,6 +172,10 @@ class CloudClient(Interface):
|
|||
if identifier.startswith("remote_"):
|
||||
async_dispatcher_send(self._hass, DISPATCHER_REMOTE_UPDATE, data)
|
||||
|
||||
async def async_cloud_connect_update(self, connect: bool) -> None:
|
||||
"""Process cloud remote message to client."""
|
||||
await self._prefs.async_update(remote_enabled=connect)
|
||||
|
||||
async def async_alexa_message(self, payload: dict[Any, Any]) -> dict[Any, Any]:
|
||||
"""Process cloud alexa message to client."""
|
||||
cloud_user = await self._prefs.get_cloud_user()
|
||||
|
|
|
@ -452,7 +452,6 @@ async def websocket_remote_connect(hass, connection, msg):
|
|||
"""Handle request for connect remote."""
|
||||
cloud = hass.data[DOMAIN]
|
||||
await cloud.client.prefs.async_update(remote_enabled=True)
|
||||
await cloud.remote.connect()
|
||||
connection.send_result(msg["id"], await _account_data(cloud))
|
||||
|
||||
|
||||
|
@ -465,7 +464,6 @@ async def websocket_remote_disconnect(hass, connection, msg):
|
|||
"""Handle request for disconnect remote."""
|
||||
cloud = hass.data[DOMAIN]
|
||||
await cloud.client.prefs.async_update(remote_enabled=False)
|
||||
await cloud.remote.disconnect()
|
||||
connection.send_result(msg["id"], await _account_data(cloud))
|
||||
|
||||
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
"domain": "cloud",
|
||||
"name": "Home Assistant Cloud",
|
||||
"documentation": "https://www.home-assistant.io/integrations/cloud",
|
||||
"requirements": ["hass-nabucasa==0.49.0"],
|
||||
"requirements": ["hass-nabucasa==0.50.0"],
|
||||
"dependencies": ["http", "webhook"],
|
||||
"after_dependencies": ["google_assistant", "alexa"],
|
||||
"codeowners": ["@home-assistant/cloud"],
|
||||
|
|
|
@ -15,7 +15,7 @@ ciso8601==2.1.3
|
|||
cryptography==3.4.8
|
||||
defusedxml==0.7.1
|
||||
emoji==1.2.0
|
||||
hass-nabucasa==0.49.0
|
||||
hass-nabucasa==0.50.0
|
||||
home-assistant-frontend==20210911.0
|
||||
httpx==0.19.0
|
||||
ifaddr==0.1.7
|
||||
|
|
|
@ -769,7 +769,7 @@ habitipy==0.2.0
|
|||
hangups==0.4.14
|
||||
|
||||
# homeassistant.components.cloud
|
||||
hass-nabucasa==0.49.0
|
||||
hass-nabucasa==0.50.0
|
||||
|
||||
# homeassistant.components.splunk
|
||||
hass_splunk==0.1.1
|
||||
|
|
|
@ -456,7 +456,7 @@ habitipy==0.2.0
|
|||
hangups==0.4.14
|
||||
|
||||
# homeassistant.components.cloud
|
||||
hass-nabucasa==0.49.0
|
||||
hass-nabucasa==0.50.0
|
||||
|
||||
# homeassistant.components.tasmota
|
||||
hatasmota==0.2.20
|
||||
|
|
|
@ -549,14 +549,8 @@ async def test_enabling_remote(hass, hass_ws_client, setup_api, mock_cloud_login
|
|||
|
||||
assert len(mock_connect.mock_calls) == 1
|
||||
|
||||
|
||||
async def test_disabling_remote(hass, hass_ws_client, setup_api, mock_cloud_login):
|
||||
"""Test we call right code to disable remote UI."""
|
||||
client = await hass_ws_client(hass)
|
||||
cloud = hass.data[DOMAIN]
|
||||
|
||||
with patch("hass_nabucasa.remote.RemoteUI.disconnect") as mock_disconnect:
|
||||
await client.send_json({"id": 5, "type": "cloud/remote/disconnect"})
|
||||
await client.send_json({"id": 6, "type": "cloud/remote/disconnect"})
|
||||
response = await client.receive_json()
|
||||
assert response["success"]
|
||||
assert not cloud.client.remote_autostart
|
||||
|
|
|
@ -163,7 +163,9 @@ async def test_remote_ui_url(hass, mock_cloud_fixture):
|
|||
with pytest.raises(cloud.CloudNotAvailable):
|
||||
cloud.async_remote_ui_url(hass)
|
||||
|
||||
await cl.client.prefs.async_update(remote_enabled=True)
|
||||
with patch.object(cl.remote, "connect"):
|
||||
await cl.client.prefs.async_update(remote_enabled=True)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
# No instance domain
|
||||
with pytest.raises(cloud.CloudNotAvailable):
|
||||
|
|
Loading…
Add table
Reference in a new issue