Skip to content

Test broad except when entity creation fails during discovery#822

Draft
TheJulianJES wants to merge 1 commit into
devfrom
zigpy-bot/test-discovery-entity-creation-except
Draft

Test broad except when entity creation fails during discovery#822
TheJulianJES wants to merge 1 commit into
devfrom
zigpy-bot/test-discovery-entity-creation-except

Conversation

@TheJulianJES

Copy link
Copy Markdown
Contributor

Addresses comment in:

What

Adds a test for the broad except in discover_entities_for_endpoint that skips (and logs) an entity whose instantiation raises, instead of aborting discovery for the rest of the endpoint.

Why

discover_entities_for_endpoint wraps each entity instantiation in a broad except (discovery.py:355-357, logging "Failed to create %s entity"). This path used to be exercised incidentally: BinarySensor.__init__ called self._state = self.is_on, which could raise and trip the except. #821 removed that dead _state assignment, so the except was no longer covered by the suite (entities that would fail are now filtered out earlier via _is_supported).

The test creates an on/off switch device, patches Switch.__init__ to raise, joins the device, and asserts the failure is logged and the switch entity is skipped while the device still joins.

`discover_entities_for_endpoint` wraps each entity instantiation in a
broad `except` so one broken entity is skipped (and logged) rather than
aborting discovery for the whole endpoint. This path was previously only
exercised incidentally; #821 removed the `BinarySensor._state = self.is_on`
init call that used to trip it, leaving discovery.py:355-357 uncovered.

Add an explicit test: patch `Switch.__init__` to raise, join the device,
and assert the failure is logged and the switch entity skipped while the
device still joins.
Comment thread tests/test_discover.py
Comment on lines +1007 to +1020
zigpy_device = create_mock_zigpy_device(
zha_gateway,
{
1: {
SIG_EP_INPUT: [
zigpy.zcl.clusters.general.Basic.cluster_id,
zigpy.zcl.clusters.general.OnOff.cluster_id,
],
SIG_EP_OUTPUT: [],
SIG_EP_TYPE: zigpy.profiles.zha.DeviceType.ON_OFF_SWITCH,
SIG_EP_PROFILE: zigpy.profiles.zha.PROFILE_ID,
}
},
)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

We instead want to create zigpy device from diagnostics files, right?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I personally just find it to be a simpler pattern to read, since the diagnostics JSON contains more data to fully initialize a ZHA device. If you think manually creating a device is easier, let's do it.

@codecov

codecov Bot commented Jul 9, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 97.30%. Comparing base (c38e03e) to head (7ae2fce).

Additional details and impacted files
@@            Coverage Diff             @@
##              dev     #822      +/-   ##
==========================================
+ Coverage   97.27%   97.30%   +0.02%     
==========================================
  Files          55       55              
  Lines       10930    10930              
==========================================
+ Hits        10632    10635       +3     
+ Misses        298      295       -3     

☔ View full report in Codecov by Harness.
📢 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.

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.

3 participants