Sync empty entities when Google is disabled in cloud (#72806)

This commit is contained in:
Paulus Schoutsen 2022-06-23 05:41:34 -04:00 committed by GitHub
parent 0dd181f922
commit 10b083bbf5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 44 additions and 13 deletions

View file

@ -210,11 +210,11 @@ class CloudClient(Interface):
async def async_google_message(self, payload: dict[Any, Any]) -> dict[Any, Any]:
"""Process cloud google message to client."""
if not self._prefs.google_enabled:
return ga.turned_off_response(payload)
gconf = await self.get_google_config()
if not self._prefs.google_enabled:
return ga.api_disabled_response(payload, gconf.agent_user_id)
return await ga.async_handle_message(
self._hass, gconf, gconf.cloud_user, payload, gc.SOURCE_CLOUD
)

View file

@ -219,6 +219,7 @@ class CloudGoogleConfig(AbstractConfig):
sync_entities = True
elif not self.enabled and self.is_local_sdk_active:
self.async_disable_local_sdk()
sync_entities = True
self._cur_entity_prefs = prefs.google_entity_configs
self._cur_default_expose = prefs.google_default_expose

View file

@ -356,9 +356,6 @@ class AbstractConfig(ABC):
pprint.pformat(payload),
)
if not self.enabled:
return json_response(smart_home.turned_off_response(payload))
if (agent_user_id := self.get_local_agent_user_id(webhook_id)) is None:
# No agent user linked to this webhook, means that the user has somehow unregistered
# removing webhook and stopping processing of this request.
@ -370,6 +367,11 @@ class AbstractConfig(ABC):
webhook.async_unregister(self.hass, webhook_id)
return None
if not self.enabled:
return json_response(
smart_home.api_disabled_response(payload, agent_user_id)
)
result = await smart_home.async_handle_message(
self.hass,
self,

View file

@ -99,7 +99,7 @@ async def async_devices_sync(hass, data, payload):
except Exception: # pylint: disable=broad-except
_LOGGER.exception("Error serializing %s", entity.entity_id)
response = {"agentUserId": agent_user_id, "devices": devices}
response = create_sync_response(agent_user_id, devices)
_LOGGER.debug("Syncing entities response: %s", response)
@ -300,9 +300,24 @@ async def async_devices_proxy_selected(hass, data: RequestData, payload):
return {}
def turned_off_response(message):
def create_sync_response(agent_user_id: str, devices: list):
"""Return an empty sync response."""
return {
"agentUserId": agent_user_id,
"devices": devices,
}
def api_disabled_response(message, agent_user_id):
"""Return a device turned off response."""
inputs: list = message.get("inputs")
if inputs and inputs[0].get("intent") == "action.devices.SYNC":
payload = create_sync_response(agent_user_id, [])
else:
payload = {"errorCode": "deviceTurnedOff"}
return {
"requestId": message.get("requestId"),
"payload": {"errorCode": "deviceTurnedOff"},
"payload": payload,
}

View file

@ -134,7 +134,16 @@ async def test_handler_google_actions(hass):
assert device["roomHint"] == "living room"
async def test_handler_google_actions_disabled(hass, mock_cloud_fixture):
@pytest.mark.parametrize(
"intent,response_payload",
[
("action.devices.SYNC", {"agentUserId": "myUserName", "devices": []}),
("action.devices.QUERY", {"errorCode": "deviceTurnedOff"}),
],
)
async def test_handler_google_actions_disabled(
hass, mock_cloud_fixture, intent, response_payload
):
"""Test handler Google Actions when user has disabled it."""
mock_cloud_fixture._prefs[PREF_ENABLE_GOOGLE] = False
@ -142,13 +151,17 @@ async def test_handler_google_actions_disabled(hass, mock_cloud_fixture):
assert await async_setup_component(hass, "cloud", {})
reqid = "5711642932632160983"
data = {"requestId": reqid, "inputs": [{"intent": "action.devices.SYNC"}]}
data = {"requestId": reqid, "inputs": [{"intent": intent}]}
cloud = hass.data["cloud"]
with patch(
"hass_nabucasa.Cloud._decode_claims",
return_value={"cognito:username": "myUserName"},
):
resp = await cloud.client.async_google_message(data)
assert resp["requestId"] == reqid
assert resp["payload"]["errorCode"] == "deviceTurnedOff"
assert resp["payload"] == response_payload
async def test_webhook_msg(hass, caplog):