From 00c4938d8bed7ad6cf9cbc3d743057c66280f728 Mon Sep 17 00:00:00 2001 From: Mike Degatano Date: Thu, 26 Sep 2024 10:35:16 -0400 Subject: [PATCH] Add by alias flag to addon to dict (#13) --- aiohasupervisor/models/addons.py | 6 ++++++ tests/test_addons.py | 20 ++++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/aiohasupervisor/models/addons.py b/aiohasupervisor/models/addons.py index 1ed7950..12d02be 100644 --- a/aiohasupervisor/models/addons.py +++ b/aiohasupervisor/models/addons.py @@ -6,6 +6,7 @@ from typing import Any from mashumaro import field_options +from mashumaro.config import TO_DICT_ADD_BY_ALIAS_FLAG, BaseConfig from .base import DEFAULT, Options, Request, RequestConfig, ResponseData @@ -151,6 +152,11 @@ class AddonInfoStoreExtFields(ABC): metadata=field_options(alias="hassio_role"), ) + class Config(BaseConfig): + """Mashumaro config options.""" + + code_generation_options = [TO_DICT_ADD_BY_ALIAS_FLAG] # noqa: RUF012 + @dataclass(frozen=True) class AddonInfoStoreExtInstalledBaseFields(ABC): diff --git a/tests/test_addons.py b/tests/test_addons.py index 04a730c..bf026e3 100644 --- a/tests/test_addons.py +++ b/tests/test_addons.py @@ -12,8 +12,11 @@ AddonState, AddonsUninstall, Capability, + InstalledAddonComplete, + StoreAddonComplete, SupervisorRole, ) +from aiohasupervisor.models.base import Response from . import load_fixture from .const import SUPERVISOR_URL @@ -234,3 +237,20 @@ async def test_addons_stats( assert stats.cpu_percent == 0 assert stats.memory_usage == 24588288 assert stats.network_rx == 1717120021 + + +async def test_addons_serialize_by_alias() -> None: + """Test serializing addons by alias.""" + response = Response.from_json(load_fixture("store_addon_info.json")) + store_addon_info = StoreAddonComplete.from_dict(response.data) + + assert store_addon_info.supervisor_api is False + assert (store_addon_info.to_dict())["supervisor_api"] is False + assert (store_addon_info.to_dict(by_alias=True))["hassio_api"] is False + + response = Response.from_json(load_fixture("addons_info.json")) + addon_info = InstalledAddonComplete.from_dict(response.data) + + assert addon_info.supervisor_api is True + assert (addon_info.to_dict())["supervisor_api"] is True + assert (addon_info.to_dict(by_alias=True))["hassio_api"] is True