Add MockDeviceListener to tuya tests

This commit is contained in:
epenet 2025-07-16 13:38:08 +00:00
parent 412035b970
commit 31a5c064f5
3 changed files with 47 additions and 4 deletions

View File

@ -2,11 +2,12 @@
from __future__ import annotations
from typing import Any
from unittest.mock import patch
from tuya_sharing import CustomerDevice
from homeassistant.components.tuya import ManagerCompat
from homeassistant.components.tuya import DeviceListener, ManagerCompat
from homeassistant.const import Platform
from homeassistant.core import HomeAssistant
@ -162,6 +163,29 @@ DEVICE_MOCKS = {
}
class MockDeviceListener(DeviceListener):
"""Mocked DeviceListener for testing."""
async def async_send_device_update(
self,
hass: HomeAssistant,
device: CustomerDevice,
updated_status_properties: dict[str, Any] | None = None,
) -> None:
"""Mock update device method."""
property_list: list[str] = []
if updated_status_properties:
for key, value in updated_status_properties.items():
if key not in device.status:
raise ValueError(
f"Property {key} not found in device status: {device.status}"
)
device.status[key] = value
property_list.append(key)
self.update_device(device, property_list)
await hass.async_block_till_done()
async def initialize_entry(
hass: HomeAssistant,
mock_manager: ManagerCompat,

View File

@ -21,6 +21,8 @@ from homeassistant.core import HomeAssistant
from homeassistant.helpers.json import json_dumps
from homeassistant.util import dt as dt_util
from . import MockDeviceListener
from tests.common import MockConfigEntry, async_load_json_object_fixture
@ -181,3 +183,13 @@ async def mock_device(hass: HomeAssistant, mock_device_code: str) -> CustomerDev
}
device.status = details["status"]
return device
@pytest.fixture
def mock_listener(
hass: HomeAssistant, mock_manager: ManagerCompat
) -> MockDeviceListener:
"""Create a DeviceListener for testing."""
listener = MockDeviceListener(hass, mock_manager)
mock_manager.add_device_listener(listener)
return listener

View File

@ -13,7 +13,7 @@ from homeassistant.const import Platform
from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er
from . import DEVICE_MOCKS, initialize_entry
from . import DEVICE_MOCKS, MockDeviceListener, initialize_entry
from tests.common import MockConfigEntry, snapshot_platform
@ -78,16 +78,23 @@ async def test_bitmap(
mock_manager: ManagerCompat,
mock_config_entry: MockConfigEntry,
mock_device: CustomerDevice,
mock_listener: MockDeviceListener,
fault_value: int,
tankfull: str,
defrost: str,
wet: str,
) -> None:
"""Test BITMAP fault sensor on cs_arete_two_12l_dehumidifier_air_purifier."""
mock_device.status["fault"] = fault_value
await initialize_entry(hass, mock_manager, mock_config_entry, mock_device)
assert hass.states.get("binary_sensor.dehumidifier_tank_full").state == "off"
assert hass.states.get("binary_sensor.dehumidifier_defrost").state == "off"
assert hass.states.get("binary_sensor.dehumidifier_wet").state == "off"
await mock_listener.async_send_device_update(
hass, mock_device, {"fault": fault_value}
)
assert hass.states.get("binary_sensor.dehumidifier_tank_full").state == tankfull
assert hass.states.get("binary_sensor.dehumidifier_defrost").state == defrost
assert hass.states.get("binary_sensor.dehumidifier_wet").state == wet