Skip to content

Commit

Permalink
feat: use ruff, minor code change and fix documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
M0NsTeRRR committed Nov 16, 2023
1 parent e39dcd8 commit 8c15f5e
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 150 deletions.
5 changes: 3 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,12 @@ jobs:
run: |
export PATH="$PATH:$HOME/.local/bin"
poetry install --with=dev
- name: Run pre-commit
- name: Run ruff
run: |
export PATH="$PATH:$HOME/.local/bin"
source `poetry env info --path`/bin/activate
poetry run black .
poetry run ruff format --check .
poetry run ruff check .
- name: Run test
run: |
export PATH="$PATH:$HOME/.local/bin"
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Python async HTTP client for [awtrix-light](https://github.com/Blueforcer/awtrix
[![PyPI](https://img.shields.io/pypi/v/awtrix-light-client.svg)](https://pypi.python.org/pypi/awtrix-light-client)
[![PyPI versions](https://img.shields.io/pypi/pyversions/awtrix-light-client.svg)](https://pypi.python.org/pypi/awtrix-light-client)
[![Python test](https://github.com/M0NsTeRRR/awtrix-light-client/actions/workflows/test.yml/badge.svg)](https://github.com/M0NsTeRRR/awtrix-light-client/actions/workflows/test.yml)
[![Code Style](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/ambv/black)
[![Code Style](https://img.shields.io/badge/code%20style-ruff-000000.svg)](https://github.com/astral-sh/ruff)

# Warning
This client has been tested with awtrix-light v0.90 use with caution as official dev documentation is not fully documented and can cause crash
Expand Down Expand Up @@ -36,7 +36,7 @@ AWTRIX_HTTP_CLIENT_AWTRIX="<AWTRIX CONFIG>"
"base_url": "http://192.168.0.1",
"username": "admin",
"password": "password",
"verify_ssl": false,
"verify_ssl": false
}
```
`verify_ssl` used to verify https config (if accessing behind an HTTPS reverse proxy), can be `true`, `false`, or can point to a local ca bundle PEM encoded to validate local CA
Expand Down
39 changes: 16 additions & 23 deletions awtrix_light_client/http_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,8 @@ async def _make_request(
url: str,
params: Dict[Any, Any] = None,
data: Dict[Any, Any] = None,
json_data: Dict[Any, Any] = None,
):
r = await self._client.request(
method, url, params=params, data=data, json=json_data
)
r = await self._client.request(method, url, params=params, json=data)

if not r.is_success:
raise AwtrixLightHttpClientError(
Expand Down Expand Up @@ -93,33 +90,33 @@ async def set_power(self, power: bool) -> None:
"""
Toggle the matrix on or off
"""
await self._make_request("POST", "power", json_data={"power": power})
await self._make_request("POST", "power", data={"power": power})

async def set_sleep(self, seconds: int) -> None:
"""
Send the board in deep sleep mode (turns off the matrix as well), good for saving battery life
"""
await self._make_request("POST", "sleep", json_data={"sleep": seconds})
await self._make_request("POST", "sleep", data={"sleep": seconds})

async def set_sound(self, sound: str) -> None:
"""
Play a RTTTL sound from the MELODIES folder
"""
await self._make_request("POST", "sound", json_data={"sound": sound})
await self._make_request("POST", "sound", data={"sound": sound})

async def set_rtttl(self, rtttl: str) -> None:
"""
Play a RTTTL sound from a given RTTTL string
"""
await self._make_request("POST", "rtttl", json_data={"rtttl": rtttl})
await self._make_request("POST", "rtttl", data={"rtttl": rtttl})

async def set_moodlight(self, moodlight: Moodlight) -> None:
"""
Set the entire matrix to a custom color or temperature
To disable moodlight pass `Moodlight()`
"""
await self._make_request(
"POST", "moodlight", json_data=moodlight.model_dump(exclude_none=True)
"POST", "moodlight", data=moodlight.model_dump(exclude_none=True)
)

async def set_indicator(
Expand All @@ -141,18 +138,18 @@ async def set_indicator(
if blink and fade:
raise ValueError("fade and blink can't be set together")

json_data = {"color": as_hex(color, format="long").upper()}
data = {"color": as_hex(color, format="long").upper()}

if blink:
json_data["blink"] = blink
data["blink"] = blink

if fade:
json_data["fade"] = fade
data["fade"] = fade

await self._make_request(
"POST",
f"indicator{indicator}",
json_data=json_data,
data=data,
)

async def set_custom_application(
Expand All @@ -167,22 +164,18 @@ async def set_custom_application(
To eradicate a single app, direct the command to, for instance, test1
"""
if isinstance(custom_application, CustomApplication):
json_data = custom_application.model_dump(exclude_none=True)
data = custom_application.model_dump(exclude_none=True)
else:
json_data = [
app.model_dump(exclude_none=True) for app in custom_application
]
data = [app.model_dump(exclude_none=True) for app in custom_application]

await self._make_request(
"POST", "custom", params={"name": name}, json_data=json_data
)
await self._make_request("POST", "custom", params={"name": name}, data=data)

async def notify(self, notification: Notification) -> None:
"""
One-Time Notification
"""
await self._make_request(
"POST", "notify", json_data=notification.model_dump(exclude_none=True)
"POST", "notify", data=notification.model_dump(exclude_none=True)
)

async def dismiss_notification(self) -> None:
Expand All @@ -207,7 +200,7 @@ async def switch_app(self, name: str) -> None:
"""
Directly transition to a desired app using its name
"""
await self._make_request("POST", "switch", json_data={"name": name})
await self._make_request("POST", "switch", data={"name": name})

async def get_settings(self) -> Settings:
"""
Expand All @@ -220,7 +213,7 @@ async def set_settings(self, s: Settings) -> None:
You can initiate the firmware update either through the update button in HA or using the following
"""
await self._make_request(
"POST", "settings", json_data=s.model_dump(exclude_none=True)
"POST", "settings", data=s.model_dump(exclude_none=True)
)

async def update(self) -> None:
Expand Down
131 changes: 32 additions & 99 deletions poetry.lock

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

8 changes: 4 additions & 4 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,16 @@ include = [
[tool.poetry.dependencies]
python = ">=3.9"
httpx = "~0.25.0"
pydantic = ">=2.4.2,<2.6.0"
pydantic-settings = ">=2.0.3,<2.2.0"
pydantic = "~2.5.1"
pydantic-settings = "~2.1.0"
pydantic-extra-types = "~2.1.0"

[tool.poetry.group.dev.dependencies]
black = ">=23.10.1,<23.12.0"
pytest = "~7.4.3"
pytest-cov = "~4.1.0"
pytest-asyncio = "~0.21.1"
pytest-httpx = ">=0.26,<0.28"
pytest-httpx = "~0.27.0"
ruff = "~0.1.5"

[tool.pytest.ini_options]
asyncio_mode = "auto"
Expand Down
Loading

0 comments on commit 8c15f5e

Please sign in to comment.