Use None as default value for strict connection cloud store (#116219)

pull/116230/head
Robert Resch 2024-04-26 11:22:04 +02:00 committed by GitHub
parent 49d8ac0811
commit 67f6a84f5d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 27 additions and 6 deletions

View File

@ -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,
}

View File

@ -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