Skip to content

[tests] Add unit test coverage for web_port property#11811

Merged
bdraco merged 1 commit intodevfrom
web_port_cover
Nov 10, 2025
Merged

[tests] Add unit test coverage for web_port property#11811
bdraco merged 1 commit intodevfrom
web_port_cover

Conversation

@bdraco
Copy link
Copy Markdown
Member

@bdraco bdraco commented Nov 10, 2025

What does this implement/fix?

Adds unit test coverage for the CORE.web_port property to prevent future regressions that would break the Dashboard's VISIT button functionality.

Background

The Dashboard PR esphome/dashboard#826 fixes Dashboard Issue esphome/dashboard#766 by changing the VISIT button visibility logic from checking loaded_integrations to checking web_port:

# Before (incorrect):
if "web_server" in device.loaded_integrations:
    show_visit_button()

# After (correct):
if device.web_port is not None:
    show_visit_button()

The issue occurred because captive_portal auto-loads ota.web_server platform, causing "web_server" to appear in loaded_integrations even when no web UI is configured.

What This PR Does

This PR adds 4 unit tests in tests/unit_tests/test_core.py to ensure the web_port property behavior remains correct:

  1. test_web_port__none - Returns None when no web_server configured
  2. test_web_port__explicit_web_server_default_port - Returns 80 when web_server configured without port
  3. test_web_port__explicit_web_server_custom_port - Returns custom port when explicitly set
  4. test_web_port__ota_web_server_platform_only - Critical - Returns None when only ota.web_server platform is loaded (not full web_server component)

The critical test (#4) ensures that web_port stays None even when:

  • "web_server" appears in loaded_integrations (due to platform load)
  • "ota/web_server" appears in loaded_platforms
  • But CONF_WEB_SERVER is NOT in config (no web UI)

This guarantees the Dashboard fix works correctly.

Implementation Details

The web_port property (in esphome/core/__init__.py:652-662) correctly checks CONF_WEB_SERVER in self.config, which is the explicit user configuration, NOT loaded_integrations:

@property
def web_port(self) -> int | None:
    if self.config is None:
        raise ValueError("Config has not been loaded yet")

    if CONF_WEB_SERVER in self.config:  # ← Checks explicit config
        try:
            return self.config[CONF_WEB_SERVER][CONF_PORT]
        except KeyError:
            return 80

    return None

These tests ensure this behavior never changes accidentally.

Types of changes

  • Code quality improvements to existing code or addition of tests

Related issue or feature (if applicable):

Test Environment

Not applicable - these are unit tests that run in CI for all platforms.

Example entry for config.yaml:

Not applicable - no user-facing changes.

Checklist:

  • The code change is tested and works locally.
  • Tests have been added to verify that the new code works (under tests/ folder).

If user exposed functionality or configuration variables are added/changed:

  • Documentation added/updated in esphome-docs. (Not applicable - no user-facing changes)

@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented Nov 10, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 72.43%. Comparing base (2a16653) to head (92c268a).

Additional details and impacted files
@@            Coverage Diff             @@
##              dev   #11811      +/-   ##
==========================================
+ Coverage   72.38%   72.43%   +0.05%     
==========================================
  Files          53       53              
  Lines       11134    11134              
  Branches     1504     1504              
==========================================
+ Hits         8059     8065       +6     
+ Misses       2684     2677       -7     
- Partials      391      392       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@bdraco bdraco marked this pull request as ready for review November 10, 2025 15:07
Copilot AI review requested due to automatic review settings November 10, 2025 15:07
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds comprehensive unit test coverage for the web_port property in the EsphomeCore class. The tests validate the correct behavior of web port detection and specifically address Dashboard Issue #766 regarding the distinction between the OTA web_server platform and the full web_server component.

  • Added tests for web_port property covering multiple scenarios
  • Included critical test case for OTA web_server platform edge case
  • Comprehensive documentation explaining the Dashboard Issue #766 context

@bdraco bdraco merged commit f32b69b into dev Nov 10, 2025
39 checks passed
@bdraco
Copy link
Copy Markdown
Member Author

bdraco commented Nov 10, 2025

Thanks

@bdraco bdraco deleted the web_port_cover branch November 10, 2025 16:00
@github-actions github-actions bot locked and limited conversation to collaborators Nov 12, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants