示例#1
0
def _parse_attribute(entity, attrib, value, *argv, **kwargs):
    """parse non standard attributes."""
    import zigpy.types as t
    from zigpy.zcl import foundation as f
    if type(value) is str:
        result = bytearray()
        result.extend(map(ord, value))
        value = result
    if entity.entity_connect == {}:
        entity_store = zha_new.get_entity_store(entity.hass)
        device_store = entity_store.get(entity._endpoint._device._ieee, {})
        for dev_ent in device_store:
            if hasattr(dev_ent, 'cluster_key'):
                entity.entity_connect[dev_ent.cluster_key] = dev_ent
    attributes = {}
    if attrib == 85:
        #result = []
        result = float(t.Double(value))
        attributes["power"] = float(t.Double(value))
        attributes["unit_of_measurement"] = 'W'
        attrib = 0
    else:
        result = value
    attributes["Last seen"] = dt_util.now()
    if "path" in attributes:
        entity._endpoint._device.handle_RouteRecord(attributes["path"])
    entity._device_state_attributes.update(attributes)
    return (attrib, result)
示例#2
0
def test_single_and_double_with_struct(value, only_double):
    # Float and double must match the behavior of the built-in struct module
    if not only_double:
        assert t.Single(value).serialize() == struct.pack("<f", value)
        v1, r1 = t.Single.deserialize(struct.pack("<f", value))
        assert compare_with_nan(v1, t.Single(value))
        assert r1 == b""

    assert t.Double(value).serialize() == struct.pack("<d", value)
    v2, r2 = t.Double.deserialize(struct.pack("<d", value))
    assert compare_with_nan(v2, t.Double(value))
    assert r2 == b""
示例#3
0
def test_double():
    value = 1.25
    extra = b"ab12!"
    v = t.Double(value)
    ser = v.serialize()
    assert t.Double.deserialize(ser) == (value, b"")
    assert t.Double.deserialize(ser + extra) == (value, extra)

    with pytest.raises(ValueError):
        t.Double.deserialize(ser[1:])
def _parse_attribute(entity, attrib, value, *argv):
    """ parse non standard atrributes."""
    import zigpy.types as t
    from zigpy.zcl import foundation as f
    _LOGGER.debug('parse value type %s', type(value))
    result = []
    if type(value) is str:
        result = bytearray()
        result.extend(map(ord, value))
        value = result
    if entity.entity_connect == {}:
        entity_store = zha_new.get_entity_store(entity.hass)
        device_store = entity_store.get(entity._endpoint._device._ieee, {})
        for dev_ent in device_store:
            if hasattr(dev_ent, 'cluster_key'):
                entity.entity_connect[dev_ent.cluster_key] = dev_ent
    attributes = {}
    if attrib == 85:
        attributes["value"] = float(t.Double(value))
        attributes["last seen"] = dt_util.now()
        if isinstance(value, float):       # rotation
            event = "rotation"
            command = 'cube_rotation'
        else:
            if value >= 499:
                event = "double_tap"
            elif value >= 250:
                event = "slide"
            elif value >= 100:
                event = "flip_180"
            elif value == 0:
                event = "shake"
            elif value <= 15:
                event = "noise"
            elif value > 0:
                event = "flip_90"
            command = 'cube_slide'
        _LOGGER.debug("value### "+str(result) + "## state ## "+event)
        entity._state = event
        event_data = {
                    'entity_id': entity.entity_id,
                    'channel': 'Level',
                    'command': command,
                    'step': event
                   }
        entity.hass.bus.fire('cube_event', event_data)
    elif attrib == 0xff01:
        _LOGGER.debug("Parse dict 0xff01: set friendly attribute names")
        attribute_name = {
            4: "X-attrib-4",
            1: "battery_voltage_mV",
            100: "temperature",
            101: "humidity",
            102: "pressure",
            5: "X-attrib-5",
            6: "X-attrib-6",
            10: "path"  # was X-attrib-10
        }
        result = {}
        while value:
            skey = int(value[0])
            svalue, value = f.TypeValue.deserialize(value[1:])
            result[skey] = svalue.value
        for item, value in result.items():
            key = attribute_name[item] \
                if item in attribute_name else "0xff01-" + str(item)
            attributes[key] = value
        if "battery_voltage_mV" in attributes:
            attributes["battery_level"] = int(
                _battery_percent(attributes["battery_voltage_mV"]))

    if "path" in attributes:
        entity._endpoint._device.handle_RouteRecord(attributes["path"])
    entity._device_state_attributes.update(attributes)
示例#5
0
def test_double():
    v = t.Double(1.25)
    ser = v.serialize()
    assert t.Double.deserialize(ser) == (1.25, b'')