Skip to content

Commit

Permalink
Merge pull request #48 from noahhusby/feat/property-safety
Browse files Browse the repository at this point in the history
Implement proper type checking
  • Loading branch information
noahhusby authored Oct 25, 2024
2 parents 9c305b9 + a396a0c commit 698fbc0
Show file tree
Hide file tree
Showing 7 changed files with 270 additions and 137 deletions.
8 changes: 4 additions & 4 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ repos:
entry: poetry run ruff format
require_serial: true
stages: [commit, push, manual]
- id: pytest
name: 🧪 Running tests and test coverage with pytest
- id: mypy
name: Static type checking using mypy
language: system
types: [python]
entry: poetry run pytest
pass_filenames: false
entry: poetry run mypy
require_serial: true
227 changes: 130 additions & 97 deletions aiostreammagic/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,65 @@

from dataclasses import dataclass, field
from enum import StrEnum
from typing import Optional

from mashumaro import field_options
from mashumaro.mixins.orjson import DataClassORJSONMixin


class TransportControl(StrEnum):
"""Control enum."""

PAUSE = "pause"
PLAY = "play"
PLAY_PAUSE = "play_pause"
TOGGLE_SHUFFLE = "toggle_shuffle"
TOGGLE_REPEAT = "toggle_repeat"
TRACK_NEXT = "track_next"
TRACK_PREVIOUS = "track_previous"
SEEK = "seek"
STOP = "stop"


class ShuffleMode(StrEnum):
"""Shuffle mode."""

OFF = "off"
ALL = "all"
TOGGLE = "toggle"


class RepeatMode(StrEnum):
"""Repeat mode."""

OFF = "off"
ALL = "all"
TOGGLE = "toggle"


class CallbackType(StrEnum):
"""Callback type."""

STATE = "state"
CONNECTION = "connection"


class DisplayBrightness(StrEnum):
"""Display brightness."""

BRIGHT = "bright"
DIM = "dim"
OFF = "off"


class ControlBusMode(StrEnum):
"""Control bus mode."""

AMPLIFIER = "amplifier"
RECEIVER = "receiver"
OFF = "off"


@dataclass
class Info(DataClassORJSONMixin):
"""Cambridge Audio device metadata."""
Expand All @@ -33,7 +87,9 @@ class Source(DataClassORJSONMixin):
ui_selectable: bool = field(metadata=field_options(alias="ui_selectable"))
description: str = field(metadata=field_options(alias="description"))
description_locale: str = field(metadata=field_options(alias="description_locale"))
preferred_order: int = field(metadata=field_options(alias="preferred_order"), default=None)
preferred_order: Optional[int] = field(
metadata=field_options(alias="preferred_order"), default=None
)


@dataclass
Expand All @@ -44,64 +100,88 @@ class State(DataClassORJSONMixin):
power: bool = field(metadata=field_options(alias="power"))
pre_amp_mode: bool = field(metadata=field_options(alias="pre_amp_mode"))
pre_amp_state: bool = field(metadata=field_options(alias="pre_amp_state"))
volume_step: int = field(metadata=field_options(alias="volume_step"), default=None)
volume_db: int = field(metadata=field_options(alias="volume_db"), default=None)
volume_percent: int = field(
volume_step: Optional[int] = field(
metadata=field_options(alias="volume_step"), default=None
)
volume_db: Optional[int] = field(
metadata=field_options(alias="volume_db"), default=None
)
volume_percent: Optional[int] = field(
metadata=field_options(alias="volume_percent"), default=None
)
mute: bool = field(metadata=field_options(alias="mute"), default=False)
audio_output: str = field(
audio_output: Optional[str] = field(
metadata=field_options(alias="audio_output"), default=None
)
control_bus: ControlBusMode = field(
metadata=field_options(alias="cbus"), default="off"
metadata=field_options(alias="cbus"), default=ControlBusMode.OFF
)


@dataclass
class PlayStateMetadata(DataClassORJSONMixin):
"""Data class representing StreamMagic play state metadata."""

class_name: Optional[str] = field(
metadata=field_options(alias="class"), default=None
)
source: Optional[str] = field(metadata=field_options(alias="source"), default=None)
name: Optional[str] = field(metadata=field_options(alias="name"), default=None)
title: Optional[str] = field(metadata=field_options(alias="title"), default=None)
art_url: Optional[str] = field(
metadata=field_options(alias="art_url"), default=None
)
sample_format: Optional[str] = field(
metadata=field_options(alias="sample_format"), default=None
)
mqa: Optional[str] = field(metadata=field_options(alias="mqa"), default=None)
signal: Optional[bool] = field(metadata=field_options(alias="signal"), default=None)
codec: Optional[str] = field(metadata=field_options(alias="codec"), default=None)
lossless: Optional[bool] = field(
metadata=field_options(alias="lossless"), default=None
)
sample_rate: Optional[int] = field(
metadata=field_options(alias="sample_rate"), default=None
)
bitrate: Optional[int] = field(
metadata=field_options(alias="bitrate"), default=None
)
encoding: Optional[str] = field(
metadata=field_options(alias="encoding"), default=None
)
radio_id: Optional[int] = field(
metadata=field_options(alias="radio_id"), default=None
)
duration: Optional[int] = field(
metadata=field_options(alias="duration"), default=None
)
artist: Optional[str] = field(metadata=field_options(alias="artist"), default=None)
station: Optional[str] = field(
metadata=field_options(alias="station"), default=None
)
album: Optional[str] = field(metadata=field_options(alias="album"), default=None)


@dataclass
class PlayState(DataClassORJSONMixin):
"""Data class representing StreamMagic play state."""

state: str = field(metadata=field_options(alias="state"), default="not_ready")
metadata: PlayStateMetadata = field(
metadata=field_options(alias="metadata"), default=None
metadata=field_options(alias="metadata"), default_factory=PlayStateMetadata
)
presettable: bool = field(
metadata=field_options(alias="presettable"), default=False
)
position: int = field(metadata=field_options(alias="position"), default=None)
position: Optional[int] = field(
metadata=field_options(alias="position"), default=None
)
mode_repeat: str = field(metadata=field_options(alias="mode_repeat"), default="off")
mode_shuffle: str = field(
metadata=field_options(alias="mode_shuffle"), default="off"
)


@dataclass
class PlayStateMetadata(DataClassORJSONMixin):
"""Data class representing StreamMagic play state metadata."""

class_name: str = field(metadata=field_options(alias="class"), default=None)
source: str = field(metadata=field_options(alias="source"), default=None)
name: str = field(metadata=field_options(alias="name"), default=None)
title: str = field(metadata=field_options(alias="title"), default=None)
art_url: str = field(metadata=field_options(alias="art_url"), default=None)
sample_format: str = field(
metadata=field_options(alias="sample_format"), default=None
)
mqa: str = field(metadata=field_options(alias="mqa"), default=None)
signal: bool = field(metadata=field_options(alias="signal"), default=None)
codec: str = field(metadata=field_options(alias="codec"), default=None)
lossless: bool = field(metadata=field_options(alias="lossless"), default=None)
sample_rate: int = field(metadata=field_options(alias="sample_rate"), default=None)
bitrate: int = field(metadata=field_options(alias="bitrate"), default=None)
encoding: str = field(metadata=field_options(alias="encoding"), default=None)
radio_id: int | None = field(metadata=field_options(alias="radio_id"), default=None)
duration: int | None = field(metadata=field_options(alias="duration"), default=None)
artist: str | None = field(metadata=field_options(alias="artist"), default=None)
station: str | None = field(metadata=field_options(alias="station"), default=None)
album: str | None = field(metadata=field_options(alias="album"), default=None)


@dataclass
class PresetList(DataClassORJSONMixin):
"""Data class representing StreamMagic preset table."""
Expand All @@ -112,21 +192,25 @@ class PresetList(DataClassORJSONMixin):
presettable: bool = field(
metadata=field_options(alias="presettable"), default=False
)
presets: list[Preset] = field(metadata=field_options(alias="presets"), default=None)
presets: list[Preset] = field(
metadata=field_options(alias="presets"), default_factory=list
)


@dataclass
class Preset(DataClassORJSONMixin):
"""Data class representing StreamMagic preset."""

preset_id: int = field(metadata=field_options(alias="id"), default=None)
name: str = field(metadata=field_options(alias="name"), default=None)
type: str = field(metadata=field_options(alias="type"), default=None)
preset_class: str = field(metadata=field_options(alias="class"), default=None)
state: str = field(metadata=field_options(alias="state"), default=None)
preset_id: int = field(metadata=field_options(alias="id"))
name: str = field(metadata=field_options(alias="name"))
type: str = field(metadata=field_options(alias="type"))
preset_class: str = field(metadata=field_options(alias="class"))
state: str = field(metadata=field_options(alias="state"))
is_playing: bool = field(metadata=field_options(alias="is_playing"), default=False)
art_url: str = field(metadata=field_options(alias="art_url"), default=None)
airable_radio_id: int = field(
art_url: Optional[str] = field(
metadata=field_options(alias="art_url"), default=None
)
airable_radio_id: Optional[int] = field(
metadata=field_options(alias="airable_radio_id"), default=None
)

Expand All @@ -136,15 +220,17 @@ class NowPlaying(DataClassORJSONMixin):
"""Data class representing NowPlaying state."""

controls: list[TransportControl] = field(
metadata=field_options(alias="controls"), default=None
metadata=field_options(alias="controls"), default_factory=list
)


@dataclass
class AudioOutput(DataClassORJSONMixin):
"""Data class representing StreamMagic audio output."""

outputs: list[Output] = field(metadata=field_options(alias="outputs"), default=None)
outputs: list[Output] = field(
metadata=field_options(alias="outputs"), default_factory=list
)


@dataclass
Expand All @@ -169,56 +255,3 @@ class Update(DataClassORJSONMixin):
metadata=field_options(alias="update_available"), default=False
)
updating: bool = field(metadata=field_options(alias="updating"), default=False)


class TransportControl(StrEnum):
"""Control enum."""

PAUSE = "pause"
PLAY = "play"
PLAY_PAUSE = "play_pause"
TOGGLE_SHUFFLE = "toggle_shuffle"
TOGGLE_REPEAT = "toggle_repeat"
TRACK_NEXT = "track_next"
TRACK_PREVIOUS = "track_previous"
SEEK = "seek"
STOP = "stop"


class ShuffleMode(StrEnum):
"""Shuffle mode."""

OFF = "off"
ALL = "all"
TOGGLE = "toggle"


class RepeatMode(StrEnum):
"""Repeat mode."""

OFF = "off"
ALL = "all"
TOGGLE = "toggle"


class CallbackType(StrEnum):
"""Callback type."""

STATE = "state"
CONNECTION = "connection"


class DisplayBrightness(StrEnum):
"""Display brightness."""

BRIGHT = "bright"
DIM = "dim"
OFF = "off"


class ControlBusMode(StrEnum):
"""Control bus mode."""

AMPLIFIER = "amplifier"
RECEIVER = "receiver"
OFF = "off"
Loading

0 comments on commit 698fbc0

Please sign in to comment.