diff --git a/homeassistant/components/cloud/prefs.py b/homeassistant/components/cloud/prefs.py index 9fce615128b..72207513ca9 100644 --- a/homeassistant/components/cloud/prefs.py +++ b/homeassistant/components/cloud/prefs.py @@ -365,13 +365,16 @@ class CloudPreferences: @property def strict_connection(self) -> http.const.StrictConnectionMode: """Return the strict connection mode.""" - mode = self._prefs.get( - PREF_STRICT_CONNECTION, http.const.StrictConnectionMode.DISABLED - ) + mode = self._prefs.get(PREF_STRICT_CONNECTION) - if not isinstance(mode, http.const.StrictConnectionMode): + if mode is None: + # Set to default value + # We store None in the store as the default value to detect if the user has changed the + # value or not. + mode = http.const.StrictConnectionMode.DISABLED + elif not isinstance(mode, http.const.StrictConnectionMode): mode = http.const.StrictConnectionMode(mode) - return mode # type: ignore[no-any-return] + return mode async def get_cloud_user(self) -> str: """Return ID of Home Assistant Cloud system user.""" @@ -430,5 +433,5 @@ class CloudPreferences: PREF_REMOTE_DOMAIN: None, PREF_REMOTE_ALLOW_REMOTE_ENABLE: True, PREF_USERNAME: username, - PREF_STRICT_CONNECTION: http.const.StrictConnectionMode.DISABLED, + PREF_STRICT_CONNECTION: None, } diff --git a/tests/components/cloud/test_prefs.py b/tests/components/cloud/test_prefs.py index 1ed2e1d524f..57715fe2bdf 100644 --- a/tests/components/cloud/test_prefs.py +++ b/tests/components/cloud/test_prefs.py @@ -197,3 +197,21 @@ async def test_strict_connection_convertion( await hass.async_block_till_done() assert cloud.client.prefs.strict_connection is mode + + +@pytest.mark.parametrize("storage_data", [{}, {PREF_STRICT_CONNECTION: None}]) +async def test_strict_connection_default( + hass: HomeAssistant, + cloud: MagicMock, + hass_storage: dict[str, Any], + storage_data: dict[str, Any], +) -> None: + """Test strict connection default values.""" + hass_storage[STORAGE_KEY] = { + "version": 1, + "data": storage_data, + } + assert await async_setup_component(hass, DOMAIN, {DOMAIN: {}}) + await hass.async_block_till_done() + + assert cloud.client.prefs.strict_connection is StrictConnectionMode.DISABLED