예제 #1
0
async def test_property_custom_get_value_button_event(hass):
    state = State('sensor.button', '')
    prop = CustomEntityProperty.get(
        hass, BASIC_CONFIG, state, {
            const.CONF_ENTITY_PROPERTY_TYPE: const.EVENT_INSTANCE_BUTTON,
        })
    assert prop.supported()
    assert prop.get_value() is None

    state = State('sensor.button', '', {'action': 'foo'})
    prop = CustomEntityProperty.get(
        hass, BASIC_CONFIG, state, {
            const.CONF_ENTITY_PROPERTY_TYPE: const.EVENT_INSTANCE_BUTTON,
            const.CONF_ENTITY_PROPERTY_ATTRIBUTE: 'action'
        })
    assert prop.supported()
    assert prop.get_value() is None

    state = State('sensor.button', '', {'action': 'long_click_press'})
    prop = CustomEntityProperty.get(
        hass, BASIC_CONFIG, state, {
            const.CONF_ENTITY_PROPERTY_TYPE: const.EVENT_INSTANCE_BUTTON,
            const.CONF_ENTITY_PROPERTY_ATTRIBUTE: 'action'
        })
    assert prop.supported()
    assert prop.get_value() == 'long_press'
예제 #2
0
async def test_property_custom_get_value_binary_event(hass):
    state = State('binary_sensor.test', STATE_UNAVAILABLE)
    prop = CustomEntityProperty.get(
        hass, BASIC_CONFIG, state, {
            const.CONF_ENTITY_PROPERTY_TYPE: const.EVENT_INSTANCE_GAS,
        })
    assert prop.get_value() is None

    state = State('binary_sensor.test', STATE_ON)
    prop = CustomEntityProperty.get(
        hass, BASIC_CONFIG, state, {
            const.CONF_ENTITY_PROPERTY_TYPE: const.EVENT_INSTANCE_GAS,
        })
    assert prop.get_value() == 'detected'
예제 #3
0
async def test_property_custom_get_value_float_conversion(
        hass, instance: str, unit, value):
    state = State('sensor.test', '100')
    prop = CustomEntityProperty.get(
        hass, BASIC_CONFIG, state, {
            const.CONF_ENTITY_PROPERTY_TYPE: instance,
            const.CONF_ENTITY_PROPERTY_UNIT_OF_MEASUREMENT: unit
        })
    assert prop.get_value() == value
    prop.state.state = STATE_UNAVAILABLE
    assert prop.get_value() is None

    state = State('sensor.test', '100', {ATTR_UNIT_OF_MEASUREMENT: unit})
    prop = CustomEntityProperty.get(
        hass, BASIC_CONFIG, state, {const.CONF_ENTITY_PROPERTY_TYPE: instance})
    assert prop.get_value() == value
예제 #4
0
async def test_property_custom_value_float_limit(hass):
    state = State('sensor.test', '-5')
    prop = CustomEntityProperty.get(hass, BASIC_CONFIG, state, {
        const.CONF_ENTITY_PROPERTY_TYPE:
        const.FLOAT_INSTANCE_BATTERY_LEVEL,
    })
    assert prop.supported()
    assert prop.get_value() == 0
예제 #5
0
async def test_property_custom(hass, domain, instance):
    state = State(f'{domain}.test', '10')
    if domain == binary_sensor.DOMAIN and instance in [
            'humidity', 'temperature', 'pressure', 'co2_level', 'power',
            'voltage', 'amperage', 'illumination', 'tvoc', 'pm1_density',
            'pm2.5_density', 'pm10_density'
    ]:
        with pytest.raises(SmartHomeError) as e:
            CustomEntityProperty.get(
                hass, BASIC_CONFIG, state,
                {const.CONF_ENTITY_PROPERTY_TYPE: instance})

        assert e.value.code == const.ERR_DEVICE_UNREACHABLE
        assert 'Unsupported' in e.value.message
        return

    prop = CustomEntityProperty.get(
        hass, BASIC_CONFIG, state, {const.CONF_ENTITY_PROPERTY_TYPE: instance})
    if instance in [
            'vibration', 'open', 'button', 'motion', 'smoke', 'gas',
            'water_leak'
    ]:
        assert prop.type == PROPERTY_EVENT
    else:
        if domain == binary_sensor.DOMAIN and instance in [
                'water_level', 'battery_level'
        ]:
            assert prop.type == PROPERTY_EVENT
        else:
            assert prop.type == PROPERTY_FLOAT

    assert prop.parameters()['instance'] == instance

    if prop.type == PROPERTY_FLOAT:
        instance_unit = prop.parameters()['unit']
        if instance == 'pressure':
            assert 'pressure' in instance_unit

    if prop.type == PROPERTY_EVENT:
        assert len(prop.parameters()['events']) != 0

    if instance in ['button', 'vibration']:
        assert not prop.retrievable
    else:
        assert prop.retrievable
예제 #6
0
async def test_property_custom_get_value_float(hass):
    state = State('sensor.test', '3.36')
    prop = CustomEntityProperty.get(
        hass, BASIC_CONFIG, state, {
            const.CONF_ENTITY_PROPERTY_TYPE: const.FLOAT_INSTANCE_TEMPERATURE,
        })
    assert prop.supported()
    assert prop.get_value() == 3.36
    for s in ['', '-', 'none', 'unknown']:
        prop.state.state = s.upper()
        assert prop.get_value() is None

    prop.state.state = 'not-a-number'
    with pytest.raises(SmartHomeError) as e:
        prop.get_value()
    assert e.value.code == const.ERR_NOT_SUPPORTED_IN_CURRENT_MODE
    assert 'Unsupported' in e.value.message

    with pytest.raises(SmartHomeError) as e:
        prop = CustomEntityProperty.get(
            hass, BASIC_CONFIG, state, {
                const.CONF_ENTITY_PROPERTY_TYPE:
                const.FLOAT_INSTANCE_TEMPERATURE,
                const.CONF_ENTITY_PROPERTY_ATTRIBUTE: 'value'
            })
        prop.get_value()
    assert e.value.code == const.ERR_DEVICE_UNREACHABLE
    assert 'not found' in e.value.message

    state = State('sensor.test', '3')
    with pytest.raises(SmartHomeError) as e:
        CustomEntityProperty.get(
            hass, BASIC_CONFIG, state, {
                const.CONF_ENTITY_PROPERTY_TYPE:
                const.FLOAT_INSTANCE_TEMPERATURE,
                const.CONF_ENTITY_PROPERTY_ENTITY: 'sensor.test_2'
            })
    assert e.value.code == const.ERR_DEVICE_UNREACHABLE
    assert 'not found' in e.value.message

    hass.states.async_set('sensor.test_2', '4.52')
    prop = CustomEntityProperty.get(
        hass, BASIC_CONFIG, state, {
            const.CONF_ENTITY_PROPERTY_TYPE: const.FLOAT_INSTANCE_TEMPERATURE,
            const.CONF_ENTITY_PROPERTY_ENTITY: 'sensor.test_2'
        })
    assert prop.get_value() == 4.52

    hass.states.async_set('sensor.test_2', '4.52', {'value': 9.99})
    prop = CustomEntityProperty.get(
        hass, BASIC_CONFIG, state, {
            const.CONF_ENTITY_PROPERTY_TYPE: const.FLOAT_INSTANCE_TEMPERATURE,
            const.CONF_ENTITY_PROPERTY_ENTITY: 'sensor.test_2',
            const.CONF_ENTITY_PROPERTY_ATTRIBUTE: 'value'
        })
    assert prop.get_value() == 9.99
예제 #7
0
async def test_yandex_entity_serialize(hass):
    class PauseCapability(ToggleCapability):
        instance = TOGGLE_INSTANCE_PAUSE

        def supported(self) -> bool:
            return True

        def get_value(self):
            if self.state.state == STATE_UNAVAILABLE:
                return None

            return self.state.state == STATE_ON

        async def set_state(self, *args, **kwargs):
            pass

    state = State('switch.unavailable', STATE_UNAVAILABLE)
    entity = YandexEntity(hass, BASIC_CONFIG, state)
    assert entity.query_serialize() == {'id': state.entity_id, 'error_code': ERR_DEVICE_UNREACHABLE}
    assert entity.notification_serialize('') == {'id': state.entity_id, 'error_code': ERR_DEVICE_UNREACHABLE}

    state = State('switch.test', STATE_ON)
    state_pause = State('input_boolean.pause', STATE_OFF)
    cap_onoff = OnOffCapabilityBasic(hass, BASIC_CONFIG, state)
    cap_pause = PauseCapability(hass, BASIC_CONFIG, state_pause)

    state_temp = State('sensor.temp', '5', attributes={
        ATTR_UNIT_OF_MEASUREMENT: TEMP_CELSIUS,
        ATTR_DEVICE_CLASS: DEVICE_CLASS_TEMPERATURE,
    })
    state_humidity = State('sensor.humidity', '95', attributes={
        ATTR_UNIT_OF_MEASUREMENT: PERCENTAGE,
        ATTR_DEVICE_CLASS: DEVICE_CLASS_HUMIDITY,
    })
    hass.states.async_set(state_humidity.entity_id, state_humidity.state, state_humidity.attributes)

    state_voltage = State('sensor.voltage', '220', attributes={
        ATTR_UNIT_OF_MEASUREMENT: 'V',
        ATTR_DEVICE_CLASS: DEVICE_CLASS_VOLTAGE,
    })

    prop_temp = TemperatureProperty(hass, BASIC_CONFIG, state_temp)
    prop_humidity_custom = CustomEntityProperty.get(hass, BASIC_CONFIG, state, {
        CONF_ENTITY_PROPERTY_ENTITY: state_humidity.entity_id,
        CONF_ENTITY_PROPERTY_TYPE: const.FLOAT_INSTANCE_HUMIDITY,
    })
    prop_voltage = VoltageProperty(hass, BASIC_CONFIG, state_voltage)

    state_button = State('binary_sensor.button', '', attributes={
        'action': 'click'
    })
    prop_button = CustomEventEntityProperty(hass, BASIC_CONFIG, state, state_button, {
        CONF_ENTITY_PROPERTY_ATTRIBUTE: 'action',
        CONF_ENTITY_PROPERTY_TYPE: const.EVENT_INSTANCE_BUTTON
    })

    entity = YandexEntity(hass, BASIC_CONFIG, state)

    with patch.object(entity, 'capabilities', return_value=[cap_onoff, cap_pause]), patch.object(
        entity, 'properties', return_value=[prop_temp, prop_voltage, prop_humidity_custom, prop_button]
    ):
        assert entity.query_serialize() == {
            'id': 'switch.test',
            'capabilities': [{
                'type': 'devices.capabilities.on_off',
                'state': {'instance': 'on', 'value': True}
            }, {
                'type': 'devices.capabilities.toggle',
                'state': {'instance': 'pause', 'value': False}
            }],
            'properties': [{
                'type': 'devices.properties.float',
                'state': {'instance': 'temperature', 'value': 5.0}
            }, {
                'type': 'devices.properties.float',
                'state': {'instance': 'voltage', 'value': 220.0}
            }, {
                'type': 'devices.properties.float',
                'state': {'instance': 'humidity', 'value': 95.0}
            }]
        }
        assert entity.notification_serialize('switch.test') == {
            'id': 'switch.test',
            'capabilities': [{
                'type': 'devices.capabilities.on_off',
                'state': {'instance': 'on', 'value': True}
            }, {
                'type': 'devices.capabilities.toggle',
                'state': {'instance': 'pause', 'value': False}
            }],
            'properties': []
        }
        assert entity.notification_serialize('sensor.voltage') == {
            'id': 'switch.test',
            'capabilities': [{
                'type': 'devices.capabilities.on_off',
                'state': {'instance': 'on', 'value': True}
            }, {
                'type': 'devices.capabilities.toggle',
                'state': {'instance': 'pause', 'value': False}
            }],
            'properties': [{
                'type': 'devices.properties.float',
                'state': {'instance': 'voltage', 'value': 220.0}
            }]
        }
        assert entity.notification_serialize('sensor.humidity') == {
            'id': 'switch.test',
            'capabilities': [{
                'type': 'devices.capabilities.on_off',
                'state': {'instance': 'on', 'value': True}
            }, {
                'type': 'devices.capabilities.toggle',
                'state': {'instance': 'pause', 'value': False}
            }],
            'properties': [{
                'type': 'devices.properties.float',
                'state': {'instance': 'humidity', 'value': 95.0}
            }]
        }

        prop_voltage.reportable = False
        assert entity.notification_serialize('sensor.voltage') == {
            'id': 'switch.test',
            'capabilities': [{
                'type': 'devices.capabilities.on_off',
                'state': {'instance': 'on', 'value': True}
            }, {
                'type': 'devices.capabilities.toggle',
                'state': {'instance': 'pause', 'value': False}
            }],
            'properties': []
        }
        prop_voltage.reportable = True

        assert entity.notification_serialize('binary_sensor.button') == {
            'id': 'switch.test',
            'capabilities': [{
                'type': 'devices.capabilities.on_off',
                'state': {'instance': 'on', 'value': True}
            }, {
                'type': 'devices.capabilities.toggle',
                'state': {'instance': 'pause', 'value': False}
            }],
            'properties': [{
                'type': 'devices.properties.event',
                'state': {'instance': 'button', 'value': 'click'}
            }]
        }

        cap_pause.retrievable = False
        prop_temp.retrievable = False
        assert entity.query_serialize() == {
            'id': 'switch.test',
            'capabilities': [{
                'type': 'devices.capabilities.on_off',
                'state': {'instance': 'on', 'value': True}
            }],
            'properties': [{
                'type': 'devices.properties.float',
                'state': {'instance': 'voltage', 'value': 220.0}
            }, {
                'type': 'devices.properties.float',
                'state': {'instance': 'humidity', 'value': 95.0}
            }]
        }
        cap_pause.retrievable = True
        prop_temp.retrievable = True

        state_pause.state = STATE_UNAVAILABLE
        state_voltage.state = STATE_UNAVAILABLE
        prop_humidity_custom.property_state.state = STATE_UNAVAILABLE
        assert entity.query_serialize() == {
            'id': 'switch.test',
            'capabilities': [{
                'type': 'devices.capabilities.on_off',
                'state': {'instance': 'on', 'value': True}
            }],
            'properties': [{
                'type': 'devices.properties.float',
                'state': {'instance': 'temperature', 'value': 5.0}
            }]
        }
예제 #8
0
async def test_property_custom_no_beta(hass):
    state = State('binary_sensor.test', STATE_ON)
    prop = CustomEntityProperty.get(
        hass, ConfigNoBeta(), state,
        {const.CONF_ENTITY_PROPERTY_TYPE: 'button'})
    assert prop.supported() is False