A library for interacting with Mercury switches.
Project description
py-mercury-switch-api
A Python library for interacting with Mercury (水星) network switches via their web interface.
Features
- 🔍 Auto-detection - Automatically detects switch model
- 📊 System Info - Retrieve switch model, MAC address, IP, firmware version
- 🔌 Port Status - Monitor port states, connection speeds, and link status
- 📈 Traffic Statistics - Get TX/RX packet counts per port
- 🏷️ VLAN Support - Read 802.1Q VLAN configuration
- 🧪 Offline Mode - Test with saved HTML pages for development
Installation
pip install py-mercury-switch-api
Quick Start
from py_mercury_switch_api import MercurySwitchConnector
# Create connector
connector = MercurySwitchConnector(
host="192.168.1.1",
username="admin",
password="password"
)
# Login and get switch information
if connector.get_login_cookie():
# Auto-detect switch model
connector.autodetect_model()
print(f"Detected: {connector.switch_model.MODEL_NAME}")
# Get all switch information
info = connector.get_switch_infos()
print(info)
API Reference
MercurySwitchConnector
The main class for interacting with Mercury switches.
Constructor
MercurySwitchConnector(host: str, username: str, password: str)
| Parameter | Type | Description |
|---|---|---|
host |
str |
IP address or hostname of the switch |
username |
str |
Login username (usually admin) |
password |
str |
Login password |
Methods
| Method | Description |
|---|---|
get_login_cookie() |
Authenticate and obtain session cookie. Returns True on success. |
autodetect_model() |
Detect and configure the switch model automatically. |
get_switch_infos() |
Retrieve all available switch information as a dictionary. |
get_unique_id() |
Get a unique identifier for the switch (model + IP). |
Return Data Structure
The get_switch_infos() method returns a dictionary containing:
{
# System Information
"switch_model": "SG108-Pro",
"switch_mac": "AA:BB:CC:DD:EE:FF",
"switch_ip": "192.168.1.1",
"switch_firmware": "1.0.0",
"switch_hardware": "V1.0",
# Port Status (for each port 1-N)
"port_1_state": "on", # Port enabled state
"port_1_status": "on", # Link status (connected/disconnected)
"port_1_speed": "1000M Full", # Configured speed
"port_1_connection_speed": "1000M Full", # Actual connection speed
# Traffic Statistics (for each port)
"port_1_tx_good": 123456, # TX good packets
"port_1_tx_bad": 0, # TX bad packets
"port_1_rx_good": 654321, # RX good packets
"port_1_rx_bad": 0, # RX bad packets
# VLAN Information
"vlan_enabled": True,
"vlan_type": "802.1Q",
"vlan_count": 2,
"vlan_1_name": "default",
"vlan_1_tagged_ports": "1, 2",
"vlan_1_untagged_ports": "3, 4, 5, 6, 7, 8",
}
Supported Models
| Model | Ports | Status |
|---|---|---|
| SG108Pro | 8 | ✅ Supported |
| SG105E | 5 | 🚧 Planned |
Home Assistant Integration
This library is designed to be used with Home Assistant. A custom integration is available at:
Coming soon
Development
Setup
# Clone the repository
git clone https://github.com/daxingplay/py-mercury-switch-api.git
cd py-mercury-switch-api
# Install in development mode
pip install -e ".[dev]"
# Or install dependencies manually
pip install -e .
pip install pytest pytest-cov ruff mypy
Running Tests
pytest
Code Quality
# Format code
ruff format .
# Lint
ruff check .
# Type check
mypy src/py_mercury_switch_api
Adding New Model Support
See CONTRIBUTING.md for detailed instructions on how to add support for new Mercury switch models.
Quick Overview
- Create a model class in
models.py:
class SG116E(AutodetectedMercuryModel):
"""Mercury SG116E 16-port switch."""
MODEL_NAME = "SG116E"
PORTS = 16
CHECKS_AND_RESULTS: ClassVar = [
("check_system_info_model", ["SG116E"]),
]
-
Add test fixtures in
tests/fixtures/MODEL_NAME/0/ -
Run tests to verify the model works correctly
Contributing
Contributions are welcome! Please see CONTRIBUTING.md for guidelines.
How to Contribute
- Fork the repository
- Create your feature branch (
git checkout -b feature/new-model) - Commit your changes (
git commit -am 'Add support for SG116E') - Push to the branch (
git push origin feature/new-model) - Open a Pull Request
License
This project is licensed under the Apache Software License 2.0 - see the LICENSE file for details.
Acknowledgments
- Inspired by py-netgear-plus
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 py_mercury_switch_api-0.3.0.tar.gz.
File metadata
- Download URL: py_mercury_switch_api-0.3.0.tar.gz
- Upload date:
- Size: 29.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2322582dfd1b03a35d5b76b8a111067f403ed5b30767cea9c87a1ab1d9ec37f2
|
|
| MD5 |
f90316e566c305c7e7824c40bab0fcee
|
|
| BLAKE2b-256 |
9d08f204dcaeca2a117f40d4c368bbf5dda8e036812f595390d986edc8f8f00a
|
Provenance
The following attestation bundles were made for py_mercury_switch_api-0.3.0.tar.gz:
Publisher:
publish.yml on daxingplay/py-mercury-switch-api
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
py_mercury_switch_api-0.3.0.tar.gz -
Subject digest:
2322582dfd1b03a35d5b76b8a111067f403ed5b30767cea9c87a1ab1d9ec37f2 - Sigstore transparency entry: 833619504
- Sigstore integration time:
-
Permalink:
daxingplay/py-mercury-switch-api@fd622348e42e608ea357eab96a76410e0cb150e1 -
Branch / Tag:
refs/tags/v0.3.0 - Owner: https://github.com/daxingplay
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@fd622348e42e608ea357eab96a76410e0cb150e1 -
Trigger Event:
release
-
Statement type:
File details
Details for the file py_mercury_switch_api-0.3.0-py3-none-any.whl.
File metadata
- Download URL: py_mercury_switch_api-0.3.0-py3-none-any.whl
- Upload date:
- Size: 19.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4581dba02e17000840ef97efc6595e18c9c757320e7a1b9c569797fb1afde9df
|
|
| MD5 |
7039aa0086bb935e080cac3230962b79
|
|
| BLAKE2b-256 |
625a8aced75c3258041db8d7f0c035be9f2ba317c1a96c028cc65fbc3568104b
|
Provenance
The following attestation bundles were made for py_mercury_switch_api-0.3.0-py3-none-any.whl:
Publisher:
publish.yml on daxingplay/py-mercury-switch-api
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
py_mercury_switch_api-0.3.0-py3-none-any.whl -
Subject digest:
4581dba02e17000840ef97efc6595e18c9c757320e7a1b9c569797fb1afde9df - Sigstore transparency entry: 833619506
- Sigstore integration time:
-
Permalink:
daxingplay/py-mercury-switch-api@fd622348e42e608ea357eab96a76410e0cb150e1 -
Branch / Tag:
refs/tags/v0.3.0 - Owner: https://github.com/daxingplay
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@fd622348e42e608ea357eab96a76410e0cb150e1 -
Trigger Event:
release
-
Statement type: