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

This commit is contained in:
Paulus Schoutsen
2022-06-23 11:41:34 +02:00
committed by GitHub
parent 0dd181f922
commit 10b083bbf5
5 changed files with 44 additions and 13 deletions
+3 -3
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
)
@@ -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
@@ -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,
@@ -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,
}