Skip to content

Commit

Permalink
Add pylint and black
Browse files Browse the repository at this point in the history
  • Loading branch information
LouisChrist committed Mar 9, 2024
1 parent c94a044 commit 64fe694
Show file tree
Hide file tree
Showing 5 changed files with 256 additions and 27 deletions.
66 changes: 42 additions & 24 deletions blueos/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@

from blueos.entities import Status, Volume

StringDict: TypeAlias = dict[str, Union[str, 'StringDict']]
T: TypeAlias = TypeVar('T')
StringDict: TypeAlias = dict[str, Union[str, "StringDict"]]
T: TypeAlias = TypeVar("T")


def chained_get(data: StringDict, *keys, _map: Callable[[str], T] = lambda x: x) -> T | None:
def chained_get(
data: StringDict, *keys, _map: Callable[[str], T] = lambda x: x
) -> T | None:
local_data = data
for key in keys:
local_data = local_data.get(key)
Expand All @@ -19,7 +21,9 @@ def chained_get(data: StringDict, *keys, _map: Callable[[str], T] = lambda x: x)


class BlueOS:
def __init__(self, host: str, port: int = 11000, session: aiohttp.ClientSession = None):
def __init__(
self, host: str, port: int = 11000, session: aiohttp.ClientSession = None
):
self.base_url = f"http://{host}:{port}"
if session:
self._owned_session = False
Expand Down Expand Up @@ -51,22 +55,27 @@ async def status(self, etag: str = None, timeout: int = 30) -> Status:
if etag:
params["etag"] = etag
params["timeout"] = timeout
async with self._session.get(f"{self.base_url}/Status", params=params) as response:
async with self._session.get(
f"{self.base_url}/Status", params=params
) as response:
response.raise_for_status()
response_data = await response.text()
response_dict = xmltodict.parse(response_data)

status = Status(etag=chained_get(response_dict, "status", "@etag"),
state=chained_get(response_dict, "status", "state"),
album=chained_get(response_dict, "status", "album"),
artist=chained_get(response_dict, "status", "artist"),
name=chained_get(response_dict, "status", "title1"),
image=chained_get(response_dict, "status", "image"),
volume=chained_get(response_dict, "status", "volume", _map=int),
mute=chained_get(response_dict, "status", "mute") == "1",
seconds=chained_get(response_dict, "status", "secs", _map=int),
total_seconds=chained_get(response_dict, "status", "totlen", _map=float)
)
status = Status(
etag=chained_get(response_dict, "status", "@etag"),
state=chained_get(response_dict, "status", "state"),
album=chained_get(response_dict, "status", "album"),
artist=chained_get(response_dict, "status", "artist"),
name=chained_get(response_dict, "status", "title1"),
image=chained_get(response_dict, "status", "image"),
volume=chained_get(response_dict, "status", "volume", _map=int),
mute=chained_get(response_dict, "status", "mute") == "1",
seconds=chained_get(response_dict, "status", "secs", _map=int),
total_seconds=chained_get(
response_dict, "status", "totlen", _map=float
),
)

return status

Expand All @@ -79,7 +88,9 @@ async def mac(self) -> str:

return chained_get(response_dict, "SyncStatus", "@mac")

async def volume(self, level: int = None, mute: bool = None, tell_slaves: bool = None) -> Volume:
async def volume(
self, level: int = None, mute: bool = None, tell_slaves: bool = None
) -> Volume:
"""Get or set the volume of the device. Uses the /Volume endpoint."""
params = {}
if level:
Expand All @@ -89,15 +100,18 @@ async def volume(self, level: int = None, mute: bool = None, tell_slaves: bool =
if tell_slaves:
params["tell_slaves"] = "1" if tell_slaves else "0"

async with self._session.get(f"{self.base_url}/Volume", params=params) as response:
async with self._session.get(
f"{self.base_url}/Volume", params=params
) as response:
response.raise_for_status()
response_data = await response.text()
response_dict = xmltodict.parse(response_data)

volume = Volume(volume=chained_get(response_dict, "volume", "#text", _map=int),
db=chained_get(response_dict, "volume", "@db", _map=float),
mute=chained_get(response_dict, "volume", "@mute") == "1"
)
volume = Volume(
volume=chained_get(response_dict, "volume", "#text", _map=int),
db=chained_get(response_dict, "volume", "@db", _map=float),
mute=chained_get(response_dict, "volume", "@mute") == "1",
)

return volume

Expand All @@ -106,7 +120,9 @@ async def play(self, seek: int = None) -> str:
if seek:
params["seek"] = seek

async with self._session.get(f"{self.base_url}/Play", params=params) as response:
async with self._session.get(
f"{self.base_url}/Play", params=params
) as response:
response.raise_for_status()
response_data = await response.text()
response_dict = xmltodict.parse(response_data)
Expand All @@ -118,7 +134,9 @@ async def pause(self, toggle: bool = None) -> str:
if toggle:
params["toggle"] = "1"

async with self._session.get(f"{self.base_url}/Pause", params=params) as response:
async with self._session.get(
f"{self.base_url}/Pause", params=params
) as response:
response.raise_for_status()
response_data = await response.text()
response_dict = xmltodict.parse(response_data)
Expand Down
3 changes: 1 addition & 2 deletions blueos/cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@ async def run():
print(mac)




def main():
asyncio.run(run())


if __name__ == "__main__":
main()
2 changes: 2 additions & 0 deletions format-and-lint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
poetry run black blueos
poetry run pylint blueos
208 changes: 207 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ pydantic = "^2.6.3"
blueos = "blueos.cmd:main"


[tool.poetry.group.dev.dependencies]
pylint = "^3.1.0"
black = "^24.2.0"

[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"

0 comments on commit 64fe694

Please sign in to comment.