Skip to content

Commit

Permalink
Refactor intenrals
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewsayre committed Jan 11, 2025
1 parent e2a722e commit 92ee75b
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 20 deletions.
9 changes: 4 additions & 5 deletions pyheos/group.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,16 @@ class HeosGroup:
is_muted: bool = False
heos: Optional["Heos"] = field(repr=False, hash=False, compare=False, default=None)

@classmethod
def from_data(
cls,
@staticmethod
def _from_data(
data: dict[str, Any],
heos: Optional["Heos"] = None,
) -> "HeosGroup":
"""Create a new instance from the provided data."""
player_id: int | None = None
player_ids: list[int] = []
player_id, player_ids = cls.__get_ids(data[c.ATTR_PLAYERS])
return cls(
player_id, player_ids = HeosGroup.__get_ids(data[c.ATTR_PLAYERS])
return HeosGroup(
name=data[c.ATTR_NAME],
group_id=int(data[c.ATTR_GROUP_ID]),
lead_player_id=player_id,
Expand Down
6 changes: 3 additions & 3 deletions pyheos/heos.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ async def get_system_info(self) -> HeosSystem:
4.2.1 Get Players"""
response = await self._connection.command(PlayerCommands.get_players())
payload = cast(Sequence[dict], response.payload)
hosts = list([HeosHost.from_data(item) for item in payload])
hosts = list([HeosHost._from_data(item) for item in payload])
host = next(host for host in hosts if host.ip_address == self._options.host)
return HeosSystem(self._signed_in_username, host, hosts)

Expand Down Expand Up @@ -1055,7 +1055,7 @@ async def get_groups(self, *, refresh: bool = False) -> dict[int, HeosGroup]:
result = await self._connection.command(GroupCommands.get_groups())
payload = cast(Sequence[dict], result.payload)
for data in payload:
group = HeosGroup.from_data(data, cast("Heos", self))
group = HeosGroup._from_data(data, cast("Heos", self))
groups[group.group_id] = group
self._groups = groups
# Update all statuses
Expand Down Expand Up @@ -1105,7 +1105,7 @@ async def get_group_info(
)
payload = cast(dict[str, Any], result.payload)
if group is None:
group = HeosGroup.from_data(payload, cast("Heos", self))
group = HeosGroup._from_data(payload, cast("Heos", self))
else:
group._update_from_data(payload)
await group.refresh(refresh_base_info=False)
Expand Down
12 changes: 6 additions & 6 deletions pyheos/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,24 +34,24 @@ def uri_masked(self) -> str:
def _get_uri(self, mask: bool = False) -> str:
"""Get the command as a URI string."""
query_string = (
f"?{HeosCommand._encode_query(self.parameters, mask=mask)}"
f"?{HeosCommand.__encode_query(self.parameters, mask=mask)}"
if self.parameters
else ""
)
return f"{BASE_URI}{self.command}{query_string}"

@classmethod
def _quote(cls, value: Any) -> str:
@staticmethod
def __quote(value: Any) -> str:
"""Quote a string per the CLI specification."""
return "".join([QUOTE_MAP.get(char, char) for char in str(value)])

@classmethod
def _encode_query(cls, items: dict[str, Any], *, mask: bool = False) -> str:
@staticmethod
def __encode_query(items: dict[str, Any], *, mask: bool = False) -> str:
"""Encode a dict to query string per CLI specifications."""
pairs = []
for key in sorted(items.keys()):
value = MASK if mask and key in MASKED_PARAMS else items[key]
item = f"{key}={HeosCommand._quote(value)}"
item = f"{key}={HeosCommand.__quote(value)}"
# Ensure 'url' goes last per CLI spec and is not quoted
if key == c.ATTR_URL:
pairs.append(f"{key}={value}")
Expand Down
6 changes: 3 additions & 3 deletions pyheos/player.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ def _update_from_message(self, message: HeosMessage) -> None:
self.source_id = optional_int(data.get(c.ATTR_SOURCE_ID))
self.options = ServiceOption._from_options(message.options)
self._update_supported_controls()
self.clear_progress()
self._clear_progress()

def _update_supported_controls(self) -> None:
"""Updates the supported controls based on the source and type."""
Expand All @@ -145,7 +145,7 @@ def _on_event(self, event: HeosMessage, all_progress_events: bool) -> bool:
return True
return False

def clear_progress(self) -> None:
def _clear_progress(self) -> None:
"""Clear the current position."""
self.current_position = None
self.current_position_updated = None
Expand Down Expand Up @@ -254,7 +254,7 @@ async def _on_event(self, event: HeosMessage, all_progress_events: bool) -> bool
if event.command == const.EVENT_PLAYER_STATE_CHANGED:
self.state = PlayState(event.get_message_value(c.ATTR_STATE))
if self.state == PlayState.PLAY:
self.now_playing_media.clear_progress()
self.now_playing_media._clear_progress()
elif event.command == const.EVENT_PLAYER_NOW_PLAYING_CHANGED:
await self.refresh_now_playing_media()
elif event.command == const.EVENT_PLAYER_VOLUME_CHANGED:
Expand Down
4 changes: 2 additions & 2 deletions pyheos/system.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ class HeosHost:
ip_address: str
network: NetworkType

@classmethod
def from_data(cls, data: dict[str, str]) -> "HeosHost":
@staticmethod
def _from_data(data: dict[str, str]) -> "HeosHost":
"""Create a HeosHost object from a dictionary.
Args:
Expand Down
2 changes: 1 addition & 1 deletion tests/test_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def test_group_from_data_no_leader_raises() -> None:
],
}
with pytest.raises(ValueError, match="No leader found in group data"):
HeosGroup.from_data(data, None)
HeosGroup._from_data(data, None)


@pytest.mark.parametrize(
Expand Down

0 comments on commit 92ee75b

Please sign in to comment.