Skip to content

Commit

Permalink
Emit state change events for sensors not tied to an attribute (#60)
Browse files Browse the repository at this point in the history
  • Loading branch information
puddly authored Jul 5, 2024
1 parent 6794a64 commit 4fbc162
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
10 changes: 9 additions & 1 deletion tests/test_climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import asyncio
from collections.abc import Awaitable, Callable
import logging
from unittest.mock import AsyncMock, call, patch
from unittest.mock import AsyncMock, MagicMock, call, patch

import pytest
import zhaquirks.sinope.thermostat
Expand Down Expand Up @@ -39,6 +39,7 @@
SinopeHVACAction,
ThermostatHVACAction,
)
from zha.const import STATE_CHANGED
from zha.exceptions import ZHAException
from zha.zigbee.device import Device

Expand Down Expand Up @@ -313,6 +314,10 @@ async def test_climate_hvac_action_running_state(
device_climate_sinope, platform=Platform.SENSOR, entity_type=SinopeHVACAction
)

subscriber = MagicMock()
entity.on_event(STATE_CHANGED, subscriber)
sensor_entity.on_event(STATE_CHANGED, subscriber)

assert entity.state["hvac_action"] == "off"
assert sensor_entity.state["state"] == "off"

Expand Down Expand Up @@ -352,6 +357,9 @@ async def test_climate_hvac_action_running_state(
assert entity.state["hvac_action"] == "fan"
assert sensor_entity.state["state"] == "fan"

# Both entities are updated!
assert len(subscriber.mock_calls) == 2 * 6


@pytest.mark.looptime
async def test_sinope_time(
Expand Down
12 changes: 8 additions & 4 deletions zha/application/platforms/sensor/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,10 +253,14 @@ def handle_cluster_handler_attribute_updated(
event: ClusterAttributeUpdatedEvent, # pylint: disable=unused-argument
) -> None:
"""Handle attribute updates from the cluster handler."""
if event.attribute_name == self._attribute_name or (
hasattr(self, "_attr_extra_state_attribute_names")
and event.attribute_name
in getattr(self, "_attr_extra_state_attribute_names")
if (
event.attribute_name == self._attribute_name
or (
hasattr(self, "_attr_extra_state_attribute_names")
and event.attribute_name
in getattr(self, "_attr_extra_state_attribute_names")
)
or self._attribute_name is None
):
self.maybe_emit_state_changed_event()

Expand Down

0 comments on commit 4fbc162

Please sign in to comment.