Exemplo n.º 1
0
async def test_unavailable_state_doesnt_sync(hass):
    """Test that an unavailable entity does not sync over."""
    light = DemoLight(
        None,
        'Demo Light',
        state=False,
    )
    light.hass = hass
    light.entity_id = 'light.demo_light'
    light._available = False
    await light.async_update_ha_state()

    result = await sh.async_handle_message(
        hass, BASIC_CONFIG, {
            "requestId": REQ_ID,
            "inputs": [{
                "intent": "action.devices.SYNC"
            }]
        })

    assert result == {
        'requestId': REQ_ID,
        'payload': {
            'agentUserId': 'test-agent',
            'devices': []
        }
    }
async def test_unavailable_state_doesnt_sync(hass):
    """Test that an unavailable entity does not sync over."""
    light = DemoLight(
        None, 'Demo Light',
        state=False,
    )
    light.hass = hass
    light.entity_id = 'light.demo_light'
    light._available = False
    await light.async_update_ha_state()

    result = await sh.async_handle_message(hass, BASIC_CONFIG, {
        "requestId": REQ_ID,
        "inputs": [{
            "intent": "action.devices.SYNC"
        }]
    })

    assert result == {
        'requestId': REQ_ID,
        'payload': {
            'agentUserId': 'test-agent',
            'devices': []
        }
    }
Exemplo n.º 3
0
async def test_query_message(hass):
    """Test a sync message."""
    light = DemoLight(None, 'Demo Light', state=False, rgb=[237, 224, 33])
    light.hass = hass
    light.entity_id = 'light.demo_light'
    await light.async_update_ha_state()

    light2 = DemoLight(
        None,
        'Another Light',
        state=True,
        rgb=[237, 224, 33],
        ct=400,
        brightness=78,
    )
    light2.hass = hass
    light2.entity_id = 'light.another_light'
    await light2.async_update_ha_state()

    result = await sh.async_handle_message(
        hass, BASIC_CONFIG, {
            "requestId":
            REQ_ID,
            "inputs": [{
                "intent": "action.devices.QUERY",
                "payload": {
                    "devices": [{
                        "id": "light.demo_light",
                    }, {
                        "id": "light.another_light",
                    }, {
                        "id": "light.non_existing",
                    }]
                }
            }]
        })

    assert result == {
        'requestId': REQ_ID,
        'payload': {
            'devices': {
                'light.non_existing': {
                    'online': False,
                },
                'light.demo_light': {
                    'on': False,
                    'online': True,
                },
                'light.another_light': {
                    'on': True,
                    'online': True,
                    'brightness': 30,
                    'color': {
                        'spectrumRGB': 15589409,
                        'temperature': 2500,
                    }
                },
            }
        }
    }
Exemplo n.º 4
0
async def test_sync_message(hass):
    """Test a sync message."""
    light = DemoLight(None, 'Demo Light', state=False, rgb=[237, 224, 33])
    light.hass = hass
    light.entity_id = 'light.demo_light'
    await light.async_update_ha_state()

    # This should not show up in the sync request
    hass.states.async_set('sensor.no_match', 'something')

    # Excluded via config
    hass.states.async_set('light.not_expose', 'on')

    config = helpers.Config(
        should_expose=lambda state: state.entity_id != 'light.not_expose',
        agent_user_id='test-agent',
        entity_config={
            'light.demo_light': {
                const.CONF_ROOM_HINT: 'Living Room',
                const.CONF_ALIASES: ['Hello', 'World']
            }
        })

    result = await sh.async_handle_message(
        hass, config, {
            "requestId": REQ_ID,
            "inputs": [{
                "intent": "action.devices.SYNC"
            }]
        })

    assert result == {
        'requestId': REQ_ID,
        'payload': {
            'agentUserId':
            'test-agent',
            'devices': [{
                'id':
                'light.demo_light',
                'name': {
                    'name': 'Demo Light',
                    'nicknames': [
                        'Hello',
                        'World',
                    ]
                },
                'traits': [
                    trait.TRAIT_BRIGHTNESS,
                    trait.TRAIT_ONOFF,
                    trait.TRAIT_COLOR_SPECTRUM,
                    trait.TRAIT_COLOR_TEMP,
                ],
                'type':
                sh.TYPE_LIGHT,
                'willReportState':
                False,
                'attributes': {
                    'colorModel': 'rgb',
                    'temperatureMinK': 6493,
                    'temperatureMaxK': 2000,
                },
                'roomHint':
                'Living Room'
            }]
        }
    }
Exemplo n.º 5
0
async def test_query_message(hass):
    """Test a sync message."""
    light = DemoLight(
        None,
        'Demo Light',
        state=False,
        hs_color=(180, 75),
    )
    light.hass = hass
    light.entity_id = 'light.demo_light'
    await light.async_update_ha_state()

    light2 = DemoLight(
        None,
        'Another Light',
        state=True,
        hs_color=(180, 75),
        ct=400,
        brightness=78,
    )
    light2.hass = hass
    light2.entity_id = 'light.another_light'
    await light2.async_update_ha_state()

    events = []
    hass.bus.async_listen(EVENT_QUERY_RECEIVED, events.append)

    result = await sh.async_handle_message(
        hass, BASIC_CONFIG, {
            "requestId":
            REQ_ID,
            "inputs": [{
                "intent": "action.devices.QUERY",
                "payload": {
                    "devices": [{
                        "id": "light.demo_light",
                    }, {
                        "id": "light.another_light",
                    }, {
                        "id": "light.non_existing",
                    }]
                }
            }]
        })

    assert result == {
        'requestId': REQ_ID,
        'payload': {
            'devices': {
                'light.non_existing': {
                    'online': False,
                },
                'light.demo_light': {
                    'on': False,
                    'online': True,
                },
                'light.another_light': {
                    'on': True,
                    'online': True,
                    'brightness': 30,
                    'color': {
                        'spectrumRGB': 4194303,
                        'temperature': 2500,
                    }
                },
            }
        }
    }

    assert len(events) == 3
    assert events[0].event_type == EVENT_QUERY_RECEIVED
    assert events[0].data == {
        'request_id': REQ_ID,
        'entity_id': 'light.demo_light'
    }
    assert events[1].event_type == EVENT_QUERY_RECEIVED
    assert events[1].data == {
        'request_id': REQ_ID,
        'entity_id': 'light.another_light'
    }
    assert events[2].event_type == EVENT_QUERY_RECEIVED
    assert events[2].data == {
        'request_id': REQ_ID,
        'entity_id': 'light.non_existing'
    }
Exemplo n.º 6
0
async def test_sync_message(hass):
    """Test a sync message."""
    light = DemoLight(
        None,
        'Demo Light',
        state=False,
        hs_color=(180, 75),
    )
    light.hass = hass
    light.entity_id = 'light.demo_light'
    await light.async_update_ha_state()

    # This should not show up in the sync request
    hass.states.async_set('sensor.no_match', 'something')

    # Excluded via config
    hass.states.async_set('light.not_expose', 'on')

    config = helpers.Config(
        should_expose=lambda state: state.entity_id != 'light.not_expose',
        allow_unlock=False,
        entity_config={
            'light.demo_light': {
                const.CONF_ROOM_HINT: 'Living Room',
                const.CONF_ALIASES: ['Hello', 'World']
            }
        })

    events = []
    hass.bus.async_listen(EVENT_SYNC_RECEIVED, events.append)

    result = await sh.async_handle_message(
        hass, config, 'test-agent', {
            "requestId": REQ_ID,
            "inputs": [{
                "intent": "action.devices.SYNC"
            }]
        })

    assert result == {
        'requestId': REQ_ID,
        'payload': {
            'agentUserId':
            'test-agent',
            'devices': [{
                'id':
                'light.demo_light',
                'name': {
                    'name': 'Demo Light',
                    'nicknames': [
                        'Hello',
                        'World',
                    ]
                },
                'traits': [
                    trait.TRAIT_BRIGHTNESS,
                    trait.TRAIT_ONOFF,
                    trait.TRAIT_COLOR_SPECTRUM,
                    trait.TRAIT_COLOR_TEMP,
                ],
                'type':
                sh.TYPE_LIGHT,
                'willReportState':
                False,
                'attributes': {
                    'colorModel': 'rgb',
                    'temperatureMinK': 2000,
                    'temperatureMaxK': 6535,
                },
                'roomHint':
                'Living Room'
            }]
        }
    }
    await hass.async_block_till_done()

    assert len(events) == 1
    assert events[0].event_type == EVENT_SYNC_RECEIVED
    assert events[0].data == {
        'request_id': REQ_ID,
    }
Exemplo n.º 7
0
async def test_sync_in_area(hass, registries):
    """Test a sync message where room hint comes from area."""
    area = registries.area.async_create("Living Room")

    device = registries.device.async_get_or_create(
        config_entry_id='1234',
        connections={(device_registry.CONNECTION_NETWORK_MAC,
                      '12:34:56:AB:CD:EF')})
    registries.device.async_update_device(device.id, area_id=area.id)

    entity = registries.entity.async_get_or_create(
        'light',
        'test',
        '1235',
        suggested_object_id='demo_light',
        device_id=device.id)

    light = DemoLight(
        None,
        'Demo Light',
        state=False,
        hs_color=(180, 75),
    )
    light.hass = hass
    light.entity_id = entity.entity_id
    await light.async_update_ha_state()

    config = helpers.Config(should_expose=lambda _: True,
                            allow_unlock=False,
                            entity_config={})

    events = []
    hass.bus.async_listen(EVENT_SYNC_RECEIVED, events.append)

    result = await sh.async_handle_message(
        hass, config, 'test-agent', {
            "requestId": REQ_ID,
            "inputs": [{
                "intent": "action.devices.SYNC"
            }]
        })

    assert result == {
        'requestId': REQ_ID,
        'payload': {
            'agentUserId':
            'test-agent',
            'devices': [{
                'id':
                'light.demo_light',
                'name': {
                    'name': 'Demo Light'
                },
                'traits': [
                    trait.TRAIT_BRIGHTNESS,
                    trait.TRAIT_ONOFF,
                    trait.TRAIT_COLOR_SPECTRUM,
                    trait.TRAIT_COLOR_TEMP,
                ],
                'type':
                sh.TYPE_LIGHT,
                'willReportState':
                False,
                'attributes': {
                    'colorModel': 'rgb',
                    'temperatureMinK': 2000,
                    'temperatureMaxK': 6535,
                },
                'roomHint':
                'Living Room'
            }]
        }
    }
    await hass.async_block_till_done()

    assert len(events) == 1
    assert events[0].event_type == EVENT_SYNC_RECEIVED
    assert events[0].data == {
        'request_id': REQ_ID,
    }
async def test_query_message(hass):
    """Test a sync message."""
    light = DemoLight(
        None, 'Demo Light',
        state=False,
        hs_color=(180, 75),
    )
    light.hass = hass
    light.entity_id = 'light.demo_light'
    await light.async_update_ha_state()

    light2 = DemoLight(
        None, 'Another Light',
        state=True,
        hs_color=(180, 75),
        ct=400,
        brightness=78,
    )
    light2.hass = hass
    light2.entity_id = 'light.another_light'
    await light2.async_update_ha_state()

    result = await sh.async_handle_message(hass, BASIC_CONFIG, {
        "requestId": REQ_ID,
        "inputs": [{
            "intent": "action.devices.QUERY",
            "payload": {
                "devices": [{
                    "id": "light.demo_light",
                }, {
                    "id": "light.another_light",
                }, {
                    "id": "light.non_existing",
                }]
            }
        }]
    })

    assert result == {
        'requestId': REQ_ID,
        'payload': {
            'devices': {
                'light.non_existing': {
                    'online': False,
                },
                'light.demo_light': {
                    'on': False,
                    'online': True,
                },
                'light.another_light': {
                    'on': True,
                    'online': True,
                    'brightness': 30,
                    'color': {
                        'spectrumRGB': 4194303,
                        'temperature': 2500,
                    }
                },
            }
        }
    }
async def test_sync_message(hass):
    """Test a sync message."""
    light = DemoLight(
        None, 'Demo Light',
        state=False,
        hs_color=(180, 75),
    )
    light.hass = hass
    light.entity_id = 'light.demo_light'
    await light.async_update_ha_state()

    # This should not show up in the sync request
    hass.states.async_set('sensor.no_match', 'something')

    # Excluded via config
    hass.states.async_set('light.not_expose', 'on')

    config = helpers.Config(
        should_expose=lambda state: state.entity_id != 'light.not_expose',
        agent_user_id='test-agent',
        entity_config={
            'light.demo_light': {
                const.CONF_ROOM_HINT: 'Living Room',
                const.CONF_ALIASES: ['Hello', 'World']
            }
        }
    )

    result = await sh.async_handle_message(hass, config, {
        "requestId": REQ_ID,
        "inputs": [{
            "intent": "action.devices.SYNC"
        }]
    })

    assert result == {
        'requestId': REQ_ID,
        'payload': {
            'agentUserId': 'test-agent',
            'devices': [{
                'id': 'light.demo_light',
                'name': {
                    'name': 'Demo Light',
                    'nicknames': [
                        'Hello',
                        'World',
                    ]
                },
                'traits': [
                    trait.TRAIT_BRIGHTNESS,
                    trait.TRAIT_ONOFF,
                    trait.TRAIT_COLOR_SPECTRUM,
                    trait.TRAIT_COLOR_TEMP,
                ],
                'type': sh.TYPE_LIGHT,
                'willReportState': False,
                'attributes': {
                    'colorModel': 'rgb',
                    'temperatureMinK': 2000,
                    'temperatureMaxK': 6535,
                },
                'roomHint': 'Living Room'
            }]
        }
    }
Exemplo n.º 10
0
async def test_query_message(hass):
    """Test a sync message."""
    light = DemoLight(
        None, 'Demo Light',
        state=False,
        hs_color=(180, 75),
    )
    light.hass = hass
    light.entity_id = 'light.demo_light'
    await light.async_update_ha_state()

    light2 = DemoLight(
        None, 'Another Light',
        state=True,
        hs_color=(180, 75),
        ct=400,
        brightness=78,
    )
    light2.hass = hass
    light2.entity_id = 'light.another_light'
    await light2.async_update_ha_state()

    events = []
    hass.bus.async_listen(EVENT_QUERY_RECEIVED, events.append)

    result = await sh.async_handle_message(
        hass, BASIC_CONFIG, 'test-agent',
        {
            "requestId": REQ_ID,
            "inputs": [{
                "intent": "action.devices.QUERY",
                "payload": {
                    "devices": [{
                        "id": "light.demo_light",
                    }, {
                        "id": "light.another_light",
                    }, {
                        "id": "light.non_existing",
                    }]
                }
            }]
        })

    assert result == {
        'requestId': REQ_ID,
        'payload': {
            'devices': {
                'light.non_existing': {
                    'online': False,
                },
                'light.demo_light': {
                    'on': False,
                    'online': True,
                },
                'light.another_light': {
                    'on': True,
                    'online': True,
                    'brightness': 30,
                    'color': {
                        'spectrumRGB': 4194303,
                        'temperature': 2500,
                    }
                },
            }
        }
    }

    assert len(events) == 3
    assert events[0].event_type == EVENT_QUERY_RECEIVED
    assert events[0].data == {
        'request_id': REQ_ID,
        'entity_id': 'light.demo_light'
    }
    assert events[1].event_type == EVENT_QUERY_RECEIVED
    assert events[1].data == {
        'request_id': REQ_ID,
        'entity_id': 'light.another_light'
    }
    assert events[2].event_type == EVENT_QUERY_RECEIVED
    assert events[2].data == {
        'request_id': REQ_ID,
        'entity_id': 'light.non_existing'
    }
Exemplo n.º 11
0
async def test_sync_in_area(hass, registries):
    """Test a sync message where room hint comes from area."""
    area = registries.area.async_create("Living Room")

    device = registries.device.async_get_or_create(
        config_entry_id='1234',
        connections={
            (device_registry.CONNECTION_NETWORK_MAC, '12:34:56:AB:CD:EF')
        })
    registries.device.async_update_device(device.id, area_id=area.id)

    entity = registries.entity.async_get_or_create(
        'light', 'test', '1235',
        suggested_object_id='demo_light',
        device_id=device.id)

    light = DemoLight(
        None, 'Demo Light',
        state=False,
        hs_color=(180, 75),
    )
    light.hass = hass
    light.entity_id = entity.entity_id
    await light.async_update_ha_state()

    config = helpers.Config(
        should_expose=lambda _: True,
        allow_unlock=False,
        entity_config={}
    )

    events = []
    hass.bus.async_listen(EVENT_SYNC_RECEIVED, events.append)

    result = await sh.async_handle_message(
        hass, config, 'test-agent',
        {
            "requestId": REQ_ID,
            "inputs": [{
                "intent": "action.devices.SYNC"
            }]
        })

    assert result == {
        'requestId': REQ_ID,
        'payload': {
            'agentUserId': 'test-agent',
            'devices': [{
                'id': 'light.demo_light',
                'name': {
                    'name': 'Demo Light'
                },
                'traits': [
                    trait.TRAIT_BRIGHTNESS,
                    trait.TRAIT_ONOFF,
                    trait.TRAIT_COLOR_SPECTRUM,
                    trait.TRAIT_COLOR_TEMP,
                ],
                'type': sh.TYPE_LIGHT,
                'willReportState': False,
                'attributes': {
                    'colorModel': 'rgb',
                    'temperatureMinK': 2000,
                    'temperatureMaxK': 6535,
                },
                'roomHint': 'Living Room'
            }]
        }
    }
    await hass.async_block_till_done()

    assert len(events) == 1
    assert events[0].event_type == EVENT_SYNC_RECEIVED
    assert events[0].data == {
        'request_id': REQ_ID,
    }