async def test_lock_unlock(hass, hk_driver, events):
    """Test if accessory and HA are updated accordingly."""
    code = "1234"
    config = {ATTR_CODE: code}
    entity_id = "lock.kitchen_door"

    hass.states.async_set(entity_id, None)
    await hass.async_block_till_done()
    acc = Lock(hass, hk_driver, "Lock", entity_id, 2, config)
    await acc.run_handler()

    assert acc.aid == 2
    assert acc.category == 6  # DoorLock

    assert acc.char_current_state.value == 3
    assert acc.char_target_state.value == 1

    hass.states.async_set(entity_id, STATE_LOCKED)
    await hass.async_block_till_done()
    assert acc.char_current_state.value == 1
    assert acc.char_target_state.value == 1

    hass.states.async_set(entity_id, STATE_UNLOCKED)
    await hass.async_block_till_done()
    assert acc.char_current_state.value == 0
    assert acc.char_target_state.value == 0

    hass.states.async_set(entity_id, STATE_UNKNOWN)
    await hass.async_block_till_done()
    assert acc.char_current_state.value == 3
    assert acc.char_target_state.value == 0

    hass.states.async_remove(entity_id)
    await hass.async_block_till_done()
    assert acc.char_current_state.value == 3
    assert acc.char_target_state.value == 0

    # Set from HomeKit
    call_lock = async_mock_service(hass, DOMAIN, "lock")
    call_unlock = async_mock_service(hass, DOMAIN, "unlock")

    await hass.async_add_executor_job(
        acc.char_target_state.client_update_value, 1)
    await hass.async_block_till_done()
    assert call_lock
    assert call_lock[0].data[ATTR_ENTITY_ID] == entity_id
    assert call_lock[0].data[ATTR_CODE] == code
    assert acc.char_target_state.value == 1
    assert len(events) == 1
    assert events[-1].data[ATTR_VALUE] is None

    await hass.async_add_executor_job(
        acc.char_target_state.client_update_value, 0)
    await hass.async_block_till_done()
    assert call_unlock
    assert call_unlock[0].data[ATTR_ENTITY_ID] == entity_id
    assert call_unlock[0].data[ATTR_CODE] == code
    assert acc.char_target_state.value == 0
    assert len(events) == 2
    assert events[-1].data[ATTR_VALUE] is None
    def test_lock_unlock(self):
        """Test if accessory and HA are updated accordingly."""
        kitchen_lock = 'lock.kitchen_door'

        acc = Lock(self.hass, 'Lock', kitchen_lock, 2, config=None)
        acc.run()

        self.assertEqual(acc.aid, 2)
        self.assertEqual(acc.category, 6)  # DoorLock

        self.assertEqual(acc.char_current_state.value, 3)
        self.assertEqual(acc.char_target_state.value, 1)

        self.hass.states.set(kitchen_lock, STATE_LOCKED)
        self.hass.block_till_done()

        self.assertEqual(acc.char_current_state.value, 1)
        self.assertEqual(acc.char_target_state.value, 1)

        self.hass.states.set(kitchen_lock, STATE_UNLOCKED)
        self.hass.block_till_done()

        self.assertEqual(acc.char_current_state.value, 0)
        self.assertEqual(acc.char_target_state.value, 0)

        self.hass.states.set(kitchen_lock, STATE_UNKNOWN)
        self.hass.block_till_done()

        self.assertEqual(acc.char_current_state.value, 3)
        self.assertEqual(acc.char_target_state.value, 0)

        # Set from HomeKit
        acc.char_target_state.client_update_value(1)
        self.hass.block_till_done()
        self.assertEqual(
            self.events[0].data[ATTR_SERVICE], 'lock')
        self.assertEqual(acc.char_target_state.value, 1)

        acc.char_target_state.client_update_value(0)
        self.hass.block_till_done()
        self.assertEqual(
            self.events[1].data[ATTR_SERVICE], 'unlock')
        self.assertEqual(acc.char_target_state.value, 0)

        self.hass.states.remove(kitchen_lock)
        self.hass.block_till_done()
    def test_lock_unlock(self):
        """Test if accessory and HA are updated accordingly."""
        kitchen_lock = 'lock.kitchen_door'

        acc = Lock(self.hass, 'Lock', kitchen_lock, 2, config=None)
        acc.run()

        self.assertEqual(acc.aid, 2)
        self.assertEqual(acc.category, 6)  # DoorLock

        self.assertEqual(acc.char_current_state.value, 3)
        self.assertEqual(acc.char_target_state.value, 1)

        self.hass.states.set(kitchen_lock, STATE_LOCKED)
        self.hass.block_till_done()

        self.assertEqual(acc.char_current_state.value, 1)
        self.assertEqual(acc.char_target_state.value, 1)

        self.hass.states.set(kitchen_lock, STATE_UNLOCKED)
        self.hass.block_till_done()

        self.assertEqual(acc.char_current_state.value, 0)
        self.assertEqual(acc.char_target_state.value, 0)

        self.hass.states.set(kitchen_lock, STATE_UNKNOWN)
        self.hass.block_till_done()

        self.assertEqual(acc.char_current_state.value, 3)
        self.assertEqual(acc.char_target_state.value, 0)

        # Set from HomeKit
        acc.char_target_state.client_update_value(1)
        self.hass.block_till_done()
        self.assertEqual(self.events[0].data[ATTR_SERVICE], 'lock')
        self.assertEqual(acc.char_target_state.value, 1)

        acc.char_target_state.client_update_value(0)
        self.hass.block_till_done()
        self.assertEqual(self.events[1].data[ATTR_SERVICE], 'unlock')
        self.assertEqual(acc.char_target_state.value, 0)

        self.hass.states.remove(kitchen_lock)
        self.hass.block_till_done()
Beispiel #4
0
async def test_lock_unlock(hass):
    """Test if accessory and HA are updated accordingly."""
    code = '1234'
    config = {ATTR_CODE: code}
    entity_id = 'lock.kitchen_door'

    hass.states.async_set(entity_id, None)
    await hass.async_block_till_done()
    acc = Lock(hass, 'Lock', entity_id, 2, config)
    await hass.async_add_job(acc.run)

    assert acc.aid == 2
    assert acc.category == 6  # DoorLock

    assert acc.char_current_state.value == 3
    assert acc.char_target_state.value == 1

    hass.states.async_set(entity_id, STATE_LOCKED)
    await hass.async_block_till_done()
    assert acc.char_current_state.value == 1
    assert acc.char_target_state.value == 1

    hass.states.async_set(entity_id, STATE_UNLOCKED)
    await hass.async_block_till_done()
    assert acc.char_current_state.value == 0
    assert acc.char_target_state.value == 0

    hass.states.async_set(entity_id, STATE_UNKNOWN)
    await hass.async_block_till_done()
    assert acc.char_current_state.value == 3
    assert acc.char_target_state.value == 0

    hass.states.async_remove(entity_id)
    await hass.async_block_till_done()
    assert acc.char_current_state.value == 3
    assert acc.char_target_state.value == 0

    # Set from HomeKit
    call_lock = async_mock_service(hass, DOMAIN, 'lock')
    call_unlock = async_mock_service(hass, DOMAIN, 'unlock')

    await hass.async_add_job(acc.char_target_state.client_update_value, 1)
    await hass.async_block_till_done()
    assert call_lock
    assert call_lock[0].data[ATTR_ENTITY_ID] == entity_id
    assert call_lock[0].data[ATTR_CODE] == code
    assert acc.char_target_state.value == 1

    await hass.async_add_job(acc.char_target_state.client_update_value, 0)
    await hass.async_block_till_done()
    assert call_unlock
    assert call_unlock[0].data[ATTR_ENTITY_ID] == entity_id
    assert call_unlock[0].data[ATTR_CODE] == code
    assert acc.char_target_state.value == 0
Beispiel #5
0
async def test_no_code(hass, config):
    """Test accessory if lock doesn't require a code."""
    entity_id = 'lock.kitchen_door'

    hass.states.async_set(entity_id, None)
    await hass.async_block_till_done()
    acc = Lock(hass, 'Lock', entity_id, 2, config)

    # Set from HomeKit
    call_lock = async_mock_service(hass, DOMAIN, 'lock')

    await hass.async_add_job(acc.char_target_state.client_update_value, 1)
    await hass.async_block_till_done()
    assert call_lock
    assert call_lock[0].data[ATTR_ENTITY_ID] == entity_id
    assert ATTR_CODE not in call_lock[0].data
    assert acc.char_target_state.value == 1
async def test_no_code(hass, hk_driver, config, events):
    """Test accessory if lock doesn't require a code."""
    entity_id = "lock.kitchen_door"

    hass.states.async_set(entity_id, None)
    await hass.async_block_till_done()
    acc = Lock(hass, hk_driver, "Lock", entity_id, 2, config)

    # Set from HomeKit
    call_lock = async_mock_service(hass, DOMAIN, "lock")

    acc.char_target_state.client_update_value(1)
    await hass.async_block_till_done()
    assert call_lock
    assert call_lock[0].data[ATTR_ENTITY_ID] == entity_id
    assert ATTR_CODE not in call_lock[0].data
    assert acc.char_target_state.value == 1
    assert len(events) == 1
    assert events[-1].data[ATTR_VALUE] is None