Skip to content

Commit

Permalink
Add by alias flag to addon to dict (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
mdegat01 authored Sep 26, 2024
1 parent 2d0598c commit 00c4938
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
6 changes: 6 additions & 0 deletions aiohasupervisor/models/addons.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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):
Expand Down
20 changes: 20 additions & 0 deletions tests/test_addons.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

0 comments on commit 00c4938

Please sign in to comment.