Skip to main content
Pre-release

This release is a pre-release and may not be stable for production use.

Milesight Gateway API

GitHub Release PyPI PyPI - Python Version License

A Python client for Milesight LoRaWAN gateways (UG65, UG67 and their relatives). It talks to the gateway's local REST API and web interface, and decodes uplinks with Milesight's own codecs. The Home Assistant integration is built on it.

  • Network server: devices, applications, payload codecs, profiles, gateways of a fleet, packet log
  • Downlinks: queue a command for the gateway's codec, or raw bytes
  • Web interface: model, firmware, serial number, CPU, memory, storage, uptime
  • Firmware: the newest release from Milesight's download page
  • Decoder (extra): Milesight's decoders and encoders for 145 models, with a field catalog per model
  • LoRaWAN connection (Python 3.12+): a lorawan-connection backend on top of the packet log

It also covers what the gateway's own interface lacks on large installations, such as exporting the device list, the gateways or the custom payload codecs.

Installation

pip install Milesight-Gateway-API            # client
pip install "Milesight-Gateway-API[decoder]" # client and decoder

The gateway needs firmware 60.0.0.42-r5 or newer.

Usage

The client takes the gateway login and the AES Secret Key and IV from the gateway's security settings. Copy examples/.env_example to .env for the examples.

import os
from aiohttp import ClientSession
from milesight_gateway_api import MilesightGatewayClient

client = MilesightGatewayClient(
    os.getenv("USERNAME"),
    os.getenv("PASSWORD"),
    os.getenv("SECRET_KEY").encode(),  # 16 characters
    os.getenv("IV").encode(),          # 16 characters
    os.getenv("BASE_URL"),             # https://192.0.2.10
    os.getenv("PORT"),                 # 8080
)

async with ClientSession() as session:
    await client.get_jwt_token(session)                     # log in first
    # One page above the device count gives a consistent list on a busy network.
    devices, total = await client.get_all_devices(session, limit=10000)
    device = await client.get_device(session, "24E1247140000001")

Network server

Call Returns
get_all_devices(session), get_device(session, search) Devices, all or by name or devEUI
get_all_applications(session), get_applications(session) Applications
get_data_transmission_integration(session, app_id, "mqtt") An application's MQTT or HTTP integration
get_payload_codecs(session, "default" | "custom", search=...) Payload codecs
get_payload_codecs_by_device(session, dev_eui) The codec assigned to a device
get_profiles(session, organization_id, application_id) Device profiles
get_gateway_fleet(session, organization_id), get_gateways(session) The gateways of a fleet with their link state
get_packets(session, limit) The newest entries of the packet log
get_network_server_settings(session), get_packet_forwarder_info(session) Network server and packet forwarder

The gateway runs the device's codec over the command, so the keys are that codec's vocabulary. The device picks the downlink up after its next uplink.

await client.enqueue_downlink(session, "24E1247140000001", {"report_interval": 10})
await client.enqueue_raw_downlink(session, "24E1247140000001", bytes.fromhex("ff2800"), 85)
queue = await client.get_downlink_queue(session, "24E1247140000001")

Web interface and firmware

The web interface has its own session with the same account. After one refused login the client stops trying and raises WebLoginRefusedError, because the gateway locks an account after a few failed logins.

from milesight_gateway_api import get_latest_firmware

info = await client.get_login_status(session)   # model, firmware; no login needed
system = await client.get_system_info(session)  # serial, uptime, CPU, memory
release = await get_latest_firmware(session, "60")  # 60: UG65 and UG67
print(release.version, release.download_url)

Decoder

With the decoder extra, the package runs Milesight's own decoders and encoders from Milesight-IoT/codec (MIT) in an embedded JavaScript engine (dukpy), so raw bytes decode exactly as on the gateway. The scripts ship packed; a model's script is unpacked on first use.

from milesight_gateway_api import decoder

decoder.decode("ws203", bytes.fromhex("01754003674400046871050000"))
# {'battery': 64, 'temperature': 6.8, 'humidity': 56.5, 'occupancy': 0}

decoder.models()["ws203"]["fields"]["occupancy"]  # type, unit, access, value names

decoder.encode("wt101", {"report_status": 1})      # b'\xff\x28\x00', for FPort 85
decoder.encode("wt101", decoder.nest({"temperature_control.enable": 0}))

Enumerations come back as raw numbers; the model's fields name them. scripts/update_codecs.py refreshes the bundle from Milesight's repository.

LoRaWAN connection

milesight_gateway_api.lorawan implements the lorawan-connection protocol on top of the network server's packet log: devices, uplinks with their gateway receptions, raw downlinks and the fleet's gateways. It needs Python 3.12 or newer and pip install lorawan-connection.

from lorawan_connection import UplinkEvent
from milesight_gateway_api.lorawan import MilesightConnection

connection = MilesightConnection(client, session, network_id="home")
await connection.async_connect()

def listener(event):
    if isinstance(event, UplinkEvent):
        print(event.dev_eui, event.data.hex(), [rx.rssi for rx in event.receptions])

unsubscribe = await connection.async_subscribe(listener=listener)
gateways = await connection.async_get_gateways()
...
await connection.close()

milesight_gateway_api.lorawan_devices builds on it: devices that decode their uplinks with the bundled codecs and encode settings.

License

MIT, see LICENSE. The bundled codecs are Milesight's, under their own MIT license in milesight_gateway_api/codecs/LICENSE.

Metadata

Release files for Milesight-Gateway-API 0.2.0b2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for Milesight-Gateway-API 0.2.0b2
File Size Uploaded Provenance Actions
milesight_gateway_api-0.2.0b2.tar.gz 316.0 kB With provenance Details for milesight_gateway_api-0.2.0b2.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for Milesight-Gateway-API 0.2.0b2
File Interpreter ABI Platform Actions
milesight_gateway_api-0.2.0b2-py3-none-any.whl Python 3 none any Details for milesight_gateway_api-0.2.0b2-py3-none-any.whl

Total release size: 620.8 kB

Release files / milesight_gateway_api-0.2.0b2.tar.gz

Download URL milesight_gateway_api-0.2.0b2.tar.gz
Size 316.0 kB
Tags Source
SHA-256 checksum
How to use checksums
a1bf7acf6a23f3cfff3a4b584e6be66fd75e71f0b38fec167ceea838802d39cb
BLAKE2b-256 checksum
How to use checksums
fb348e1e5099f8543ebd47807991bb07d78162865caeb06ac9bf13d036f5d507
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 26, 2026.

Transparency log

Release files / milesight_gateway_api-0.2.0b2-py3-none-any.whl

Download URL milesight_gateway_api-0.2.0b2-py3-none-any.whl
Size 304.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
fec1339936368622e5a9e23b2729683393db653e18055b4fc152ba8a2b3ca2ad
BLAKE2b-256 checksum
How to use checksums
a8a04b5bcf457988cf61a0a79e026a594f62c97990196d619a3dbfed4f7f91f8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 26, 2026.

Transparency log

Security for Milesight-Gateway-API 0.2.0b2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for milesight_gateway_api-0.2.0b2.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 26, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page