From 52b5d2e37005e3ff1ee7cdcd296a2c23576b66df Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Fri, 19 Jan 2024 19:22:17 -1000 Subject: [PATCH] Avoid json encoder default fallback when serializing config (#108360) Co-authored-by: Paulus Schoutsen --- homeassistant/core.py | 9 +++--- tests/components/api/test_init.py | 22 ++++++++------ tests/components/mobile_app/test_webhook.py | 2 +- .../components/websocket_api/test_commands.py | 29 +++++++++---------- tests/test_core.py | 8 ++--- 5 files changed, 37 insertions(+), 33 deletions(-) diff --git a/homeassistant/core.py b/homeassistant/core.py index bef89118de9..9c7689f483b 100644 --- a/homeassistant/core.py +++ b/homeassistant/core.py @@ -2416,6 +2416,7 @@ class Config: Async friendly. """ + allowlist_external_dirs = list(self.allowlist_external_dirs) return { "latitude": self.latitude, "longitude": self.longitude, @@ -2423,12 +2424,12 @@ class Config: "unit_system": self.units.as_dict(), "location_name": self.location_name, "time_zone": self.time_zone, - "components": self.components, + "components": list(self.components), "config_dir": self.config_dir, # legacy, backwards compat - "whitelist_external_dirs": self.allowlist_external_dirs, - "allowlist_external_dirs": self.allowlist_external_dirs, - "allowlist_external_urls": self.allowlist_external_urls, + "whitelist_external_dirs": allowlist_external_dirs, + "allowlist_external_dirs": allowlist_external_dirs, + "allowlist_external_urls": list(self.allowlist_external_urls), "version": __version__, "config_source": self.config_source, "recovery_mode": self.recovery_mode, diff --git a/tests/components/api/test_init.py b/tests/components/api/test_init.py index d9c8e7481fa..0d6f2498c79 100644 --- a/tests/components/api/test_init.py +++ b/tests/components/api/test_init.py @@ -254,16 +254,20 @@ async def test_api_get_config(hass: HomeAssistant, mock_api_client: TestClient) """Test the return of the configuration.""" resp = await mock_api_client.get(const.URL_API_CONFIG) result = await resp.json() - if "components" in result: - result["components"] = set(result["components"]) - if "whitelist_external_dirs" in result: - result["whitelist_external_dirs"] = set(result["whitelist_external_dirs"]) - if "allowlist_external_dirs" in result: - result["allowlist_external_dirs"] = set(result["allowlist_external_dirs"]) - if "allowlist_external_urls" in result: - result["allowlist_external_urls"] = set(result["allowlist_external_urls"]) + ignore_order_keys = ( + "components", + "allowlist_external_dirs", + "whitelist_external_dirs", + "allowlist_external_urls", + ) + config = hass.config.as_dict() - assert hass.config.as_dict() == result + for key in ignore_order_keys: + if key in result: + result[key] = set(result[key]) + config[key] = set(config[key]) + + assert result == config async def test_api_get_components( diff --git a/tests/components/mobile_app/test_webhook.py b/tests/components/mobile_app/test_webhook.py index c5e5801cda8..f7581f03241 100644 --- a/tests/components/mobile_app/test_webhook.py +++ b/tests/components/mobile_app/test_webhook.py @@ -318,7 +318,7 @@ async def test_webhook_handle_get_config( "unit_system": hass_config["unit_system"], "location_name": hass_config["location_name"], "time_zone": hass_config["time_zone"], - "components": hass_config["components"], + "components": set(hass_config["components"]), "version": hass_config["version"], "theme_color": "#03A9F4", # Default frontend theme color "entities": { diff --git a/tests/components/websocket_api/test_commands.py b/tests/components/websocket_api/test_commands.py index 270ad9bf178..68e2e14a08c 100644 --- a/tests/components/websocket_api/test_commands.py +++ b/tests/components/websocket_api/test_commands.py @@ -716,22 +716,21 @@ async def test_get_config( assert msg["type"] == const.TYPE_RESULT assert msg["success"] - if "components" in msg["result"]: - msg["result"]["components"] = set(msg["result"]["components"]) - if "whitelist_external_dirs" in msg["result"]: - msg["result"]["whitelist_external_dirs"] = set( - msg["result"]["whitelist_external_dirs"] - ) - if "allowlist_external_dirs" in msg["result"]: - msg["result"]["allowlist_external_dirs"] = set( - msg["result"]["allowlist_external_dirs"] - ) - if "allowlist_external_urls" in msg["result"]: - msg["result"]["allowlist_external_urls"] = set( - msg["result"]["allowlist_external_urls"] - ) + result = msg["result"] + ignore_order_keys = ( + "components", + "allowlist_external_dirs", + "whitelist_external_dirs", + "allowlist_external_urls", + ) + config = hass.config.as_dict() - assert msg["result"] == hass.config.as_dict() + for key in ignore_order_keys: + if key in result: + result[key] = set(result[key]) + config[key] = set(config[key]) + + assert result == config async def test_ping(websocket_client: MockHAClientWebSocket) -> None: diff --git a/tests/test_core.py b/tests/test_core.py index e6a1362a30e..01eb4c517b1 100644 --- a/tests/test_core.py +++ b/tests/test_core.py @@ -1622,11 +1622,11 @@ async def test_config_as_dict() -> None: CONF_UNIT_SYSTEM: METRIC_SYSTEM.as_dict(), "location_name": "Home", "time_zone": "UTC", - "components": set(), + "components": [], "config_dir": "/test/ha-config", - "whitelist_external_dirs": set(), - "allowlist_external_dirs": set(), - "allowlist_external_urls": set(), + "whitelist_external_dirs": [], + "allowlist_external_dirs": [], + "allowlist_external_urls": [], "version": __version__, "config_source": ha.ConfigSource.DEFAULT, "recovery_mode": False,