Repository: home-assistant/core
File: tests/components/usb/test_init.py (Python)

Code before:
            pid="3039",
            serial_number=slae_sh_device.serial_number,
            manufacturer=slae_sh_device.manufacturer,
            description=slae_sh_device.description,
        )
    ]

    aiousbwatcher_callback = None

    def async_register_callback(callback):
        nonlocal aiousbwatcher_callback
        aiousbwatcher_callback = callback

    MockAIOUSBWatcher = MagicMock()
    MockAIOUSBWatcher.async_register_callback = async_register_callback

    with (
        patch("sys.platform", "linux"),
        patch("homeassistant.components.usb.async_get_usb", return_value=new_usb),
        patch_scanned_serial_ports(return_value=mock_ports),
        patch(
            "homeassistant.components.usb.AIOUSBWatcher", return_value=MockAIOUSBWatcher
        ),
        patch.object(hass.config_entries.flow, "async_init") as mock_config_flow,
    ):
        assert await async_setup_component(hass, DOMAIN, {DOMAIN: {}})
        await hass.async_block_till_done()
        hass.bus.async_fire(EVENT_HOMEASSISTANT_STARTED)
        await hass.async_block_till_done()

        assert aiousbwatcher_callback is not None

        assert len(mock_config_flow.mock_calls) == 1
        assert mock_config_flow.mock_calls[0][1][0] == "test1"
        await hass.async_block_till_done()
        assert len(mock_config_flow.mock_calls) == 1

        mock_ports.append(
            USBDevice(
                device=slae_sh_device.device,
                vid="0FA0",
                pid="0FA0",
                serial_number=slae_sh_device.serial_number,
                manufacturer=slae_sh_device.manufacturer,
                description=slae_sh_device.description,
            )
        )

        aiousbwatcher_callback()
        await hass.async_block_till_done()


Review comment (suggestion):
Is `{DOMAIN: {}}` correct here? The two strings do match but `usb` in the config seems like it could be independent from the domain, no? In the ZBT-2 test above, it is set up via `assert await async_setup_component(hass, USB_DOMAIN, {"usb": {}})`.

Code after:
            pid="3039",
            serial_number=slae_sh_device.serial_number,
            manufacturer=slae_sh_device.manufacturer,
            description=slae_sh_device.description,
        )
    ]

    aiousbwatcher_callback = None

    def async_register_callback(callback):
        nonlocal aiousbwatcher_callback
        aiousbwatcher_callback = callback

    MockAIOUSBWatcher = MagicMock()
    MockAIOUSBWatcher.async_register_callback = async_register_callback

    with (
        patch("sys.platform", "linux"),
        patch("homeassistant.components.usb.async_get_usb", return_value=new_usb),
        patch_scanned_serial_ports(return_value=mock_ports),
        patch(
            "homeassistant.components.usb.AIOUSBWatcher", return_value=MockAIOUSBWatcher
        ),
        patch.object(hass.config_entries.flow, "async_init") as mock_config_flow,
    ):
        assert await async_setup_component(hass, DOMAIN, {"usb": {}})
        await hass.async_block_till_done()
        hass.bus.async_fire(EVENT_HOMEASSISTANT_STARTED)
        await hass.async_block_till_done()

        assert aiousbwatcher_callback is not None

        assert len(mock_config_flow.mock_calls) == 1
        assert mock_config_flow.mock_calls[0][1][0] == "test1"
        await hass.async_block_till_done()
        assert len(mock_config_flow.mock_calls) == 1

        mock_ports.append(
            USBDevice(
                device=slae_sh_device.device,
                vid="0FA0",
                pid="0FA0",
                serial_number=slae_sh_device.serial_number,
                manufacturer=slae_sh_device.manufacturer,
                description=slae_sh_device.description,
            )
        )

        aiousbwatcher_callback()
        await hass.async_block_till_done()