Exemplo n.º 1
0
def test_repr():
    """Test service representation."""
    service = Service(uuid1(), "TestService")
    service.characteristics = [get_chars()[0]]
    assert (
        service.__repr__() == "<service display_name=TestService chars={'Char 1': 0}>"
    )
Exemplo n.º 2
0
def test_linked_service_to_HAP():
    """Test created HAP representation of a service."""
    uuid = uuid1()
    pyhap_char_to_HAP = "pyhap.characteristic.Characteristic.to_HAP"

    service = Service(uuid, "Test Service")
    linked_service = Service(uuid1(), "Test Linked Service")
    service.add_linked_service(linked_service)
    service.characteristics = get_chars()
    with patch(pyhap_char_to_HAP) as mock_char_HAP, patch.object(
        service, "broker"
    ) as mock_broker, patch.object(linked_service, "broker") as mock_linked_broker:
        mock_iid = mock_broker.iid_manager.get_iid
        mock_iid.return_value = 2
        mock_linked_iid = mock_linked_broker.iid_manager.get_iid
        mock_linked_iid.return_value = 3
        mock_char_HAP.side_effect = ("Char 1", "Char 2")
        hap_repr = service.to_HAP()
        mock_iid.assert_called_with(service)

    assert hap_repr == {
        "iid": 2,
        "type": str(uuid).upper(),
        "characteristics": ["Char 1", "Char 2"],
        "linked": [mock_linked_iid()],
    }
Exemplo n.º 3
0
def test_linked_service_to_HAP():
    """Test created HAP representation of a service."""
    uuid = uuid1()
    pyhap_char_to_HAP = 'pyhap.characteristic.Characteristic.to_HAP'

    service = Service(uuid, 'Test Service')
    linked_service = Service(uuid1(), 'Test Linked Service')
    service.add_linked_service(linked_service)
    service.characteristics = get_chars()
    with patch(pyhap_char_to_HAP) as mock_char_HAP, \
            patch.object(service, 'broker') as mock_broker, \
            patch.object(linked_service, 'broker') as mock_linked_broker:
        mock_iid = mock_broker.iid_manager.get_iid
        mock_iid.return_value = 2
        mock_linked_iid = mock_linked_broker.iid_manager.get_iid
        mock_linked_iid.return_value = 3
        mock_char_HAP.side_effect = ('Char 1', 'Char 2')
        hap_repr = service.to_HAP()
        mock_iid.assert_called_with(service)

    assert hap_repr == {
        'iid': 2,
        'type': str(uuid).upper(),
        'characteristics': ['Char 1', 'Char 2'],
        'linked': [mock_linked_iid()],
    }
Exemplo n.º 4
0
def test_get_characteristic():
    """Test getting a characteristic from a service."""
    service = Service(uuid1(), "Test Service")
    chars = get_chars()
    service.characteristics = chars
    assert service.get_characteristic("Char 1") == chars[0]
    with pytest.raises(ValueError):
        service.get_characteristic("Not found")
Exemplo n.º 5
0
def test_configure_char():
    """Test preconfiguring a characteristic from a service."""
    pyhap_char = "pyhap.characteristic.Characteristic"

    service = Service(uuid1(), "Test Service")
    chars = get_chars()
    service.characteristics = chars

    with pytest.raises(ValueError):
        service.configure_char("Char not found")
    assert service.configure_char("Char 1") == chars[0]

    with patch(pyhap_char + ".override_properties") as mock_override_prop, patch(
        pyhap_char + ".set_value"
    ) as mock_set_value:
        service.configure_char("Char 1")
        mock_override_prop.assert_not_called()
        mock_set_value.assert_not_called()
        assert service.get_characteristic("Char 1").setter_callback is None

    with patch(pyhap_char + ".override_properties") as mock_override_prop:
        new_properties = {"Format": "string"}
        new_valid_values = {0: "on", 1: "off"}
        service.configure_char("Char 1", properties=new_properties)
        mock_override_prop.assert_called_with(new_properties, None)
        service.configure_char("Char 1", valid_values=new_valid_values)
        mock_override_prop.assert_called_with(None, new_valid_values)
        service.configure_char(
            "Char 1", properties=new_properties, valid_values=new_valid_values
        )
        mock_override_prop.assert_called_with(new_properties, new_valid_values)

    with patch(pyhap_char + ".set_value") as mock_set_value:
        new_value = 1
        service.configure_char("Char 1", value=new_value)
        mock_set_value.assert_called_with(1, should_notify=False)

    new_setter_callback = "Test callback"
    service.configure_char("Char 1", setter_callback=new_setter_callback)
    assert service.get_characteristic("Char 1").setter_callback == new_setter_callback
Exemplo n.º 6
0
def test_configure_char():
    """Test preconfiguring a characteristic from a service."""
    pyhap_char = 'pyhap.characteristic.Characteristic'

    service = Service(uuid1(), 'Test Service')
    chars = get_chars()
    service.characteristics = chars

    with pytest.raises(ValueError):
        service.configure_char('Char not found')
    assert service.configure_char('Char 1') == chars[0]

    with patch(pyhap_char + '.override_properties') as mock_override_prop, \
            patch(pyhap_char + '.set_value') as mock_set_value:
        service.configure_char('Char 1')
        mock_override_prop.assert_not_called()
        mock_set_value.assert_not_called()
        assert service.get_characteristic('Char 1').setter_callback is None

    with patch(pyhap_char + '.override_properties') as mock_override_prop:
        new_properties = {'Format': 'string'}
        new_valid_values = {0: 'on', 1: 'off'}
        service.configure_char('Char 1', properties=new_properties)
        mock_override_prop.assert_called_with(new_properties, None)
        service.configure_char('Char 1', valid_values=new_valid_values)
        mock_override_prop.assert_called_with(None, new_valid_values)
        service.configure_char('Char 1',
                               properties=new_properties,
                               valid_values=new_valid_values)
        mock_override_prop.assert_called_with(new_properties, new_valid_values)

    with patch(pyhap_char + '.set_value') as mock_set_value:
        new_value = 1
        service.configure_char('Char 1', value=new_value)
        mock_set_value.assert_called_with(1, should_notify=False)

    new_setter_callback = 'Test callback'
    service.configure_char('Char 1', setter_callback=new_setter_callback)
    assert service.get_characteristic('Char 1').setter_callback == \
        new_setter_callback