Esempio n. 1
0
async def test_google_entity_registry_sync(hass, mock_cloud_login,
                                           cloud_prefs):
    """Test Google config responds to entity registry."""
    config = CloudGoogleConfig(hass, GACTIONS_SCHEMA({}), "mock-user-id",
                               cloud_prefs, hass.data["cloud"])
    await config.async_initialize()
    await config.async_connect_agent_user("mock-user-id")

    with patch.object(
            config,
            "async_schedule_google_sync_all") as mock_sync, patch.object(
                ga_helpers, "SYNC_DELAY", 0):
        # Created entity
        hass.bus.async_fire(
            EVENT_ENTITY_REGISTRY_UPDATED,
            {
                "action": "create",
                "entity_id": "light.kitchen"
            },
        )
        await hass.async_block_till_done()

        assert len(mock_sync.mock_calls) == 1

        # Removed entity
        hass.bus.async_fire(
            EVENT_ENTITY_REGISTRY_UPDATED,
            {
                "action": "remove",
                "entity_id": "light.kitchen"
            },
        )
        await hass.async_block_till_done()

        assert len(mock_sync.mock_calls) == 2

        # Entity registry updated with relevant changes
        hass.bus.async_fire(
            EVENT_ENTITY_REGISTRY_UPDATED,
            {
                "action": "update",
                "entity_id": "light.kitchen",
                "changes": ["entity_id"],
            },
        )
        await hass.async_block_till_done()

        assert len(mock_sync.mock_calls) == 3

        # Entity registry updated with non-relevant changes
        hass.bus.async_fire(
            EVENT_ENTITY_REGISTRY_UPDATED,
            {
                "action": "update",
                "entity_id": "light.kitchen",
                "changes": ["icon"]
            },
        )
        await hass.async_block_till_done()

        assert len(mock_sync.mock_calls) == 3

        # When hass is not started yet we wait till started
        hass.state = CoreState.starting
        hass.bus.async_fire(
            EVENT_ENTITY_REGISTRY_UPDATED,
            {
                "action": "create",
                "entity_id": "light.kitchen"
            },
        )
        await hass.async_block_till_done()

        assert len(mock_sync.mock_calls) == 3

    with patch.object(config, "async_sync_entities_all") as mock_sync:
        hass.bus.async_fire(EVENT_HOMEASSISTANT_STARTED)
        await hass.async_block_till_done()
        assert len(mock_sync.mock_calls) == 1