Skip to content

fix(config_flow): inject the HA websession into OWNd discovery and correct the OWNd licence - #757

Merged
GreenGrassBlueOcean merged 2 commits into
v2-phase1-architecturefrom
fix/ownd-packaging-and-websession-alignment
Oct 21, 2026
Merged

GreenGrassBlueOcean merged 2 commits into
v2-phase1-architecturefrom
fix/ownd-packaging-and-websession-alignment

Conversation

@GreenGrassBlueOcean

@GreenGrassBlueOcean GreenGrassBlueOcean commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

Rebuilt on v2-phase1-architecture after #765. The strict-typing half of the earlier version of this PR (installing OWNd in strict-typing.yml, dropping the OWNd.* mypy override, the 11 boundary fixes) landed in #765 against the PyPI OWNd==2.0.0b11 pin, and the tests/test_connection.py patch targets landed in #758. The OWNd pin stays at OWNd==2.0.0b11. What remains is the websession injection and the licence fix.


1. What is the problem?

  1. OWNd discovery opens its own HTTP sessions (custom_components/myhome/config_flow.py, quality_scale.yaml):
    • OWNd.discovery.find_gateways() and get_gateway() fetch the gateway's UPnP/SOAP description XML over HTTP (SSDP LOCATION, port 49153 on most gateways). Both accept session: aiohttp.ClientSession | None = None.
    • MyhomeFlowHandler.async_step_user and async_step_custom called them without a session, so OWNd created and closed a private ClientSession on every discovery, outside Home Assistant's managed connection pool.
    • quality_scale.yaml marked inject-websession as exempt ("No HTTP is used"), which is not true for discovery.
  2. Wrong licence for OWNd (quality_scale.yaml):
    • dependency-transparency described OWNd as "MIT/GPL licensed". OWNd is Apache-2.0 (its LICENSE and the license = "Apache-2.0" metadata on the 2.0.0b11 PyPI release).

2. How is it solved?

  1. Inject the Home Assistant websession (config_flow.py):
    • find_gateways(session=async_get_clientsession(self.hass)) in async_step_user.
    • get_gateway(user_input["address"], session=async_get_clientsession(self.hass)) in async_step_custom.
    • The optional UPnP lookup in async_step_custom still falls back to manual MAC entry on any failure, but no longer silently. The redundant except (asyncio.TimeoutError, Exception) becomes except Exception as err, which logs UPnP lookup of <address> failed, asking for the MAC: <err> at debug level. The clause itself dates from 2020 (ce7a342d); it stays broad on purpose, because a timeout, an HTTP error or malformed XML must all lead to the manual step rather than a broken config flow.
  2. Quality scale (quality_scale.yaml, docs/quality_scale_plan.md):
    • inject-websession: exempt → done, with a comment that names the two calls and says the bus itself stays on raw TCP.
    • dependency-transparency: "MIT/GPL" → "Apache-2.0".
    • The Platinum note in docs/quality_scale_plan.md now matches. The stale "504 errors remain in 13 modules" line is replaced with the actual state: all 71 modules clean, and mypy_baseline.json at "total": 0.
  3. Tests (tests/test_config_flow.py):
    • test_config_flow_injects_ha_websession_into_discovery runs the user step and then the custom step, and asserts that both find_gateways and get_gateway are awaited with session=async_get_clientsession(hass). Both are patched with an explicit new_callable=AsyncMock.
    • test_custom_step_validation_and_timeout also asserts the new debug line for a get_gateway timeout.

3. State Diagram

sequenceDiagram
    participant User as Home Assistant User
    participant CF as MyhomeFlowHandler (config_flow.py)
    participant HAHttp as async_get_clientsession(hass)
    participant OWNd as OWNd.discovery (find_gateways / get_gateway)
    participant GW as MyHOME Gateway (UPnP / SOAP XML)

    User->>CF: Start config flow (step_user / step_custom)
    CF->>HAHttp: async_get_clientsession(self.hass)
    HAHttp-->>CF: Shared aiohttp.ClientSession
    CF->>OWNd: find_gateways(session=…) / get_gateway(addr, session=…)
    OWNd->>GW: HTTP GET UPnP description XML (shared HA session)
    GW-->>OWNd: Device / service XML (model, serial, port)
    OWNd-->>CF: Discovered gateway metadata
Loading

4. Where are the traces (real ones)?


5. What does the Firmware Oracle / Encyclopedia say?

  • Gateways with a UPnP/SSDP responder (F454, F455, MH202, MH201, F452, F453AV on port 49153; MyHomeServer1, MH200N, MH200, H4890, HL4684 via SSDP) serve their description XML over HTTP during find_gateways() / get_gateway(). This is the traffic that now uses Home Assistant's session.
  • Gateways without UPnP (F461 020011, Legrand 3578) skip HTTP discovery and identify in-band via WHO 13 (*#13**15##) and WHO 1013 (*#1013*0*1##) over the OpenWebNet session, which this PR does not touch.

Verification

  • Full suite on v2-phase1-architecture at 3692dd11 (fix(typing): check mypy --strict against the pinned OWNd 2.0.0b11 types #765 merged) against the released OWNd==2.0.0b11 wheel with --cov=custom_components/myhome: 4359 passed, 5 snapshots passed, TOTAL 14461 statements, 0 missed, 100.0%.

  • scripts/typing_ratchet.py with OWNd b11 installed: 0 error(s) in 0 module(s); baseline 0.

  • ruff check custom_components tests scripts, scripts/verify_ha_standards.py, scripts/sync_documentation.py --check: all pass. scripts/quality_scale_report.py lists inject-websession as ✅ done.

  • V2 Architecture: New entities implement handle_event() and do not manually subscribe via async_dispatcher_connect. (No entities are added or changed.)

🤖 Generated with Claude Code

@codecov-commenter

Copy link
Copy Markdown

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

…rrect the OWNd licence

OWNd.discovery.find_gateways() and get_gateway() fetch the gateway's
UPnP/SOAP description over HTTP and accept an optional aiohttp session.
The config flow called them without one, so OWNd opened its own
ClientSession per call. Pass async_get_clientsession(self.hass) and
mark inject-websession done in quality_scale.yaml.

quality_scale.yaml also described OWNd as MIT/GPL licensed; it is
Apache-2.0 (OWNd LICENSE and pyproject.toml).

Rebuilt on v2-phase1-architecture: the typing changes this branch
carried before are in #765, and the OWNd pin stays at 2.0.0b11.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@GreenGrassBlueOcean
GreenGrassBlueOcean force-pushed the fix/ownd-packaging-and-websession-alignment branch from cb640b1 to 00fcf09 Compare October 21, 2026 11:33
@GreenGrassBlueOcean GreenGrassBlueOcean changed the title fix: inject HA websession into OWNd discovery, align packaging and strict typing fix(config_flow): inject the HA websession into OWNd discovery and correct the OWNd licence Oct 21, 2026
…he typing docs

Review follow-ups on #757:
- async_step_custom still falls back to manual MAC entry on any UPnP
  lookup failure, but logs it at debug level; the redundant
  (asyncio.TimeoutError, Exception) tuple becomes Exception.
- The websession test patches find_gateways/get_gateway with an
  explicit new_callable=AsyncMock, and the timeout test asserts the
  new debug line.
- docs/quality_scale_plan.md: replace the stale "504 errors remain in
  13 modules" with the actual state (71 modules clean, baseline 0).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@GreenGrassBlueOcean
GreenGrassBlueOcean merged commit c16e1ca into v2-phase1-architecture Oct 21, 2026
17 checks passed
@GreenGrassBlueOcean
GreenGrassBlueOcean deleted the fix/ownd-packaging-and-websession-alignment branch October 21, 2026 15:03
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.

2 participants