예제 #1
0
async def test_state(retrievable_device: Other):
    next(iter(retrievable_device.capabilities())).value = 60.

    state = await retrievable_device.state()
    expected = {
        'id':
        'device1',
        'capabilities': [{
            'type': 'devices.capabilities.range',
            'state': {
                'instance': 'humidity',
                'value': 60.,
            },
        }],
        'properties': [],
    }
    assert state == expected
예제 #2
0
async def test_state(retrievable_device: Other):
    next(iter(retrievable_device.capabilities())).value = 'fan_only'

    state = await retrievable_device.state()
    expected = {
        'id': 'device1',
        'capabilities': [
            {
                'type': 'devices.capabilities.mode',
                'state': {
                    'instance': 'cleanup_mode',
                    'value': 'fan_only',
                },
            }
        ],
        'properties': [],
    }
    assert state == expected
예제 #3
0
async def test_state(retrievable_device: Other):
    next(iter(retrievable_device.capabilities())).value = True

    state = await retrievable_device.state()
    expected = {
        'id': 'device1',
        'capabilities': [
            {
                'type': 'devices.capabilities.toggle',
                'state': {
                    'instance': 'backlight',
                    'value': True,
                },
            }
        ],
        'properties': [],
    }
    assert state == expected
예제 #4
0
async def test_action(retrievable_device: Other):
    result = await retrievable_device.action([{
        'type': 'devices.capabilities.mode',
        'state': {
            'instance': 'unknown',
            'value': 'americano',
        }
    }], None)
    expected = {
        'id': 'device1',
        'capabilities': [
            {
                'type': 'devices.capabilities.mode',
                'state': {
                    'instance': 'unknown',
                    'action_result': {
                        'status': 'ERROR',
                        'error_code': 'INVALID_ACTION',
                        'error_message': 'Unknown capability for this device',
                    }
                }
            }
        ]
    }
    assert result == expected

    result = await retrievable_device.action([{
        'type': 'devices.capabilities.mode',
        'state': {
            'instance': 'cleanup_mode',
            'value': 'americano',
        }
    }], None)
    expected = {
        'id': 'device1',
        'capabilities': [
            {
                'type': 'devices.capabilities.mode',
                'state': {
                    'instance': 'cleanup_mode',
                    'action_result': {
                        'status': 'ERROR',
                        'error_code': 'NOT_SUPPORTED_IN_CURRENT_MODE',
                        'error_message': 'NOT_SUPPORTED_IN_CURRENT_MODE',
                    }
                }
            }
        ]
    }
    assert result == expected

    async def change_value(capability, instance, value):
        capability.value = value
        return capability.type_id, instance

    next(iter(retrievable_device.capabilities())).change_value = change_value
    result = await retrievable_device.action([{
        'type': 'devices.capabilities.mode',
        'state': {
            'instance': 'cleanup_mode',
            'value': 'fan_only',
        }
    }], None)
    expected = {
        'id': 'device1',
        'capabilities': [
            {
                'type': 'devices.capabilities.mode',
                'state': {
                    'instance': 'cleanup_mode',
                    'action_result': {
                        'status': 'DONE',
                    }
                }
            }
        ]
    }
    assert result == expected
    assert next(iter(retrievable_device.capabilities())).value == Mode.WorkMode.FanOnly.value
async def test_action(device: Other):
    result = await device.action([{
        'type': 'devices.capabilities.color_setting',
        'state': {
            'instance': 'unknown',
            'value': False,
        }
    }], None)
    expected = {
        'id': 'device1',
        'capabilities': [
            {
                'type': 'devices.capabilities.color_setting',
                'state': {
                    'instance': 'unknown',
                    'action_result': {
                        'status': 'ERROR',
                        'error_code': 'INVALID_ACTION',
                        'error_message': 'Unknown capability for this device',
                    }
                }
            }
        ],
    }
    assert result == expected

    result = await device.action([{
        'type': 'devices.capabilities.color_setting',
        'state': {
            'instance': 'temperature_k',
            'value': False,
        }
    }], None)
    expected = {
        'id': 'device1',
        'capabilities': [
            {
                'type': 'devices.capabilities.color_setting',
                'state': {
                    'instance': 'temperature_k',
                    'action_result': {
                        'status': 'ERROR',
                        'error_code': 'NOT_SUPPORTED_IN_CURRENT_MODE',
                        'error_message': 'NOT_SUPPORTED_IN_CURRENT_MODE',
                    }
                }
            }
        ],
    }
    assert result == expected

    async def change_value(capability, instance, value):
        if capability.value is not None and instance == capability.value.name:
            capability.value.assign(value)
        elif instance not in capability.instances:
            raise TypeError(f"Instance type {instance} is not supported for capability")
        else:
            if instance == capability.temperature.name:
                newval = capability.temperature
            else:
                newval = capability.color_model
            newval.assign(value)
            capability.value = newval

        return capability.type_id, instance

    next(iter(device.capabilities())).change_value = change_value
    result = await device.action([{
        'type': 'devices.capabilities.color_setting',
        'state': {
            'instance': 'temperature_k',
            'value': 6000,
        }
    }], None)
    expected = {
        'id': 'device1',
        'capabilities': [
            {
                'type': 'devices.capabilities.color_setting',
                'state': {
                    'instance': 'temperature_k',
                    'action_result': {
                        'status': 'DONE',
                    }
                }
            }
        ]
    }
    assert result == expected

    expected_state = {
        'id': 'device1',
        'capabilities': [
            {
                'type': 'devices.capabilities.color_setting',
                'state': {
                    'instance': 'temperature_k',
                    'value': 6000,
                }
            }
        ],
        'properties': [],
    }

    assert await device.state() == expected_state