Пример #1
0
def test_wrong_version(hass):
    """Test with wrong version."""
    msg = smart_home.api_message('testName', 'testNameSpace')
    msg['header']['payloadVersion'] = '3'

    with pytest.raises(AssertionError):
        yield from smart_home.async_handle_message(hass, msg)
Пример #2
0
def test_create_api_message():
    """Create a API message."""
    msg = smart_home.api_message('testName', 'testNameSpace')

    assert msg['header']['messageId'] is not None
    assert msg['header']['name'] == 'testName'
    assert msg['header']['namespace'] == 'testNameSpace'
    assert msg['header']['payloadVersion'] == '2'
    assert msg['payload'] == {}
Пример #3
0
def test_api_entity_not_exists(hass):
    """Test api turn on process without entity."""
    msg_switch = smart_home.api_message(
        'TurnOnRequest', 'Alexa.ConnectedHome.Control', {
            'appliance': {
                'applianceId': 'switch#test'
            }
        })

    call_switch = async_mock_service(hass, 'switch', 'turn_on')

    resp = yield from smart_home.async_api_turn_on(hass, msg_switch)
    assert len(call_switch) == 0
    assert resp['header']['name'] == 'DriverInternalError'
    assert resp['header']['namespace'] == 'Alexa.ConnectedHome.Control'
Пример #4
0
def test_discovery_request(hass):
    """Test alexa discovery request."""
    msg = smart_home.api_message(
        'DiscoverAppliancesRequest', 'Alexa.ConnectedHome.Discovery')

    # settup test devices
    hass.states.async_set(
        'switch.test', 'on', {'friendly_name': "Test switch"})

    hass.states.async_set(
        'light.test_1', 'on', {'friendly_name': "Test light 1"})
    hass.states.async_set(
        'light.test_2', 'on', {
            'friendly_name': "Test light 2", 'supported_features': 1
        })

    resp = yield from smart_home.async_api_discovery(hass, msg)

    assert len(resp['payload']['discoveredAppliances']) == 3
    assert resp['header']['name'] == 'DiscoverAppliancesResponse'
    assert resp['header']['namespace'] == 'Alexa.ConnectedHome.Discovery'

    for i, appliance in enumerate(resp['payload']['discoveredAppliances']):
        if appliance['applianceId'] == 'switch#test':
            assert appliance['applianceTypes'][0] == "SWITCH"
            assert appliance['friendlyName'] == "Test switch"
            assert appliance['actions'] == ['turnOff', 'turnOn']
            continue

        if appliance['applianceId'] == 'light#test_1':
            assert appliance['applianceTypes'][0] == "LIGHT"
            assert appliance['friendlyName'] == "Test light 1"
            assert appliance['actions'] == ['turnOff', 'turnOn']
            continue

        if appliance['applianceId'] == 'light#test_2':
            assert appliance['applianceTypes'][0] == "LIGHT"
            assert appliance['friendlyName'] == "Test light 2"
            assert appliance['actions'] == \
                ['turnOff', 'turnOn', 'setPercentage']
            continue

        raise AssertionError("Unknown appliance!")
Пример #5
0
def test_create_api_message_special():
    """Create a API message response of a request with non defaults."""
    request = get_new_request('Alexa.PowerController', 'TurnOn')
    request = request['directive']

    request['header'].pop('correlationToken')

    msg = smart_home.api_message(request, 'testName', 'testNameSpace')

    assert 'event' in msg
    msg = msg['event']

    assert msg['header']['messageId'] is not None
    assert msg['header']['messageId'] != request['header']['messageId']
    assert 'correlationToken' not in msg['header']
    assert msg['header']['name'] == 'testName'
    assert msg['header']['namespace'] == 'testNameSpace'
    assert msg['header']['payloadVersion'] == '3'

    assert msg['payload'] == {}
    assert 'endpoint' not in msg
Пример #6
0
def test_api_turn_off(hass, domain):
    """Test api turn on process."""
    msg = smart_home.api_message(
        'TurnOffRequest', 'Alexa.ConnectedHome.Control', {
            'appliance': {
                'applianceId': '{}#test'.format(domain)
            }
        })

    # settup test devices
    hass.states.async_set(
        '{}.test'.format(domain), 'on', {
            'friendly_name': "Test {}".format(domain)
        })

    call = async_mock_service(hass, domain, 'turn_off')

    resp = yield from smart_home.async_api_turn_off(hass, msg)
    assert len(call) == 1
    assert call[0].data['entity_id'] == '{}.test'.format(domain)
    assert resp['header']['name'] == 'TurnOffConfirmation'
Пример #7
0
def test_create_api_message_special():
    """Create a API message response of a request with non defaults."""
    request = get_new_request('Alexa.PowerController', 'TurnOn')
    request = request['directive']

    request['header'].pop('correlationToken')

    msg = smart_home.api_message(request, 'testName', 'testNameSpace')

    assert 'event' in msg
    msg = msg['event']

    assert msg['header']['messageId'] is not None
    assert msg['header']['messageId'] != request['header']['messageId']
    assert 'correlationToken' not in msg['header']
    assert msg['header']['name'] == 'testName'
    assert msg['header']['namespace'] == 'testNameSpace'
    assert msg['header']['payloadVersion'] == '3'

    assert msg['payload'] == {}
    assert 'endpoint' not in msg
Пример #8
0
def test_create_api_message_defaults():
    """Create a API message response of a request with defaults."""
    request = get_new_request('Alexa.PowerController', 'TurnOn', 'switch#xy')
    request = request['directive']

    msg = smart_home.api_message(request, payload={'test': 3})

    assert 'event' in msg
    msg = msg['event']

    assert msg['header']['messageId'] is not None
    assert msg['header']['messageId'] != request['header']['messageId']
    assert msg['header']['correlationToken'] == \
        request['header']['correlationToken']
    assert msg['header']['name'] == 'Response'
    assert msg['header']['namespace'] == 'Alexa'
    assert msg['header']['payloadVersion'] == '3'

    assert 'test' in msg['payload']
    assert msg['payload']['test'] == 3

    assert msg['endpoint'] == request['endpoint']
Пример #9
0
def test_create_api_message_defaults():
    """Create a API message response of a request with defaults."""
    request = get_new_request('Alexa.PowerController', 'TurnOn', 'switch#xy')
    request = request['directive']

    msg = smart_home.api_message(request, payload={'test': 3})

    assert 'event' in msg
    msg = msg['event']

    assert msg['header']['messageId'] is not None
    assert msg['header']['messageId'] != request['header']['messageId']
    assert msg['header']['correlationToken'] == \
        request['header']['correlationToken']
    assert msg['header']['name'] == 'Response'
    assert msg['header']['namespace'] == 'Alexa'
    assert msg['header']['payloadVersion'] == '3'

    assert 'test' in msg['payload']
    assert msg['payload']['test'] == 3

    assert msg['endpoint'] == request['endpoint']
Пример #10
0
def test_api_set_percentage_light(hass):
    """Test api set brightness process."""
    msg_light = smart_home.api_message(
        'SetPercentageRequest', 'Alexa.ConnectedHome.Control', {
            'appliance': {
                'applianceId': 'light#test'
            },
            'percentageState': {
                'value': '50'
            }
        })

    # settup test devices
    hass.states.async_set(
        'light.test', 'off', {'friendly_name': "Test light"})

    call_light = async_mock_service(hass, 'light', 'turn_on')

    resp = yield from smart_home.async_api_set_percentage(hass, msg_light)
    assert len(call_light) == 1
    assert call_light[0].data['entity_id'] == 'light.test'
    assert call_light[0].data['brightness'] == '50'
    assert resp['header']['name'] == 'SetPercentageConfirmation'