示例#1
0
 def get_char(self, name):
     """Return new Characteristic object."""
     char_dict = self.char_types[name].copy()
     if ("Format" not in char_dict or "Permissions" not in char_dict
             or "UUID" not in char_dict):
         raise KeyError("Could not load char {}!".format(name))
     return Characteristic.from_dict(name, char_dict, from_loader=True)
示例#2
0
 def get_char(self, name):
     """Return new Characteristic object."""
     char_dict = self.char_types[name].copy()
     if 'Format' not in char_dict or \
         'Permissions' not in char_dict or \
             'UUID' not in char_dict:
         raise KeyError('Could not load char {}!'.format(name))
     return Characteristic.from_dict(name, char_dict)
示例#3
0
def get_char(props, valid=None, min_value=None, max_value=None):
    """Return a char object with given parameters."""
    if valid:
        props['ValidValues'] = valid
    if min_value:
        props['minValue'] = min_value
    if max_value:
        props['maxValue'] = max_value
    return Characteristic(display_name='Test Char', type_id=uuid1(),
                          properties=props)
示例#4
0
def get_char(props, valid=None, min_value=None, max_value=None):
    if valid is not None:
        props["ValidValues"] = valid
    if min_value is not None:
        props["minValue"] = min_value
    if max_value is not None:
        props["maxValue"] = max_value
    c = Characteristic(display_name="Test Char",
                       type_id=uuid.uuid1(),
                       properties=props)
    return c
示例#5
0
def test_from_dict():
    """Test creating a characteristic object from a dictionary."""
    uuid = uuid1()
    json_dict = {
        'UUID': str(uuid),
        'Format': 'int',
        'Permissions': 'read',
    }

    char = Characteristic.from_dict('Test Char', json_dict)
    assert char.display_name == 'Test Char'
    assert char.type_id == uuid
    assert char.properties == {'Format': 'int', 'Permissions': 'read'}
def test_from_dict():
    """Test creating a characteristic object from a dictionary."""
    uuid = uuid1()
    json_dict = {
        "UUID": str(uuid),
        "Format": "int",
        "Permissions": "read",
    }

    char = Characteristic.from_dict("Test Char", json_dict)
    assert char.display_name == "Test Char"
    assert char.type_id == uuid
    assert char.properties == {"Format": "int", "Permissions": "read"}
def test_mixing_service_char_callbacks_partial_failure(driver):
    bridge = Bridge(driver, "mybridge")
    acc = Accessory(driver, "TestAcc", aid=2)
    acc2 = UnavailableAccessory(driver, "TestAcc2", aid=3)

    service = Service(uuid1(), "Lightbulb")
    char_on = Characteristic("On", uuid1(), CHAR_PROPS)
    char_brightness = Characteristic("Brightness", uuid1(), CHAR_PROPS)

    service.add_characteristic(char_on)
    service.add_characteristic(char_brightness)

    def fail_callback(*_):
        raise ValueError

    service.setter_callback = fail_callback

    acc.add_service(service)
    bridge.add_accessory(acc)

    service2 = Service(uuid1(), "Lightbulb")
    char_on2 = Characteristic("On", uuid1(), CHAR_PROPS)
    char_brightness2 = Characteristic("Brightness", uuid1(), CHAR_PROPS)

    service2.add_characteristic(char_on2)
    service2.add_characteristic(char_brightness2)

    char_on2.setter_callback = fail_callback

    acc2.add_service(service2)
    bridge.add_accessory(acc2)

    char_on_iid = char_on.to_HAP()[HAP_REPR_IID]
    char_brightness_iid = char_brightness.to_HAP()[HAP_REPR_IID]
    char_on2_iid = char_on2.to_HAP()[HAP_REPR_IID]
    char_brightness2_iid = char_brightness2.to_HAP()[HAP_REPR_IID]

    driver.add_accessory(bridge)

    response = driver.set_characteristics(
        {
            HAP_REPR_CHARS: [
                {
                    HAP_REPR_AID: acc.aid,
                    HAP_REPR_IID: char_on_iid,
                    HAP_REPR_VALUE: True,
                },
                {
                    HAP_REPR_AID: acc.aid,
                    HAP_REPR_IID: char_brightness_iid,
                    HAP_REPR_VALUE: 88,
                },
                {
                    HAP_REPR_AID: acc2.aid,
                    HAP_REPR_IID: char_on2_iid,
                    HAP_REPR_VALUE: True,
                },
                {
                    HAP_REPR_AID: acc2.aid,
                    HAP_REPR_IID: char_brightness2_iid,
                    HAP_REPR_VALUE: 12,
                },
            ]
        },
        "mock_addr",
    )

    assert response == {
        HAP_REPR_CHARS: [
            {
                HAP_REPR_AID: acc.aid,
                HAP_REPR_IID: char_on_iid,
                HAP_REPR_STATUS:
                HAP_SERVER_STATUS.SERVICE_COMMUNICATION_FAILURE,
            },
            {
                HAP_REPR_AID: acc.aid,
                HAP_REPR_IID: char_brightness_iid,
                HAP_REPR_STATUS:
                HAP_SERVER_STATUS.SERVICE_COMMUNICATION_FAILURE,
            },
            {
                HAP_REPR_AID: acc2.aid,
                HAP_REPR_IID: char_on2_iid,
                HAP_REPR_STATUS:
                HAP_SERVER_STATUS.SERVICE_COMMUNICATION_FAILURE,
            },
            {
                HAP_REPR_AID: acc2.aid,
                HAP_REPR_IID: char_brightness2_iid,
                HAP_REPR_STATUS: 0,
            },
        ]
    }
def test_service_callbacks(driver):
    bridge = Bridge(driver, "mybridge")
    acc = Accessory(driver, "TestAcc", aid=2)
    acc2 = UnavailableAccessory(driver, "TestAcc2", aid=3)

    service = Service(uuid1(), "Lightbulb")
    char_on = Characteristic("On", uuid1(), CHAR_PROPS)
    char_brightness = Characteristic("Brightness", uuid1(), CHAR_PROPS)

    service.add_characteristic(char_on)
    service.add_characteristic(char_brightness)

    mock_callback = MagicMock()
    service.setter_callback = mock_callback

    acc.add_service(service)
    bridge.add_accessory(acc)

    service2 = Service(uuid1(), "Lightbulb")
    char_on2 = Characteristic("On", uuid1(), CHAR_PROPS)
    char_brightness2 = Characteristic("Brightness", uuid1(), CHAR_PROPS)

    service2.add_characteristic(char_on2)
    service2.add_characteristic(char_brightness2)

    mock_callback2 = MagicMock()
    service2.setter_callback = mock_callback2

    acc2.add_service(service2)
    bridge.add_accessory(acc2)

    char_on_iid = char_on.to_HAP()[HAP_REPR_IID]
    char_brightness_iid = char_brightness.to_HAP()[HAP_REPR_IID]
    char_on2_iid = char_on2.to_HAP()[HAP_REPR_IID]
    char_brightness2_iid = char_brightness2.to_HAP()[HAP_REPR_IID]

    driver.add_accessory(bridge)

    response = driver.set_characteristics(
        {
            HAP_REPR_CHARS: [
                {
                    HAP_REPR_AID: acc.aid,
                    HAP_REPR_IID: char_on_iid,
                    HAP_REPR_VALUE: True,
                },
                {
                    HAP_REPR_AID: acc.aid,
                    HAP_REPR_IID: char_brightness_iid,
                    HAP_REPR_VALUE: 88,
                },
                {
                    HAP_REPR_AID: acc2.aid,
                    HAP_REPR_IID: char_on2_iid,
                    HAP_REPR_VALUE: True,
                },
                {
                    HAP_REPR_AID: acc2.aid,
                    HAP_REPR_IID: char_brightness2_iid,
                    HAP_REPR_VALUE: 12,
                },
            ]
        },
        "mock_addr",
    )
    assert response is None

    mock_callback2.assert_called_with({"On": True, "Brightness": 12})
    mock_callback.assert_called_with({"On": True, "Brightness": 88})

    get_chars = driver.get_characteristics([
        "{}.{}".format(acc.aid, char_on_iid),
        "{}.{}".format(acc2.aid, char_on2_iid)
    ])
    assert get_chars == {
        "characteristics": [
            {
                "aid": acc.aid,
                "iid": char_on_iid,
                "status": 0,
                "value": True
            },
            {
                "aid": acc2.aid,
                "iid": char_on2_iid,
                "status": -70402
            },
        ]
    }

    def _fail_func():
        raise ValueError

    char_brightness.getter_callback = _fail_func
    get_chars = driver.get_characteristics([
        "{}.{}".format(acc.aid, char_on_iid),
        "{}.{}".format(acc2.aid, char_on2_iid),
        "{}.{}".format(acc2.aid, char_brightness_iid),
        "{}.{}".format(acc.aid, char_brightness2_iid),
    ])
    assert get_chars == {
        "characteristics": [
            {
                "aid": acc.aid,
                "iid": char_on_iid,
                "status": 0,
                "value": True
            },
            {
                "aid": acc2.aid,
                "iid": char_on2_iid,
                "status": -70402
            },
            {
                "aid": acc2.aid,
                "iid": char_brightness2_iid,
                "status": -70402
            },
            {
                "aid": acc.aid,
                "iid": char_brightness_iid,
                "status": -70402
            },
        ]
    }
示例#9
0
def get_chars():
    """Return example char objects."""
    c1 = Characteristic("Char 1", uuid1(), CHAR_PROPS)
    c2 = Characteristic("Char 2", uuid1(), CHAR_PROPS)
    return [c1, c2]
def test_service_callbacks(driver):
    bridge = Bridge(driver, "mybridge")
    acc = Accessory(driver, 'TestAcc', aid=2)
    acc2 = Accessory(driver, 'TestAcc2', aid=3)

    service = Service(uuid1(), 'Lightbulb')
    char_on = Characteristic('On', uuid1(), CHAR_PROPS)
    char_brightness = Characteristic('Brightness', uuid1(), CHAR_PROPS)

    service.add_characteristic(char_on)
    service.add_characteristic(char_brightness)

    mock_callback = MagicMock()
    service.setter_callback = mock_callback

    acc.add_service(service)
    bridge.add_accessory(acc)

    service2 = Service(uuid1(), 'Lightbulb')
    char_on2 = Characteristic('On', uuid1(), CHAR_PROPS)
    char_brightness2 = Characteristic('Brightness', uuid1(), CHAR_PROPS)

    service2.add_characteristic(char_on2)
    service2.add_characteristic(char_brightness2)

    mock_callback2 = MagicMock()
    service2.setter_callback = mock_callback2

    acc2.add_service(service2)
    bridge.add_accessory(acc2)

    char_on_iid = char_on.to_HAP()[HAP_REPR_IID]
    char_brightness_iid = char_brightness.to_HAP()[HAP_REPR_IID]
    char_on2_iid = char_on2.to_HAP()[HAP_REPR_IID]
    char_brightness2_iid = char_brightness2.to_HAP()[HAP_REPR_IID]

    driver.add_accessory(bridge)

    driver.set_characteristics(
        {
            HAP_REPR_CHARS: [{
                HAP_REPR_AID: acc.aid,
                HAP_REPR_IID: char_on_iid,
                HAP_REPR_VALUE: True
            }, {
                HAP_REPR_AID: acc.aid,
                HAP_REPR_IID: char_brightness_iid,
                HAP_REPR_VALUE: 88
            }, {
                HAP_REPR_AID: acc2.aid,
                HAP_REPR_IID: char_on2_iid,
                HAP_REPR_VALUE: True
            }, {
                HAP_REPR_AID: acc2.aid,
                HAP_REPR_IID: char_brightness2_iid,
                HAP_REPR_VALUE: 12
            }]
        }, "mock_addr")

    mock_callback2.assert_called_with({'On': True, 'Brightness': 12})
    mock_callback.assert_called_with({'On': True, 'Brightness': 88})
示例#11
0
def get_chars():
    c1 = Characteristic("Char 1", uuid.uuid1(), CHAR_PROPS)
    c2 = Characteristic("Char 2", uuid.uuid1(), CHAR_PROPS)
    return [c1, c2]