Z2MClient¶
Zigbee2MQTT HTTP ingress client with three-tier auth fallback.
zigporter.z2m_client.Z2MClient(ha_url: str | None, ha_token: str | None, z2m_url: str, verify_ssl: bool = True, mqtt_topic: str = 'zigbee2mqtt', z2m_frontend_token: str | None = None)
¶
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. Optional in standalone mode.
TYPE:
|
z2m_url
|
Full ingress URL for the Z2M add-on or standalone Z2M
instance (e.g.
TYPE:
|
verify_ssl
|
Set to
TYPE:
|
mqtt_topic
|
Z2M base MQTT topic. Must match the
TYPE:
|
z2m_frontend_token
|
Optional Z2M frontend auth token
(
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).
When no HA URL is configured (standalone Z2M mode), connects directly
to the Z2M frontend WebSocket at <z2m_url>/api.
Otherwise, 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
remove_device(friendly_name: str, force: bool = True) -> None
async
¶
Remove a device from Z2M by friendly name (HTTP fallback -> MQTT).
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
449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 | |