Avoid json encoder default fallback when serializing config (#108360)

Co-authored-by: Paulus Schoutsen <balloob@gmail.com>
This commit is contained in:
J. Nick Koston 2024-01-19 19:22:17 -10:00 committed by GitHub
parent 3184d3b168
commit 52b5d2e370
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 37 additions and 33 deletions

View file

@ -2416,6 +2416,7 @@ class Config:
Async friendly. Async friendly.
""" """
allowlist_external_dirs = list(self.allowlist_external_dirs)
return { return {
"latitude": self.latitude, "latitude": self.latitude,
"longitude": self.longitude, "longitude": self.longitude,
@ -2423,12 +2424,12 @@ class Config:
"unit_system": self.units.as_dict(), "unit_system": self.units.as_dict(),
"location_name": self.location_name, "location_name": self.location_name,
"time_zone": self.time_zone, "time_zone": self.time_zone,
"components": self.components, "components": list(self.components),
"config_dir": self.config_dir, "config_dir": self.config_dir,
# legacy, backwards compat # legacy, backwards compat
"whitelist_external_dirs": self.allowlist_external_dirs, "whitelist_external_dirs": allowlist_external_dirs,
"allowlist_external_dirs": self.allowlist_external_dirs, "allowlist_external_dirs": allowlist_external_dirs,
"allowlist_external_urls": self.allowlist_external_urls, "allowlist_external_urls": list(self.allowlist_external_urls),
"version": __version__, "version": __version__,
"config_source": self.config_source, "config_source": self.config_source,
"recovery_mode": self.recovery_mode, "recovery_mode": self.recovery_mode,

View file

@ -254,16 +254,20 @@ async def test_api_get_config(hass: HomeAssistant, mock_api_client: TestClient)
"""Test the return of the configuration.""" """Test the return of the configuration."""
resp = await mock_api_client.get(const.URL_API_CONFIG) resp = await mock_api_client.get(const.URL_API_CONFIG)
result = await resp.json() result = await resp.json()
if "components" in result: ignore_order_keys = (
result["components"] = set(result["components"]) "components",
if "whitelist_external_dirs" in result: "allowlist_external_dirs",
result["whitelist_external_dirs"] = set(result["whitelist_external_dirs"]) "whitelist_external_dirs",
if "allowlist_external_dirs" in result: "allowlist_external_urls",
result["allowlist_external_dirs"] = set(result["allowlist_external_dirs"]) )
if "allowlist_external_urls" in result: config = hass.config.as_dict()
result["allowlist_external_urls"] = set(result["allowlist_external_urls"])
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( async def test_api_get_components(

View file

@ -318,7 +318,7 @@ async def test_webhook_handle_get_config(
"unit_system": hass_config["unit_system"], "unit_system": hass_config["unit_system"],
"location_name": hass_config["location_name"], "location_name": hass_config["location_name"],
"time_zone": hass_config["time_zone"], "time_zone": hass_config["time_zone"],
"components": hass_config["components"], "components": set(hass_config["components"]),
"version": hass_config["version"], "version": hass_config["version"],
"theme_color": "#03A9F4", # Default frontend theme color "theme_color": "#03A9F4", # Default frontend theme color
"entities": { "entities": {

View file

@ -716,22 +716,21 @@ async def test_get_config(
assert msg["type"] == const.TYPE_RESULT assert msg["type"] == const.TYPE_RESULT
assert msg["success"] assert msg["success"]
if "components" in msg["result"]: result = msg["result"]
msg["result"]["components"] = set(msg["result"]["components"]) ignore_order_keys = (
if "whitelist_external_dirs" in msg["result"]: "components",
msg["result"]["whitelist_external_dirs"] = set( "allowlist_external_dirs",
msg["result"]["whitelist_external_dirs"] "whitelist_external_dirs",
) "allowlist_external_urls",
if "allowlist_external_dirs" in msg["result"]: )
msg["result"]["allowlist_external_dirs"] = set( config = hass.config.as_dict()
msg["result"]["allowlist_external_dirs"]
)
if "allowlist_external_urls" in msg["result"]:
msg["result"]["allowlist_external_urls"] = set(
msg["result"]["allowlist_external_urls"]
)
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: async def test_ping(websocket_client: MockHAClientWebSocket) -> None:

View file

@ -1622,11 +1622,11 @@ async def test_config_as_dict() -> None:
CONF_UNIT_SYSTEM: METRIC_SYSTEM.as_dict(), CONF_UNIT_SYSTEM: METRIC_SYSTEM.as_dict(),
"location_name": "Home", "location_name": "Home",
"time_zone": "UTC", "time_zone": "UTC",
"components": set(), "components": [],
"config_dir": "/test/ha-config", "config_dir": "/test/ha-config",
"whitelist_external_dirs": set(), "whitelist_external_dirs": [],
"allowlist_external_dirs": set(), "allowlist_external_dirs": [],
"allowlist_external_urls": set(), "allowlist_external_urls": [],
"version": __version__, "version": __version__,
"config_source": ha.ConfigSource.DEFAULT, "config_source": ha.ConfigSource.DEFAULT,
"recovery_mode": False, "recovery_mode": False,