"""Test Tuya initialization.""" from __future__ import annotations from syrupy.assertion import SnapshotAssertion from tuya_sharing import CustomerDevice, Manager from homeassistant.components.tuya.const import DOMAIN from homeassistant.components.tuya.diagnostics import _REDACTED_DPCODES from homeassistant.core import HomeAssistant from homeassistant.helpers import device_registry as dr, entity_registry as er from . import DEVICE_MOCKS, initialize_entry from tests.common import MockConfigEntry, async_load_json_object_fixture async def test_device_registry( hass: HomeAssistant, mock_manager: Manager, mock_config_entry: MockConfigEntry, mock_devices: CustomerDevice, device_registry: dr.DeviceRegistry, entity_registry: er.EntityRegistry, snapshot: SnapshotAssertion, ) -> None: """Validate device registry snapshots for all devices, including unsupported ones.""" await initialize_entry(hass, mock_manager, mock_config_entry, mock_devices) device_registry_entries = dr.async_entries_for_config_entry( device_registry, mock_config_entry.entry_id ) # Ensure the device registry contains same amount as DEVICE_MOCKS assert len(device_registry_entries) == len(DEVICE_MOCKS) for device_registry_entry in device_registry_entries: assert device_registry_entry == snapshot( name=list(device_registry_entry.identifiers)[0][1] ) # Ensure model is suffixed with "(unsupported)" when no entities are generated assert (" (unsupported)" in device_registry_entry.model) == ( not er.async_entries_for_device( entity_registry, device_registry_entry.id, include_disabled_entities=True, ) ) async def test_fixtures_valid(hass: HomeAssistant) -> None: """Ensure Tuya fixture files are valid.""" # We want to ensure that the fixture files do not contain # `home_assistant`, `id`, or `terminal_id` keys. # These are provided by the Tuya diagnostics and should be removed # from the fixture. EXCLUDE_KEYS = ("home_assistant", "id", "terminal_id") for device_code in DEVICE_MOCKS: details = await async_load_json_object_fixture( hass, f"{device_code}.json", DOMAIN ) for key in EXCLUDE_KEYS: assert key not in details, ( f"Please remove data[`'{key}']` from {device_code}.json" ) if "status" in details: statuses = details["status"] for key in statuses: if key in _REDACTED_DPCODES: assert statuses[key] == "**REDACTED**", ( f"Please mark `data['status']['{key}']` as `**REDACTED**`" f" in {device_code}.json" )