Skip to main content

Milesight Gateway API

GitHub Release PyPI Python versions Downloads Test Pylint 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; Wi-Fi and WireGuard settings
  • Firmware: releases from Milesight's download page, full backups and upgrades, including the version a release requires first
  • 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 client needs Python 3.9 or newer, the LoRaWAN connection Python 3.12. 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

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.

info = await client.get_login_status(session)   # model, firmware; no login needed
system = await client.get_system_info(session)  # serial, uptime, CPU, memory

Firmware

Milesight's download page lists the releases of each product family. Some releases only install on a minimum version; next_firmware() names the release to install next on the way to the newest, which may be that minimum version.

from milesight_gateway_api import download_firmware, get_firmware_releases, next_firmware

releases = await get_firmware_releases(session, "60")  # 60: UG65 and UG67, oldest first
step = next_firmware(info["rtver"], releases)          # None: nothing to install
if step:
    backup = await client.download_backup(session)     # full configuration, keep it
    image = await download_firmware(session, step)
    await client.upgrade_firmware(session, image)      # installs and reboots

upgrade_firmware() uploads the image the way the Upgrade button does; the gateway installs it and reboots, which takes a few minutes. reset=True also puts it back to factory settings. The backup holds keys and passwords.

Wi-Fi and WireGuard

Both are settings of the web interface. A call stores its values and applies them, like the page's Save and Apply buttons; apply=False only stores, and apply_web_config() then puts everything stored into effect at once.

wifi = await client.get_wifi(session)                # enabled, mode, ssid, encryption, key
await client.set_wifi_enabled(session, False)        # access point off
await client.set_wifi_access_point(session, "Workshop", "a-long-key")  # WPA2, on

interfaces = await client.get_wireguard(session)     # wg0 to wg2 as index 1 to 3
print(interfaces[0]["public_key"])                   # for the server's peer entry
await client.set_wireguard_interface(
    session, 1, enable=True, address="10.0.0.2/24", apply=False
)
await client.set_wireguard_peer(
    session, 1, "server", "<server public key>", ["10.0.0.0/24"],
    endpoint="vpn.example.com", port=51820, keepalive=25, apply=False,
)
await client.apply_web_config(session)
await client.remove_wireguard_peer(session, 1, "server")

Keys come back in clear text from get_wifi() and get_wireguard(). A value the gateway refuses raises WebConfigError.

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 1.0.1

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 1.0.1
File Size Uploaded Provenance Actions
milesight_gateway_api-1.0.1.tar.gz 329.0 kB With provenance Details for milesight_gateway_api-1.0.1.tar.gz

Built distribution (wheel)

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

Total release size: 641.9 kB

Release files / milesight_gateway_api-1.0.1.tar.gz

Download URL milesight_gateway_api-1.0.1.tar.gz
Size 329.0 kB
Tags Source
SHA-256 checksum
How to use checksums
e5151aef481ebbc23f6e8885608d1208df8ce96644fe4cae7695ee72d805e509
BLAKE2b-256 checksum
How to use checksums
2aeaf4992c567df0a1a037b5037038cffe98b171a206a2b873fcc253a3fdeabd
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-1.0.1-py3-none-any.whl

Download URL milesight_gateway_api-1.0.1-py3-none-any.whl
Size 312.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
846e833b4105ccac7dbcef583c6857c7ba48954ce69fd7770758e6cd04d89f3a
BLAKE2b-256 checksum
How to use checksums
8cef5427f40a72c158d036375eb2297c138747f66b582d8aa0d97e0a3683ec40
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 1.0.1

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-1.0.1.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