Skip to content

Migrate serial backend from pyserial to serialx#13

Merged
OnFreund merged 4 commits into
OnFreund:masterfrom
balloob:master
Jun 27, 2026
Merged

Migrate serial backend from pyserial to serialx#13
OnFreund merged 4 commits into
OnFreund:masterfrom
balloob:master

Conversation

@balloob

@balloob balloob commented Feb 27, 2026

Copy link
Copy Markdown
Contributor

Hey,

With the Open Home Foundation we're working on adding serial proxies to ESPHome. It will allow users to connect an ESPHome device to the serial port, and then connect to it via Home Assistant. Seamlessly :-)

Because python serial land has been quite stale (with fixes not getting published), we've been working on our own serial library serialx which is a drop-in replacement for python-serial. It is already used inside Home Assistant for things like Zigbee.

We will soon add support for the ESPHome serial proxies to serialx (PR) and HA (TBD), and so are looking into getting interesting integrations in HA ready to be able to leverage this.

I've asked AI to migrate this lib to serialx, so that when ESPHome support lands, it can just be a dep bump.

Would you be open for switching the serial library and test this PR?

~Paulus

(below written by AI)

CoPilot summary

This updates the library to use serialx as the serial transport backend instead of pyserial/pyserial-asyncio-fast, covering both sync and asyncio paths. The migration keeps the existing pymonoprice API behavior intact while switching underlying serial implementation.

  • Runtime migration (sync + async)

    • Replaced serial and serial_asyncio_fast usage with serialx in core runtime code.
    • Switched sync port construction from serial_for_url(...) to serialx.Serial(...) with equivalent serial settings.
    • Switched async connection creation to serialx.create_serial_connection(...).
    • Updated transport type references to serialx.SerialTransport.
  • Timeout/error compatibility

    • Introduced a module-level SerialTimeoutException and raised it on sync read timeout, preserving the package-level timeout contract used by callers/tests.
  • Dependency updates

    • Replaced pyserial and pyserial-asyncio-fast requirements with serialx>=0.7.0 in packaging and requirements files.
    • Removed now-obsolete mypy ignore entries for removed serial modules.
  • Tests aligned with new backend contract

    • Updated timeout assertion imports in tests to reference pymonoprice.SerialTimeoutException instead of serial.SerialTimeoutException.
# before
import serial
from serial_asyncio_fast import create_serial_connection

self._port = serial.serial_for_url(port_url, do_not_open=True)

# after
import serialx

self._port = serialx.Serial(
    port_url,
    baudrate=9600,
    stopbits=serialx.StopBits.ONE,
    byte_size=8,
    parity=serialx.Parity.NONE,
)
_, protocol = await serialx.create_serial_connection(loop, MonopriceProtocol, port_url, baudrate=9600)

💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Comment thread pymonoprice/__init__.py
@balloob

balloob commented Apr 22, 2026

Copy link
Copy Markdown
Contributor Author

Home Assistant 2026.5 will have full support for ESPHome proxies and a new Serial Port selector that the Monoprice integration could leverage when migrating to serialx.

@balloob

balloob commented Jun 25, 2026

Copy link
Copy Markdown
Contributor Author

@etsinko could you take a look?

@etsinko

etsinko commented Jun 25, 2026

Copy link
Copy Markdown
Collaborator

@balloob I can't test it as I don't have monoprice hardware anymore. If you are confident that the migration is correct I can do the merge without problem.

Comment thread pymonoprice/__init__.py Outdated
@OnFreund

Copy link
Copy Markdown
Owner

Hi @etsinko, I maintain the Monoprice integration in HA. Since you no longer have the hardware, maybe it will make sense to transfer this project over to the Open Home Foundation or to me?
I can fork the repo, but if we transfer this we're going to have to handle PyPI as well.

@etsinko

etsinko commented Jun 26, 2026

Copy link
Copy Markdown
Collaborator

@OnFreund yes that would work, otherwise everything I'd do in this repo would be released untested. I can transfer the project to you.

@OnFreund

Copy link
Copy Markdown
Owner

Thanks @etsinko. Can you transfer in PyPI as well?

@balloob

balloob commented Jun 26, 2026

Copy link
Copy Markdown
Contributor Author

Excellent. I've been in touch with OnFreund and support this!

@etsinko

etsinko commented Jun 26, 2026

Copy link
Copy Markdown
Collaborator

@OnFreund I think I can only add you as a collaborator in PyPi with full access but I can't transfer it to you or at least I didn't find how to do that. What is your username on pypi?

@OnFreund

Copy link
Copy Markdown
Owner

Copilot AI and others added 3 commits June 27, 2026 09:51
* Initial plan

* feat: migrate serial backend to serialx

Co-authored-by: balloob <1444314+balloob@users.noreply.github.com>

* docs: clarify serialx port initialization

Co-authored-by: balloob <1444314+balloob@users.noreply.github.com>

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: balloob <1444314+balloob@users.noreply.github.com>
Co-authored-by: puddly <32534428+puddly@users.noreply.github.com>
Use asyncio.BaseTransport to satisfy the LSP constraint from the base protocol.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@OnFreund

Copy link
Copy Markdown
Owner

Tested on my unit

@OnFreund
OnFreund merged commit 91607eb into OnFreund:master Jun 27, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants