Skip to content

Commit 00c4938

Browse files
authored
Add by alias flag to addon to dict (#13)
1 parent 2d0598c commit 00c4938

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

aiohasupervisor/models/addons.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from typing import Any
77

88
from mashumaro import field_options
9+
from mashumaro.config import TO_DICT_ADD_BY_ALIAS_FLAG, BaseConfig
910

1011
from .base import DEFAULT, Options, Request, RequestConfig, ResponseData
1112

@@ -151,6 +152,11 @@ class AddonInfoStoreExtFields(ABC):
151152
metadata=field_options(alias="hassio_role"),
152153
)
153154

155+
class Config(BaseConfig):
156+
"""Mashumaro config options."""
157+
158+
code_generation_options = [TO_DICT_ADD_BY_ALIAS_FLAG] # noqa: RUF012
159+
154160

155161
@dataclass(frozen=True)
156162
class AddonInfoStoreExtInstalledBaseFields(ABC):

tests/test_addons.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,11 @@
1212
AddonState,
1313
AddonsUninstall,
1414
Capability,
15+
InstalledAddonComplete,
16+
StoreAddonComplete,
1517
SupervisorRole,
1618
)
19+
from aiohasupervisor.models.base import Response
1720

1821
from . import load_fixture
1922
from .const import SUPERVISOR_URL
@@ -234,3 +237,20 @@ async def test_addons_stats(
234237
assert stats.cpu_percent == 0
235238
assert stats.memory_usage == 24588288
236239
assert stats.network_rx == 1717120021
240+
241+
242+
async def test_addons_serialize_by_alias() -> None:
243+
"""Test serializing addons by alias."""
244+
response = Response.from_json(load_fixture("store_addon_info.json"))
245+
store_addon_info = StoreAddonComplete.from_dict(response.data)
246+
247+
assert store_addon_info.supervisor_api is False
248+
assert (store_addon_info.to_dict())["supervisor_api"] is False
249+
assert (store_addon_info.to_dict(by_alias=True))["hassio_api"] is False
250+
251+
response = Response.from_json(load_fixture("addons_info.json"))
252+
addon_info = InstalledAddonComplete.from_dict(response.data)
253+
254+
assert addon_info.supervisor_api is True
255+
assert (addon_info.to_dict())["supervisor_api"] is True
256+
assert (addon_info.to_dict(by_alias=True))["hassio_api"] is True

0 commit comments

Comments
 (0)