Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion tests/test_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
from zha.application.platforms import PlatformEntity
from zha.application.platforms.binary_sensor import IASZone
from zha.application.platforms.light import Light
from zha.application.platforms.sensor import LQISensor, RSSISensor
from zha.application.platforms.sensor import Battery, LQISensor, RSSISensor
from zha.application.platforms.sensor.device_class import (
SensorDeviceClass,
SensorStateClass,
Expand Down Expand Up @@ -948,6 +948,31 @@ async def test_primary_entity_computation(
]


async def test_primary_entity_weight_0_not_elected(zha_gateway: Gateway) -> None:
"""Test a weight-0 entity is not elected primary, even as the sole candidate."""

# Without the `Basic` cluster, no LQI/RSSI entities are created, so the
# battery entity is the only entity of this device
zigpy_dev = create_mock_zigpy_device(
zha_gateway,
{
1: {
SIG_EP_INPUT: [general.PowerConfiguration.cluster_id],
SIG_EP_OUTPUT: [],
SIG_EP_TYPE: zigpy.profiles.zha.DeviceType.SIMPLE_SENSOR,
SIG_EP_PROFILE: zigpy.profiles.zha.PROFILE_ID,
}
},
)
Comment thread
TheJulianJES marked this conversation as resolved.
zha_device = await join_zigpy_device(zha_gateway, zigpy_dev)

battery = get_entity(zha_device, Platform.SENSOR, entity_type=Battery)
assert list(zha_device.platform_entities.values()) == [battery]

# The weight-0 battery entity is not elected as the primary entity
assert not battery.primary


async def test_quirks_v2_primary_entity(zha_gateway: Gateway) -> None:
"""Test quirks v2 primary entity."""
registry = DeviceRegistry()
Expand Down
10 changes: 7 additions & 3 deletions zha/zigbee/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -1625,9 +1625,13 @@ def _compute_primary_entity(self, entities: Sequence[PlatformEntity]) -> None:
# It should not be possible for there to be more than one
assert not explicitly_primary

# For weight matching, only consider non-counter entities and entities which are
# not explicitly marked as not primary
candidates = [e for e in entities if e.enabled and e._attr_primary is not False]
# For weight matching, only consider entities with a non-zero primary weight
# which are not explicitly marked as not primary
candidates = [
e
for e in entities
if e.enabled and e._attr_primary is not False and e.primary_weight > 0
]
Comment thread
TheJulianJES marked this conversation as resolved.
candidates.sort(reverse=True, key=lambda e: e.primary_weight)

if not candidates:
Expand Down
Loading