Skip to content

Add WAN status sensors - #140

Open
akhoury wants to merge 1 commit into
HarvsG:masterfrom
akhoury:master
Open

Add WAN status sensors#140
akhoury wants to merge 1 commit into
HarvsG:masterfrom
akhoury:master

Conversation

@akhoury

@akhoury akhoury commented Apr 28, 2026

Copy link
Copy Markdown

Summary

Adds a read-only sensor per WAN candidate interface (e.g. wan, secondwan, wwan, tethering, modem_*) with three states: connected, failing (link up, no internet reachable), disconnected.

What's new

  • Per-WAN sensors using SensorDeviceClass.ENUM with options connected / failing / disconnected.
  • Friendly names mapped from the raw interface names: wan → "Primary WAN", secondwan → "Secondary WAN", wwan → "WiFi Repeater", tethering → "Phone Tether", modem_x_y_z → "USB Modem
    (modem_x_y_z)", IPv6 variants get an "(IPv6)" suffix. Raw name is exposed as the interface attribute for automations/templates.
  • Auto-discovery: an interface becomes a sensor once it's been seen up at least once, then persists across HA restarts via the entity registry. New interfaces (USB modem plugged in later, etc.) appear
    without an HA restart.
  • 30 s polling — piggybacks on the existing router_get_status call. Zero new HTTP requests, no gli4py changes.

Implementation

  • custom_components/glinet/wan.py — pure helpers + dataclasses (state mapping, friendly names, network-array parser). No HA imports — unit-testable in isolation.
  • custom_components/glinet/wan_sensor.pyWanStatusSensor entity class.
  • custom_components/glinet/router.py — stops discarding response["network"] from router_get_status (which was previously thrown away). Adds wan_status property, signal_wan_new /
    signal_wan_update dispatcher signals, and register_known_wan_interfaces() for platform setup.
  • custom_components/glinet/sensor.py — registers WanStatusSensor entities from the entity registry + any currently-up interfaces; subscribes to the new-WAN signal for live discovery.
  • custom_components/glinet/__init__.py — small refactor: moved from .router import GLinetRouter into async_setup_entry(). No production behavior change. Stops the package init from eagerly
    loading HA / gli4py, which lets unit tests import wan.py without those deps installed.

Tests

First unit tests in this repo (partially addresses the "Add tests" README TODO):

uv run pytest tests/ -v                                         
# 24 passed

Coverage:

  • State mapping for every (up, online) combination.
  • Friendly-name mapping for every documented raw name + modem disambiguation + raw passthrough fallback.
  • Network-array parsing: happy path, link-up-no-internet, non-list garbage, non-dict entries, missing-name skip, missing-fields default-and-warn, non-bool coercion.

Notes for review

  • No manifest.json version bump — assumed that's done at release time. Let me know if you'd prefer it bumped here.
  • The __init__.py lazy-import refactor has independent value (test isolation, faster cold-start), but I'm happy to revert it if you'd rather keep that file unchanged.

Preview in HA:

Notice the Primary WAN and Secondary WAN with connected values in my setup

Screenshot 2026-04-28 at 21 54 29

@akhoury

akhoury commented Apr 28, 2026

Copy link
Copy Markdown
Author

just noticed this one... #104 might be a better idea, either works for me as long as I can tell how my Multi-wan is doing :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant