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 }, ] }
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 = 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})