コード例 #1
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}
            }]
        }