Add instance id to the cloud integration (#103162)

This commit is contained in:
Joakim Sørensen 2023-11-07 23:52:18 +01:00 committed by GitHub
parent 947ce592c1
commit 2859055b36
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 27 additions and 2 deletions

View file

@ -230,6 +230,7 @@ class CloudClient(Interface):
"alias": self.cloud.remote.alias, "alias": self.cloud.remote.alias,
}, },
"version": HA_VERSION, "version": HA_VERSION,
"instance_id": self.prefs.instance_id,
} }
async def async_alexa_message(self, payload: dict[Any, Any]) -> dict[Any, Any]: async def async_alexa_message(self, payload: dict[Any, Any]) -> dict[Any, Any]:

View file

@ -13,6 +13,7 @@ PREF_GOOGLE_REPORT_STATE = "google_report_state"
PREF_ALEXA_ENTITY_CONFIGS = "alexa_entity_configs" PREF_ALEXA_ENTITY_CONFIGS = "alexa_entity_configs"
PREF_ALEXA_REPORT_STATE = "alexa_report_state" PREF_ALEXA_REPORT_STATE = "alexa_report_state"
PREF_DISABLE_2FA = "disable_2fa" PREF_DISABLE_2FA = "disable_2fa"
PREF_INSTANCE_ID = "instance_id"
PREF_SHOULD_EXPOSE = "should_expose" PREF_SHOULD_EXPOSE = "should_expose"
PREF_GOOGLE_LOCAL_WEBHOOK_ID = "google_local_webhook_id" PREF_GOOGLE_LOCAL_WEBHOOK_ID = "google_local_webhook_id"
PREF_USERNAME = "username" PREF_USERNAME = "username"

View file

@ -3,6 +3,7 @@ from __future__ import annotations
from collections.abc import Callable, Coroutine from collections.abc import Callable, Coroutine
from typing import Any from typing import Any
import uuid
from homeassistant.auth.const import GROUP_ID_ADMIN from homeassistant.auth.const import GROUP_ID_ADMIN
from homeassistant.auth.models import User from homeassistant.auth.models import User
@ -33,6 +34,7 @@ from .const import (
PREF_GOOGLE_REPORT_STATE, PREF_GOOGLE_REPORT_STATE,
PREF_GOOGLE_SECURE_DEVICES_PIN, PREF_GOOGLE_SECURE_DEVICES_PIN,
PREF_GOOGLE_SETTINGS_VERSION, PREF_GOOGLE_SETTINGS_VERSION,
PREF_INSTANCE_ID,
PREF_REMOTE_DOMAIN, PREF_REMOTE_DOMAIN,
PREF_TTS_DEFAULT_VOICE, PREF_TTS_DEFAULT_VOICE,
PREF_USERNAME, PREF_USERNAME,
@ -91,6 +93,13 @@ class CloudPreferences:
PREF_GOOGLE_LOCAL_WEBHOOK_ID: webhook.async_generate_id(), PREF_GOOGLE_LOCAL_WEBHOOK_ID: webhook.async_generate_id(),
} }
) )
if PREF_INSTANCE_ID not in self._prefs:
await self._save_prefs(
{
**self._prefs,
PREF_INSTANCE_ID: uuid.uuid4().hex,
}
)
@callback @callback
def async_listen_updates( def async_listen_updates(
@ -264,6 +273,11 @@ class CloudPreferences:
"""Return the published cloud webhooks.""" """Return the published cloud webhooks."""
return self._prefs.get(PREF_CLOUDHOOKS, {}) # type: ignore[no-any-return] return self._prefs.get(PREF_CLOUDHOOKS, {}) # type: ignore[no-any-return]
@property
def instance_id(self) -> str | None:
"""Return the instance ID."""
return self._prefs.get(PREF_INSTANCE_ID)
@property @property
def tts_default_voice(self) -> tuple[str, str]: def tts_default_voice(self) -> tuple[str, str]:
"""Return the default TTS voice.""" """Return the default TTS voice."""
@ -320,6 +334,7 @@ class CloudPreferences:
PREF_GOOGLE_ENTITY_CONFIGS: {}, PREF_GOOGLE_ENTITY_CONFIGS: {},
PREF_GOOGLE_SETTINGS_VERSION: GOOGLE_SETTINGS_VERSION, PREF_GOOGLE_SETTINGS_VERSION: GOOGLE_SETTINGS_VERSION,
PREF_GOOGLE_LOCAL_WEBHOOK_ID: webhook.async_generate_id(), PREF_GOOGLE_LOCAL_WEBHOOK_ID: webhook.async_generate_id(),
PREF_INSTANCE_ID: uuid.uuid4().hex,
PREF_GOOGLE_SECURE_DEVICES_PIN: None, PREF_GOOGLE_SECURE_DEVICES_PIN: None,
PREF_REMOTE_DOMAIN: None, PREF_REMOTE_DOMAIN: None,
PREF_USERNAME: username, PREF_USERNAME: username,

View file

@ -13,6 +13,7 @@
"alexa_enabled": "Alexa Enabled", "alexa_enabled": "Alexa Enabled",
"google_enabled": "Google Enabled", "google_enabled": "Google Enabled",
"logged_in": "Logged In", "logged_in": "Logged In",
"instance_id": "Instance ID",
"subscription_expiration": "Subscription Expiration" "subscription_expiration": "Subscription Expiration"
} }
}, },

View file

@ -37,6 +37,7 @@ async def system_health_info(hass: HomeAssistant) -> dict[str, Any]:
data["google_enabled"] = client.prefs.google_enabled data["google_enabled"] = client.prefs.google_enabled
data["remote_server"] = cloud.remote.snitun_server data["remote_server"] = cloud.remote.snitun_server
data["certificate_status"] = cloud.remote.certificate_status data["certificate_status"] = cloud.remote.certificate_status
data["instance_id"] = client.prefs.instance_id
data["can_reach_cert_server"] = system_health.async_check_can_reach_url( data["can_reach_cert_server"] = system_health.async_check_can_reach_url(
hass, f"https://{cloud.acme_server}/directory" hass, f"https://{cloud.acme_server}/directory"

View file

@ -1,6 +1,6 @@
"""Test the cloud.iot module.""" """Test the cloud.iot module."""
from datetime import timedelta from datetime import timedelta
from unittest.mock import AsyncMock, MagicMock, Mock, patch from unittest.mock import AsyncMock, MagicMock, Mock, PropertyMock, patch
import aiohttp import aiohttp
from aiohttp import web from aiohttp import web
@ -357,7 +357,10 @@ async def test_system_msg(hass: HomeAssistant) -> None:
async def test_cloud_connection_info(hass: HomeAssistant) -> None: async def test_cloud_connection_info(hass: HomeAssistant) -> None:
"""Test connection info msg.""" """Test connection info msg."""
with patch("hass_nabucasa.Cloud.initialize"): with patch("hass_nabucasa.Cloud.initialize"), patch(
"uuid.UUID.hex", new_callable=PropertyMock
) as hexmock:
hexmock.return_value = "12345678901234567890"
setup = await async_setup_component(hass, "cloud", {"cloud": {}}) setup = await async_setup_component(hass, "cloud", {"cloud": {}})
assert setup assert setup
cloud = hass.data["cloud"] cloud = hass.data["cloud"]
@ -372,4 +375,5 @@ async def test_cloud_connection_info(hass: HomeAssistant) -> None:
"alias": None, "alias": None,
}, },
"version": HA_VERSION, "version": HA_VERSION,
"instance_id": "12345678901234567890",
} }

View file

@ -46,6 +46,7 @@ async def test_cloud_system_health(
remote_enabled=True, remote_enabled=True,
alexa_enabled=True, alexa_enabled=True,
google_enabled=False, google_enabled=False,
instance_id="12345678901234567890",
), ),
), ),
) )
@ -70,4 +71,5 @@ async def test_cloud_system_health(
"can_reach_cert_server": "ok", "can_reach_cert_server": "ok",
"can_reach_cloud_auth": {"type": "failed", "error": "unreachable"}, "can_reach_cloud_auth": {"type": "failed", "error": "unreachable"},
"can_reach_cloud": "ok", "can_reach_cloud": "ok",
"instance_id": "12345678901234567890",
} }