1
0
mirror of https://github.com/home-assistant/core.git synced 2026-05-08 17:49:37 +01:00

Add test for Tuya event (#163812)

This commit is contained in:
epenet
2026-02-23 08:28:23 +01:00
committed by GitHub
parent b9b45c9994
commit 463003fc33
+59 -1
View File
@@ -5,11 +5,12 @@ from __future__ import annotations
import base64
from unittest.mock import patch
from freezegun.api import FrozenDateTimeFactory
import pytest
from syrupy.assertion import SnapshotAssertion
from tuya_sharing import CustomerDevice, Manager
from homeassistant.const import Platform
from homeassistant.const import STATE_UNAVAILABLE, STATE_UNKNOWN, Platform
from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er
@@ -83,3 +84,60 @@ async def test_alarm_message_event(
state = hass.states.get(entity_id)
assert state.attributes == snapshot
assert state.attributes["message"]
@pytest.mark.parametrize(
"mock_device_code",
["wxkg_l8yaz4um5b3pwyvf"],
)
@patch("homeassistant.components.tuya.PLATFORMS", [Platform.EVENT])
@pytest.mark.freeze_time("2024-01-01")
async def test_selective_state_update(
hass: HomeAssistant,
mock_manager: Manager,
mock_config_entry: MockConfigEntry,
mock_device: CustomerDevice,
mock_listener: MockDeviceListener,
freezer: FrozenDateTimeFactory,
) -> None:
"""Ensure event is only triggered when device reports actual data."""
entity_id = "event.bathroom_smart_switch_button_1"
await initialize_entry(hass, mock_manager, mock_config_entry, mock_device)
# Initial state is unknown
assert hass.states.get(entity_id).state == STATE_UNKNOWN
# Device receives a data update - event gets triggered and state gets updated
freezer.tick(10)
await mock_listener.async_send_device_update(
hass, mock_device, {"switch_mode1": "click"}
)
assert hass.states.get(entity_id).state == "2024-01-01T00:00:10.000+00:00"
# Device goes offline
freezer.tick(10)
mock_device.online = False
await mock_listener.async_send_device_update(hass, mock_device, None)
assert hass.states.get(entity_id).state == STATE_UNAVAILABLE
# Device comes back online - state should go back to last known value,
# not new datetime since no new data update has come in
freezer.tick(10)
mock_device.online = True
await mock_listener.async_send_device_update(hass, mock_device, None)
assert hass.states.get(entity_id).state == "2024-01-01T00:00:10.000+00:00"
# Device receives a new data update - event gets triggered and state gets updated
freezer.tick(10)
await mock_listener.async_send_device_update(
hass, mock_device, {"switch_mode1": "click"}
)
assert hass.states.get(entity_id).state == "2024-01-01T00:00:40.000+00:00"
# Device receives a data update on a different datapoint - event doesn't
# get triggered and state doesn't get updated
freezer.tick(10)
await mock_listener.async_send_device_update(
hass, mock_device, {"switch_mode2": "click"}
)
assert hass.states.get(entity_id).state == "2024-01-01T00:00:40.000+00:00"