Async library to control Sony Bravia TVs over RS232, built on serialkit
Project description
sony-tv-rs232
Async Python library to control Sony Bravia TVs over RS232 serial, built on serialkit and serialx.
Installation
pip install sony-tv-rs232
# To talk to a TV over an ESPHome serial proxy:
pip install 'sony-tv-rs232[esphome]'
Requires Python 3.14+.
Quick start
import asyncio
from sony_tv_rs232 import SonyTV, InputSource
async def main():
tv = SonyTV("/dev/ttyUSB0")
await tv.connect()
await tv.enable_standby_listening() # so power_on works from standby
await tv.power_on()
await tv.set_volume(20)
await tv.select_input_source(InputSource.HDMI1)
await tv.disconnect()
asyncio.run(main())
CLI
python -m sony_tv_rs232 /dev/ttyUSB0 # query + print status
python -m sony_tv_rs232 socket://192.168.1.29:5000 # raw TCP bridge
python -m sony_tv_rs232 'esphome://192.168.1.29/?port_name=TTL'
python -m sony_tv_rs232 /dev/ttyUSB0 --power on
python -m sony_tv_rs232 /dev/ttyUSB0 --input HDMI1
python -m sony_tv_rs232 /dev/ttyUSB0 --diagnose # raw wiring diagnosis
Protocol notes
Sony's consumer RS-232C protocol is effectively set-only: the TV acknowledges every Set command but most models ignore query packets, and the answer frame carries no echo of which command it answers. The library:
- updates
stateoptimistically when a Set is acknowledged, and from query replies on the Pro Bravia displays that answer them; - runs every command as an exclusive exchange, because answers are unaddressed — holding the wire for one send-and-read round, anchored on arrival order, is what keeps a dropped or garbled answer from being read as the next command's;
- paces commands ≥ 500 ms apart, as the Sony spec requires;
- reconnects automatically when the serial link drops.
All of that machinery lives in serialkit; this library is the Sony command
surface plus a checksum-discriminated answer framer.
Connection handling
from sony_tv_rs232 import CommandTimeoutError, ConnectionLostError, SonyCommandError
try:
await tv.set_volume(50)
except SonyCommandError as err: # TV returned a non-zero answer code
...
except CommandTimeoutError: # no answer in time (common on set-only TVs)
...
except ConnectionLostError: # serial link dropped
...
Subscribe to state changes; callbacks receive a TVState snapshot, or None
when the connection is lost:
unsub = tv.subscribe(lambda state: print("state:", state))
Serial connection
Sony Bravia sets use 9600 baud, 8N1. The library accepts any
serialx-compatible URL (/dev/ttyUSB0, socket://host:port,
esphome://host/?port_name=TTL).
Development
uv run --python 3.14 pytest
uvx ruff check
The RS-232C protocol reference is in docs/.
License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file sony_tv_rs232-0.2.1.tar.gz.
File metadata
- Download URL: sony_tv_rs232-0.2.1.tar.gz
- Upload date:
- Size: 15.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0aae948cc0d3731ed840a96400b61a552d059d69a3700f192048c27b1d30fb84
|
|
| MD5 |
155ce92bc85b7ffb2b0a3396672fd3f0
|
|
| BLAKE2b-256 |
c696cae84249ad39f675cfc5ca641d34b45c6efb9a2ca569e7bd8ad284e72777
|
Provenance
The following attestation bundles were made for sony_tv_rs232-0.2.1.tar.gz:
Publisher:
publish.yml on bbangert/sony-tv-rs232
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
sony_tv_rs232-0.2.1.tar.gz -
Subject digest:
0aae948cc0d3731ed840a96400b61a552d059d69a3700f192048c27b1d30fb84 - Sigstore transparency entry: 2266907834
- Sigstore integration time:
-
Permalink:
bbangert/sony-tv-rs232@95233e04521782c198b78de44b94adf552a8b4be -
Branch / Tag:
refs/tags/v0.2.1 - Owner: https://github.com/bbangert
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@95233e04521782c198b78de44b94adf552a8b4be -
Trigger Event:
release
-
Statement type:
File details
Details for the file sony_tv_rs232-0.2.1-py3-none-any.whl.
File metadata
- Download URL: sony_tv_rs232-0.2.1-py3-none-any.whl
- Upload date:
- Size: 19.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4078cde47071e9504302a79dee0591c2247f8b5ff3bbfad0324a9c8999f9072a
|
|
| MD5 |
e3ac1789e1907f480b26d95b5c1e2366
|
|
| BLAKE2b-256 |
282dd0d643b78bacb656f36acf4d2f013909ff96c7f14a7ce53a9412311f4e23
|
Provenance
The following attestation bundles were made for sony_tv_rs232-0.2.1-py3-none-any.whl:
Publisher:
publish.yml on bbangert/sony-tv-rs232
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
sony_tv_rs232-0.2.1-py3-none-any.whl -
Subject digest:
4078cde47071e9504302a79dee0591c2247f8b5ff3bbfad0324a9c8999f9072a - Sigstore transparency entry: 2266907927
- Sigstore integration time:
-
Permalink:
bbangert/sony-tv-rs232@95233e04521782c198b78de44b94adf552a8b4be -
Branch / Tag:
refs/tags/v0.2.1 - Owner: https://github.com/bbangert
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@95233e04521782c198b78de44b94adf552a8b4be -
Trigger Event:
release
-
Statement type: