Z2MClient¶
Zigbee2MQTT HTTP ingress client with three-tier auth fallback.
zigporter.z2m_client.Z2MClient(ha_url: str, ha_token: str, z2m_url: str, verify_ssl: bool = True, mqtt_topic: str = 'zigbee2mqtt')
¶
Zigbee2MQTT client.
Auth strategy for ingress requests
- Try the request with
Authorization: Bearer <token>directly. Works when HA ingress forwards the header (some reverse-proxy setups). - If the response is not JSON, exchange the Bearer token for an ingress
session cookie via
POST /api/hassio/ingress/sessionand retry. - If the session exchange fails (no Supervisor, proxy blocks the path, non-admin token), fall back to HA-native APIs:
- Device listing / lookup: HA device registry via WebSocket
- permit_join / rename:
mqtt.publishservice call via WebSocket
The mqtt_topic parameter (default "zigbee2mqtt") must match the
base topic configured in Z2M. Override via the Z2M_MQTT_TOPIC env var.
| PARAMETER | DESCRIPTION |
|---|---|
ha_url
|
Base URL of the Home Assistant instance
(e.g.
TYPE:
|
ha_token
|
Long-lived HA access token. Used for ingress session exchange and as the fallback HA-native client credential.
TYPE:
|
z2m_url
|
Full ingress URL for the Z2M add-on
(e.g.
TYPE:
|
verify_ssl
|
Set to
TYPE:
|
mqtt_topic
|
Z2M base MQTT topic. Must match the
TYPE:
|
Source code in src/zigporter/z2m_client.py
disable_permit_join() -> None
async
¶
Close the Z2M network to new joiners.
Source code in src/zigporter/z2m_client.py
enable_permit_join(seconds: int = 120) -> None
async
¶
Open the Z2M network for new devices to join.
Source code in src/zigporter/z2m_client.py
get_device_by_ieee(ieee: str) -> dict[str, Any] | None
async
¶
Find a Z2M device by IEEE address. Returns None if not found.
Source code in src/zigporter/z2m_client.py
get_devices() -> list[dict[str, Any]]
async
¶
Return the full Z2M device list.
Source code in src/zigporter/z2m_client.py
get_network_map(timeout: int = _NETWORK_MAP_TIMEOUT) -> dict[str, Any]
async
¶
Return the raw Z2M network map (nodes + links).
Tries the Z2M HTTP REST endpoint first. Z2M 2.x removed that endpoint,
so falls back to subscribing to the MQTT response topic and publishing
the request via HA's WebSocket call_service API.
timeout controls how long to wait for Z2M to respond via MQTT.
Large meshes with many routers need more time — Z2M scans each router
sequentially, so allow ~10 s per router.
Source code in src/zigporter/z2m_client.py
rename_device(current_name: str, new_name: str) -> None
async
¶
Rename a Z2M device by its current friendly name.
Source code in src/zigporter/z2m_client.py
wait_for_interview(ieee: str, timeout: int = 300, on_event: Any = None) -> tuple[str, dict[str, Any] | None]
async
¶
Subscribe to Z2M bridge events and wait for device interview completion.
Subscribes to zigbee2mqtt/bridge/event via HA WebSocket and streams
events until the target device's interview completes, fails, or the
timeout expires.
on_event(event_type, data) is called for every bridge event received
so the caller can display status updates or detect unexpected joiners.
Returns a tuple (status, data):
("successful", data)— interview completed (or device re-announced)("failed", data)— interview failed("timeout", None)— timed out without a result
Source code in src/zigporter/z2m_client.py
335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 | |