pyatv-http 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- pyatv_http-0.1.0/.github/workflows/hk.yml +21 -0
- pyatv_http-0.1.0/.github/workflows/release.yml +66 -0
- pyatv_http-0.1.0/.gitignore +42 -0
- pyatv_http-0.1.0/.yamllint +8 -0
- pyatv_http-0.1.0/LICENSE +21 -0
- pyatv_http-0.1.0/PKG-INFO +201 -0
- pyatv_http-0.1.0/README.md +177 -0
- pyatv_http-0.1.0/biome.json +7 -0
- pyatv_http-0.1.0/hk.pkl +48 -0
- pyatv_http-0.1.0/mise.toml +13 -0
- pyatv_http-0.1.0/pyproject.toml +65 -0
- pyatv_http-0.1.0/src/pyatv_http/__init__.py +0 -0
- pyatv_http-0.1.0/src/pyatv_http/app.py +74 -0
- pyatv_http-0.1.0/src/pyatv_http/atv.py +111 -0
- pyatv_http-0.1.0/src/pyatv_http/auth.py +26 -0
- pyatv_http-0.1.0/src/pyatv_http/cli.py +94 -0
- pyatv_http-0.1.0/src/pyatv_http/config.py +114 -0
- pyatv_http-0.1.0/src/pyatv_http/gen_config.py +106 -0
- pyatv_http-0.1.0/tests/test_app.py +175 -0
- pyatv_http-0.1.0/tests/test_atv.py +177 -0
- pyatv_http-0.1.0/tests/test_cli.py +113 -0
- pyatv_http-0.1.0/tests/test_config.py +192 -0
- pyatv_http-0.1.0/tests/test_gen_config.py +154 -0
- pyatv_http-0.1.0/uv.lock +1319 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
name: hk
|
|
2
|
+
|
|
3
|
+
permissions:
|
|
4
|
+
contents: read
|
|
5
|
+
packages: read
|
|
6
|
+
statuses: write
|
|
7
|
+
|
|
8
|
+
on:
|
|
9
|
+
push:
|
|
10
|
+
branches: [main]
|
|
11
|
+
pull_request:
|
|
12
|
+
|
|
13
|
+
jobs:
|
|
14
|
+
check:
|
|
15
|
+
runs-on: ubuntu-latest
|
|
16
|
+
timeout-minutes: 10
|
|
17
|
+
steps:
|
|
18
|
+
- uses: hugoh/gh-workflows/setup@fe2707057b8da8fab2460fc1f0e816561fa8a958 # v1.3.0
|
|
19
|
+
- run: uv sync
|
|
20
|
+
- run: uv run pytest
|
|
21
|
+
- uses: hugoh/gh-workflows/hk-check@fe2707057b8da8fab2460fc1f0e816561fa8a958 # v1.3.0
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
name: release
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
branches:
|
|
6
|
+
- main
|
|
7
|
+
|
|
8
|
+
concurrency:
|
|
9
|
+
group: release
|
|
10
|
+
cancel-in-progress: false
|
|
11
|
+
|
|
12
|
+
jobs:
|
|
13
|
+
release:
|
|
14
|
+
timeout-minutes: 5
|
|
15
|
+
runs-on: ubuntu-latest
|
|
16
|
+
permissions:
|
|
17
|
+
contents: write
|
|
18
|
+
outputs:
|
|
19
|
+
new_tag: ${{ steps.tag_version.outputs.new_tag }}
|
|
20
|
+
steps:
|
|
21
|
+
- uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0
|
|
22
|
+
with:
|
|
23
|
+
persist-credentials: false
|
|
24
|
+
fetch-depth: 0
|
|
25
|
+
|
|
26
|
+
- name: Bump version and push tag
|
|
27
|
+
id: tag_version
|
|
28
|
+
uses: mathieudutour/github-tag-action@a22cf08638b34d5badda920f9daf6e72c477b07b # v6.2
|
|
29
|
+
with:
|
|
30
|
+
github_token: ${{ secrets.GITHUB_TOKEN }}
|
|
31
|
+
default_bump: false
|
|
32
|
+
|
|
33
|
+
- name: Create a GitHub release
|
|
34
|
+
if: steps.tag_version.outputs.new_tag != ''
|
|
35
|
+
env:
|
|
36
|
+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
|
37
|
+
NEW_TAG: ${{ steps.tag_version.outputs.new_tag }}
|
|
38
|
+
CHANGELOG: ${{ steps.tag_version.outputs.changelog }}
|
|
39
|
+
run: |
|
|
40
|
+
gh release create "$NEW_TAG" \
|
|
41
|
+
--verify-tag \
|
|
42
|
+
--title "Release $NEW_TAG" \
|
|
43
|
+
--notes "$CHANGELOG"
|
|
44
|
+
|
|
45
|
+
publish-pypi:
|
|
46
|
+
needs: release
|
|
47
|
+
if: needs.release.outputs.new_tag != ''
|
|
48
|
+
timeout-minutes: 5
|
|
49
|
+
runs-on: ubuntu-latest
|
|
50
|
+
environment: pypi
|
|
51
|
+
permissions:
|
|
52
|
+
id-token: write
|
|
53
|
+
steps:
|
|
54
|
+
- uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0
|
|
55
|
+
with:
|
|
56
|
+
persist-credentials: false
|
|
57
|
+
fetch-depth: 0
|
|
58
|
+
ref: ${{ needs.release.outputs.new_tag }}
|
|
59
|
+
|
|
60
|
+
- uses: hugoh/gh-workflows/setup@fe2707057b8da8fab2460fc1f0e816561fa8a958 # v1.3.0
|
|
61
|
+
|
|
62
|
+
- name: Build package
|
|
63
|
+
run: uv build
|
|
64
|
+
|
|
65
|
+
- name: Publish to PyPI
|
|
66
|
+
uses: pypa/gh-action-pypi-publish@cef221092ed1bacb1cc03d23a2d87d1d172e277b # v1.14.0
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
# Python
|
|
2
|
+
__pycache__/
|
|
3
|
+
*.py[cod]
|
|
4
|
+
*$py.class
|
|
5
|
+
*.so
|
|
6
|
+
.Python
|
|
7
|
+
build/
|
|
8
|
+
develop-eggs/
|
|
9
|
+
dist/
|
|
10
|
+
downloads/
|
|
11
|
+
eggs/
|
|
12
|
+
.eggs/
|
|
13
|
+
lib/
|
|
14
|
+
lib64/
|
|
15
|
+
parts/
|
|
16
|
+
sdist/
|
|
17
|
+
var/
|
|
18
|
+
wheels/
|
|
19
|
+
*.egg-info/
|
|
20
|
+
.installed.cfg
|
|
21
|
+
*.egg
|
|
22
|
+
|
|
23
|
+
# uv
|
|
24
|
+
.venv/
|
|
25
|
+
|
|
26
|
+
# Config with real credentials
|
|
27
|
+
config.toml
|
|
28
|
+
|
|
29
|
+
# atvremote's own storage file, if run with cwd here (its default
|
|
30
|
+
# --storage-filename is relative, not $HOME-based like pyatv's library
|
|
31
|
+
# default) -- can contain real pairing credentials.
|
|
32
|
+
.pyatv.conf
|
|
33
|
+
|
|
34
|
+
# IDE
|
|
35
|
+
.vscode/
|
|
36
|
+
.idea/
|
|
37
|
+
*.swp
|
|
38
|
+
*.swo
|
|
39
|
+
|
|
40
|
+
# OS
|
|
41
|
+
.DS_Store
|
|
42
|
+
Thumbs.db
|
pyatv_http-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Hugo Haas
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: pyatv-http
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: HTTP interface for controlling Apple TVs via pyatv
|
|
5
|
+
Project-URL: Homepage, https://github.com/hugoh/pyatv-http
|
|
6
|
+
Project-URL: Repository, https://github.com/hugoh/pyatv-http
|
|
7
|
+
Project-URL: Issues, https://github.com/hugoh/pyatv-http/issues
|
|
8
|
+
Author: Hugo Haas
|
|
9
|
+
License-Expression: MIT
|
|
10
|
+
License-File: LICENSE
|
|
11
|
+
Classifier: Development Status :: 3 - Alpha
|
|
12
|
+
Classifier: Environment :: Console
|
|
13
|
+
Classifier: Environment :: Web Environment
|
|
14
|
+
Classifier: Intended Audience :: End Users/Desktop
|
|
15
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
16
|
+
Classifier: Operating System :: OS Independent
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
18
|
+
Classifier: Topic :: Home Automation
|
|
19
|
+
Requires-Python: >=3.12
|
|
20
|
+
Requires-Dist: fastapi>=0.121.0
|
|
21
|
+
Requires-Dist: pyatv>=0.16.0
|
|
22
|
+
Requires-Dist: uvicorn>=0.38.0
|
|
23
|
+
Description-Content-Type: text/markdown
|
|
24
|
+
|
|
25
|
+
# pyatv-http
|
|
26
|
+
|
|
27
|
+
An HTTP interface for controlling Apple TVs, built on top of
|
|
28
|
+
[pyatv](https://github.com/postlund/pyatv).
|
|
29
|
+
|
|
30
|
+
## Features
|
|
31
|
+
|
|
32
|
+
- `POST /<name>/turnOn` and `POST /<name>/turnOff` — checks the Apple TV's current
|
|
33
|
+
power state and only sends a command if it differs from the desired state.
|
|
34
|
+
- `GET /<name>/powerState` — reads the current power state without changing it.
|
|
35
|
+
- `GET /devices` — lists the devices available in the config file.
|
|
36
|
+
- `GET /health` — unauthenticated liveness check, for load balancers/uptime
|
|
37
|
+
monitors.
|
|
38
|
+
- Every other request requires a bearer token, configured as a list of
|
|
39
|
+
accepted tokens in the config file.
|
|
40
|
+
- Config-driven: one TOML file lists the port to listen on, accepted API
|
|
41
|
+
tokens, and the paired devices.
|
|
42
|
+
- Pairing stays out-of-band, via pyatv's own `atvremote` CLI; a `pyatv-http gen-config`
|
|
43
|
+
helper turns a paired device's stored credentials into a config snippet.
|
|
44
|
+
|
|
45
|
+
## Setup
|
|
46
|
+
|
|
47
|
+
### 1. Find your Apple TV
|
|
48
|
+
|
|
49
|
+
```sh
|
|
50
|
+
uvx --from pyatv atvremote scan
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
This lists every Apple TV on the network along with its identifiers, IP
|
|
54
|
+
address, and which protocols it supports (AirPlay, Companion, etc).
|
|
55
|
+
|
|
56
|
+
### 2. Pair with `atvremote`
|
|
57
|
+
|
|
58
|
+
Power control uses the **Companion** protocol, and pyatv-http talks to the
|
|
59
|
+
device over **AirPlay** for the initial handshake, so pair both:
|
|
60
|
+
|
|
61
|
+
```sh
|
|
62
|
+
uvx --from pyatv atvremote --id <device-identifier> pair --protocol airplay
|
|
63
|
+
uvx --from pyatv atvremote --id <device-identifier> pair --protocol companion
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
Follow the on-screen PIN prompt for each. Credentials are stored in pyatv's
|
|
67
|
+
storage file (default `~/.pyatv.conf`).
|
|
68
|
+
|
|
69
|
+
### 3. Generate a config entry
|
|
70
|
+
|
|
71
|
+
```sh
|
|
72
|
+
uv run pyatv-http gen-config \
|
|
73
|
+
--identifier <device-identifier> \
|
|
74
|
+
--address <device-ip-or-hostname> \
|
|
75
|
+
--key living_room
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
`<device-identifier>` and `<device-ip-or-hostname>` come from the `scan`
|
|
79
|
+
output in step 1; `--key` is whatever short name you want to use in URLs
|
|
80
|
+
(`--name` sets the human-readable display name too, defaulting to `--key`).
|
|
81
|
+
|
|
82
|
+
This prints a `[devices.living_room]` TOML block built from the stored
|
|
83
|
+
pairing. Paste it into your config file (see below). If the identifier
|
|
84
|
+
doesn't match any paired device, the command lists the identifiers it does
|
|
85
|
+
have on file.
|
|
86
|
+
|
|
87
|
+
### 4. Write the config file
|
|
88
|
+
|
|
89
|
+
```toml
|
|
90
|
+
port = 8080
|
|
91
|
+
|
|
92
|
+
[auth]
|
|
93
|
+
tokens = ["a-long-random-token"]
|
|
94
|
+
|
|
95
|
+
[devices.living_room]
|
|
96
|
+
name = "Living Room"
|
|
97
|
+
identifier = "AA:BB:CC:DD:EE:FF"
|
|
98
|
+
address = "10.0.0.5"
|
|
99
|
+
|
|
100
|
+
[devices.living_room.protocols.airplay]
|
|
101
|
+
identifier = "AA:BB:CC:DD:EE:FF"
|
|
102
|
+
credentials = "..."
|
|
103
|
+
|
|
104
|
+
[devices.living_room.protocols.companion]
|
|
105
|
+
identifier = "11:22:33:44:55:66"
|
|
106
|
+
credentials = "..."
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
- `port` — port the HTTP server listens on.
|
|
110
|
+
- `[auth].tokens` — required, non-empty list of bearer tokens accepted on every
|
|
111
|
+
request (see [API](#api) below). Generate one with e.g.
|
|
112
|
+
`python3 -c "import secrets; print(secrets.token_urlsafe(32))"`.
|
|
113
|
+
- The `[devices.<key>]` table key (`living_room` above) is the URL path
|
|
114
|
+
segment used in requests, e.g. `POST /living_room/turnOn`.
|
|
115
|
+
- `identifier` — the device's main pyatv identifier (from `atvremote scan`).
|
|
116
|
+
- `address` — the Apple TV's IP address or hostname; used to connect
|
|
117
|
+
directly instead of relying on mDNS discovery at request time.
|
|
118
|
+
- `[devices.<key>.protocols.<protocol>]` — one block per paired protocol,
|
|
119
|
+
exactly as generated by `gen-config`. Supported protocol names: `airplay`,
|
|
120
|
+
`companion`, `dmap`, `mrp`, `raop`.
|
|
121
|
+
|
|
122
|
+
### 5. Run the server
|
|
123
|
+
|
|
124
|
+
```sh
|
|
125
|
+
uv run pyatv-http serve --config config.toml
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
`--config` is optional; if omitted, it defaults to
|
|
129
|
+
`$XDG_CONFIG_HOME/pyatv-http/config.toml`, falling back to
|
|
130
|
+
`~/.config/pyatv-http/config.toml` when `$XDG_CONFIG_HOME` isn't set.
|
|
131
|
+
|
|
132
|
+
By default the server binds `0.0.0.0`; pass `--host` to bind a specific
|
|
133
|
+
interface.
|
|
134
|
+
|
|
135
|
+
## API
|
|
136
|
+
|
|
137
|
+
Every request must include one of the configured tokens as a bearer token:
|
|
138
|
+
|
|
139
|
+
```sh
|
|
140
|
+
TOKEN=a-long-random-token
|
|
141
|
+
|
|
142
|
+
curl -X POST http://localhost:8080/living_room/turnOn \
|
|
143
|
+
-H "Authorization: Bearer $TOKEN"
|
|
144
|
+
|
|
145
|
+
curl -X POST http://localhost:8080/living_room/turnOff \
|
|
146
|
+
-H "Authorization: Bearer $TOKEN"
|
|
147
|
+
|
|
148
|
+
curl http://localhost:8080/living_room/powerState \
|
|
149
|
+
-H "Authorization: Bearer $TOKEN"
|
|
150
|
+
|
|
151
|
+
curl http://localhost:8080/devices \
|
|
152
|
+
-H "Authorization: Bearer $TOKEN"
|
|
153
|
+
|
|
154
|
+
curl http://localhost:8080/health
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
`turnOn`/`turnOff`/`powerState` return a JSON body:
|
|
158
|
+
`{"device": "living_room", "power_state": "on"}`.
|
|
159
|
+
|
|
160
|
+
`GET /devices` returns the devices available in the config file:
|
|
161
|
+
|
|
162
|
+
```json
|
|
163
|
+
[{"device": "living_room", "name": "Living Room"}]
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
`GET /health` (no token required) returns `{"status": "ok"}`.
|
|
167
|
+
|
|
168
|
+
| Status | Meaning |
|
|
169
|
+
| ------ | ------------------------------------------------------------------ |
|
|
170
|
+
| 200 | Command sent (or no-op, if the device was already in that state) |
|
|
171
|
+
| 401 | Missing or invalid bearer token |
|
|
172
|
+
| 404 | No device configured under that name |
|
|
173
|
+
| 504 | Device could not be found/reached on the network |
|
|
174
|
+
| 502 | pyatv raised an error while connecting or sending the command |
|
|
175
|
+
|
|
176
|
+
### Interactive API docs
|
|
177
|
+
|
|
178
|
+
FastAPI auto-generates interactive documentation for the running server:
|
|
179
|
+
|
|
180
|
+
- `GET /docs` — Swagger UI (use the "Authorize" button to set your bearer
|
|
181
|
+
token, then try requests directly from the browser).
|
|
182
|
+
- `GET /redoc` — ReDoc view of the same schema.
|
|
183
|
+
- `GET /openapi.json` — the raw OpenAPI schema.
|
|
184
|
+
|
|
185
|
+
These three routes are not themselves behind the bearer-token check.
|
|
186
|
+
|
|
187
|
+
## Notes
|
|
188
|
+
|
|
189
|
+
- Each request opens a fresh connection to the Apple TV, checks its current
|
|
190
|
+
power state, and only sends `turnOn`/`turnOff` if it differs from the
|
|
191
|
+
desired state — there's no persistent connection or background polling.
|
|
192
|
+
- Pairing is entirely out-of-band via `atvremote`; this project never
|
|
193
|
+
performs the pairing handshake itself.
|
|
194
|
+
|
|
195
|
+
## Development
|
|
196
|
+
|
|
197
|
+
```sh
|
|
198
|
+
uv sync
|
|
199
|
+
uv run pytest
|
|
200
|
+
hk check --all
|
|
201
|
+
```
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
# pyatv-http
|
|
2
|
+
|
|
3
|
+
An HTTP interface for controlling Apple TVs, built on top of
|
|
4
|
+
[pyatv](https://github.com/postlund/pyatv).
|
|
5
|
+
|
|
6
|
+
## Features
|
|
7
|
+
|
|
8
|
+
- `POST /<name>/turnOn` and `POST /<name>/turnOff` — checks the Apple TV's current
|
|
9
|
+
power state and only sends a command if it differs from the desired state.
|
|
10
|
+
- `GET /<name>/powerState` — reads the current power state without changing it.
|
|
11
|
+
- `GET /devices` — lists the devices available in the config file.
|
|
12
|
+
- `GET /health` — unauthenticated liveness check, for load balancers/uptime
|
|
13
|
+
monitors.
|
|
14
|
+
- Every other request requires a bearer token, configured as a list of
|
|
15
|
+
accepted tokens in the config file.
|
|
16
|
+
- Config-driven: one TOML file lists the port to listen on, accepted API
|
|
17
|
+
tokens, and the paired devices.
|
|
18
|
+
- Pairing stays out-of-band, via pyatv's own `atvremote` CLI; a `pyatv-http gen-config`
|
|
19
|
+
helper turns a paired device's stored credentials into a config snippet.
|
|
20
|
+
|
|
21
|
+
## Setup
|
|
22
|
+
|
|
23
|
+
### 1. Find your Apple TV
|
|
24
|
+
|
|
25
|
+
```sh
|
|
26
|
+
uvx --from pyatv atvremote scan
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
This lists every Apple TV on the network along with its identifiers, IP
|
|
30
|
+
address, and which protocols it supports (AirPlay, Companion, etc).
|
|
31
|
+
|
|
32
|
+
### 2. Pair with `atvremote`
|
|
33
|
+
|
|
34
|
+
Power control uses the **Companion** protocol, and pyatv-http talks to the
|
|
35
|
+
device over **AirPlay** for the initial handshake, so pair both:
|
|
36
|
+
|
|
37
|
+
```sh
|
|
38
|
+
uvx --from pyatv atvremote --id <device-identifier> pair --protocol airplay
|
|
39
|
+
uvx --from pyatv atvremote --id <device-identifier> pair --protocol companion
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
Follow the on-screen PIN prompt for each. Credentials are stored in pyatv's
|
|
43
|
+
storage file (default `~/.pyatv.conf`).
|
|
44
|
+
|
|
45
|
+
### 3. Generate a config entry
|
|
46
|
+
|
|
47
|
+
```sh
|
|
48
|
+
uv run pyatv-http gen-config \
|
|
49
|
+
--identifier <device-identifier> \
|
|
50
|
+
--address <device-ip-or-hostname> \
|
|
51
|
+
--key living_room
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
`<device-identifier>` and `<device-ip-or-hostname>` come from the `scan`
|
|
55
|
+
output in step 1; `--key` is whatever short name you want to use in URLs
|
|
56
|
+
(`--name` sets the human-readable display name too, defaulting to `--key`).
|
|
57
|
+
|
|
58
|
+
This prints a `[devices.living_room]` TOML block built from the stored
|
|
59
|
+
pairing. Paste it into your config file (see below). If the identifier
|
|
60
|
+
doesn't match any paired device, the command lists the identifiers it does
|
|
61
|
+
have on file.
|
|
62
|
+
|
|
63
|
+
### 4. Write the config file
|
|
64
|
+
|
|
65
|
+
```toml
|
|
66
|
+
port = 8080
|
|
67
|
+
|
|
68
|
+
[auth]
|
|
69
|
+
tokens = ["a-long-random-token"]
|
|
70
|
+
|
|
71
|
+
[devices.living_room]
|
|
72
|
+
name = "Living Room"
|
|
73
|
+
identifier = "AA:BB:CC:DD:EE:FF"
|
|
74
|
+
address = "10.0.0.5"
|
|
75
|
+
|
|
76
|
+
[devices.living_room.protocols.airplay]
|
|
77
|
+
identifier = "AA:BB:CC:DD:EE:FF"
|
|
78
|
+
credentials = "..."
|
|
79
|
+
|
|
80
|
+
[devices.living_room.protocols.companion]
|
|
81
|
+
identifier = "11:22:33:44:55:66"
|
|
82
|
+
credentials = "..."
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
- `port` — port the HTTP server listens on.
|
|
86
|
+
- `[auth].tokens` — required, non-empty list of bearer tokens accepted on every
|
|
87
|
+
request (see [API](#api) below). Generate one with e.g.
|
|
88
|
+
`python3 -c "import secrets; print(secrets.token_urlsafe(32))"`.
|
|
89
|
+
- The `[devices.<key>]` table key (`living_room` above) is the URL path
|
|
90
|
+
segment used in requests, e.g. `POST /living_room/turnOn`.
|
|
91
|
+
- `identifier` — the device's main pyatv identifier (from `atvremote scan`).
|
|
92
|
+
- `address` — the Apple TV's IP address or hostname; used to connect
|
|
93
|
+
directly instead of relying on mDNS discovery at request time.
|
|
94
|
+
- `[devices.<key>.protocols.<protocol>]` — one block per paired protocol,
|
|
95
|
+
exactly as generated by `gen-config`. Supported protocol names: `airplay`,
|
|
96
|
+
`companion`, `dmap`, `mrp`, `raop`.
|
|
97
|
+
|
|
98
|
+
### 5. Run the server
|
|
99
|
+
|
|
100
|
+
```sh
|
|
101
|
+
uv run pyatv-http serve --config config.toml
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
`--config` is optional; if omitted, it defaults to
|
|
105
|
+
`$XDG_CONFIG_HOME/pyatv-http/config.toml`, falling back to
|
|
106
|
+
`~/.config/pyatv-http/config.toml` when `$XDG_CONFIG_HOME` isn't set.
|
|
107
|
+
|
|
108
|
+
By default the server binds `0.0.0.0`; pass `--host` to bind a specific
|
|
109
|
+
interface.
|
|
110
|
+
|
|
111
|
+
## API
|
|
112
|
+
|
|
113
|
+
Every request must include one of the configured tokens as a bearer token:
|
|
114
|
+
|
|
115
|
+
```sh
|
|
116
|
+
TOKEN=a-long-random-token
|
|
117
|
+
|
|
118
|
+
curl -X POST http://localhost:8080/living_room/turnOn \
|
|
119
|
+
-H "Authorization: Bearer $TOKEN"
|
|
120
|
+
|
|
121
|
+
curl -X POST http://localhost:8080/living_room/turnOff \
|
|
122
|
+
-H "Authorization: Bearer $TOKEN"
|
|
123
|
+
|
|
124
|
+
curl http://localhost:8080/living_room/powerState \
|
|
125
|
+
-H "Authorization: Bearer $TOKEN"
|
|
126
|
+
|
|
127
|
+
curl http://localhost:8080/devices \
|
|
128
|
+
-H "Authorization: Bearer $TOKEN"
|
|
129
|
+
|
|
130
|
+
curl http://localhost:8080/health
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
`turnOn`/`turnOff`/`powerState` return a JSON body:
|
|
134
|
+
`{"device": "living_room", "power_state": "on"}`.
|
|
135
|
+
|
|
136
|
+
`GET /devices` returns the devices available in the config file:
|
|
137
|
+
|
|
138
|
+
```json
|
|
139
|
+
[{"device": "living_room", "name": "Living Room"}]
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
`GET /health` (no token required) returns `{"status": "ok"}`.
|
|
143
|
+
|
|
144
|
+
| Status | Meaning |
|
|
145
|
+
| ------ | ------------------------------------------------------------------ |
|
|
146
|
+
| 200 | Command sent (or no-op, if the device was already in that state) |
|
|
147
|
+
| 401 | Missing or invalid bearer token |
|
|
148
|
+
| 404 | No device configured under that name |
|
|
149
|
+
| 504 | Device could not be found/reached on the network |
|
|
150
|
+
| 502 | pyatv raised an error while connecting or sending the command |
|
|
151
|
+
|
|
152
|
+
### Interactive API docs
|
|
153
|
+
|
|
154
|
+
FastAPI auto-generates interactive documentation for the running server:
|
|
155
|
+
|
|
156
|
+
- `GET /docs` — Swagger UI (use the "Authorize" button to set your bearer
|
|
157
|
+
token, then try requests directly from the browser).
|
|
158
|
+
- `GET /redoc` — ReDoc view of the same schema.
|
|
159
|
+
- `GET /openapi.json` — the raw OpenAPI schema.
|
|
160
|
+
|
|
161
|
+
These three routes are not themselves behind the bearer-token check.
|
|
162
|
+
|
|
163
|
+
## Notes
|
|
164
|
+
|
|
165
|
+
- Each request opens a fresh connection to the Apple TV, checks its current
|
|
166
|
+
power state, and only sends `turnOn`/`turnOff` if it differs from the
|
|
167
|
+
desired state — there's no persistent connection or background polling.
|
|
168
|
+
- Pairing is entirely out-of-band via `atvremote`; this project never
|
|
169
|
+
performs the pairing handshake itself.
|
|
170
|
+
|
|
171
|
+
## Development
|
|
172
|
+
|
|
173
|
+
```sh
|
|
174
|
+
uv sync
|
|
175
|
+
uv run pytest
|
|
176
|
+
hk check --all
|
|
177
|
+
```
|
pyatv_http-0.1.0/hk.pkl
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
amends "package://github.com/jdx/hk/releases/download/v1.50.0/hk@1.50.0#/Config.pkl"
|
|
2
|
+
import "package://github.com/jdx/hk/releases/download/v1.50.0/hk@1.50.0#/Builtins.pkl"
|
|
3
|
+
import "package://github.com/hugoh/hk-config/releases/download/v1.0.0/hk-config@1.0.0#/base.pkl" as Base
|
|
4
|
+
|
|
5
|
+
min_hk_version = Base.min_hk_version
|
|
6
|
+
|
|
7
|
+
local linters = new Mapping<String, Step> {
|
|
8
|
+
...Base.base
|
|
9
|
+
|
|
10
|
+
// Python - run via this project's own uv-managed tools, not PATH binaries
|
|
11
|
+
...Base.python
|
|
12
|
+
["ruff"] = (Builtins.ruff) {
|
|
13
|
+
check = "uv run ruff check --force-exclude {{ files }}"
|
|
14
|
+
fix = "uv run ruff check --force-exclude --fix {{ files }}"
|
|
15
|
+
}
|
|
16
|
+
["ruff-format"] = (Builtins.ruff_format) {
|
|
17
|
+
check_diff = "uv run ruff format --quiet --force-exclude --diff {{ files }}"
|
|
18
|
+
fix = "uv run ruff format --quiet --force-exclude {{ files }}"
|
|
19
|
+
}
|
|
20
|
+
["ty"] = (Builtins.ty) {
|
|
21
|
+
check = "uv run ty check {{ files }}"
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
hooks {
|
|
26
|
+
["pre-commit"] {
|
|
27
|
+
fix = true
|
|
28
|
+
stash = "git"
|
|
29
|
+
steps = linters
|
|
30
|
+
}
|
|
31
|
+
["pre-push"] {
|
|
32
|
+
steps = linters
|
|
33
|
+
}
|
|
34
|
+
["check"] {
|
|
35
|
+
steps = linters
|
|
36
|
+
}
|
|
37
|
+
["fix"] {
|
|
38
|
+
fix = true
|
|
39
|
+
steps = linters
|
|
40
|
+
}
|
|
41
|
+
// Enforces Conventional Commits, since release.yml's auto-tagging
|
|
42
|
+
// depends on commit messages following that format.
|
|
43
|
+
["commit-msg"] {
|
|
44
|
+
steps {
|
|
45
|
+
["conventional-commit"] = Builtins.check_conventional_commit
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
[tools]
|
|
2
|
+
uv = "0.11.26"
|
|
3
|
+
hk = "1.50.0"
|
|
4
|
+
biome = "2.5.4"
|
|
5
|
+
actionlint = "1.7.12"
|
|
6
|
+
pinact = "4.1.0"
|
|
7
|
+
ghalint = "1.5.6"
|
|
8
|
+
zizmor = "1.26.1"
|
|
9
|
+
gitleaks = "8.30.1"
|
|
10
|
+
typos = "1.48.0"
|
|
11
|
+
rumdl = "0.2.31"
|
|
12
|
+
"github:owenlamont/ryl" = "0.21.0"
|
|
13
|
+
tombi = "1.2.0"
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "pyatv-http"
|
|
3
|
+
dynamic = ["version"]
|
|
4
|
+
description = "HTTP interface for controlling Apple TVs via pyatv"
|
|
5
|
+
authors = [
|
|
6
|
+
{ name = "Hugo Haas" },
|
|
7
|
+
]
|
|
8
|
+
readme = "README.md"
|
|
9
|
+
license = "MIT"
|
|
10
|
+
license-files = ["LICENSE"]
|
|
11
|
+
requires-python = ">=3.12"
|
|
12
|
+
classifiers = [
|
|
13
|
+
"Development Status :: 3 - Alpha",
|
|
14
|
+
"Environment :: Console",
|
|
15
|
+
"Environment :: Web Environment",
|
|
16
|
+
"Intended Audience :: End Users/Desktop",
|
|
17
|
+
"License :: OSI Approved :: MIT License",
|
|
18
|
+
"Operating System :: OS Independent",
|
|
19
|
+
"Programming Language :: Python :: 3.12",
|
|
20
|
+
"Topic :: Home Automation",
|
|
21
|
+
]
|
|
22
|
+
|
|
23
|
+
dependencies = [
|
|
24
|
+
"pyatv>=0.16.0",
|
|
25
|
+
"fastapi>=0.121.0",
|
|
26
|
+
"uvicorn>=0.38.0",
|
|
27
|
+
]
|
|
28
|
+
|
|
29
|
+
[project.urls]
|
|
30
|
+
Homepage = "https://github.com/hugoh/pyatv-http"
|
|
31
|
+
Repository = "https://github.com/hugoh/pyatv-http"
|
|
32
|
+
Issues = "https://github.com/hugoh/pyatv-http/issues"
|
|
33
|
+
|
|
34
|
+
[project.scripts]
|
|
35
|
+
pyatv-http = "pyatv_http.cli:main"
|
|
36
|
+
|
|
37
|
+
[build-system]
|
|
38
|
+
requires = ["hatchling", "hatch-vcs"]
|
|
39
|
+
build-backend = "hatchling.build"
|
|
40
|
+
|
|
41
|
+
[dependency-groups]
|
|
42
|
+
dev = [
|
|
43
|
+
"ty>=0.0.57",
|
|
44
|
+
"ruff>=0.15.20",
|
|
45
|
+
"pytest>=9.1.1",
|
|
46
|
+
"pytest-asyncio>=1.3.0",
|
|
47
|
+
"httpx>=0.28.1",
|
|
48
|
+
]
|
|
49
|
+
|
|
50
|
+
[tool.hatch.build.targets.wheel]
|
|
51
|
+
packages = ["src/pyatv_http"]
|
|
52
|
+
|
|
53
|
+
[tool.hatch.version]
|
|
54
|
+
source = "vcs"
|
|
55
|
+
|
|
56
|
+
[tool.ruff]
|
|
57
|
+
line-length = 88
|
|
58
|
+
target-version = "py312"
|
|
59
|
+
|
|
60
|
+
[tool.ruff.lint]
|
|
61
|
+
select = ["E", "F", "I", "B", "UP", "SIM"]
|
|
62
|
+
|
|
63
|
+
[tool.pytest.ini_options]
|
|
64
|
+
testpaths = ["tests"]
|
|
65
|
+
asyncio_mode = "auto"
|
|
File without changes
|