コード例 #1
0
def test__send_request__():
    with pytest.raises(requests.exceptions.HTTPError):   
        with requests_mock.Mocker() as m:
            m.register_uri('GET', re.compile(constants.UNLOCK_RESOURCE), text='Not Found', status_code=403)
            assert TTLock(clientId=FAKE_CLIENT_ID,accessToken=FAKE_ACCESS_TOKEN).unlock(lockId=1928723)
    with requests_mock.Mocker() as m:
        m.register_uri('GET', re.compile(constants.UNLOCK_RESOURCE), text='{"errcode": 0,"errmsg": "none error message or means yes","description": "表示成功或是"}')
        assert TTLock(clientId=FAKE_CLIENT_ID
        ,accessToken=FAKE_ACCESS_TOKEN).unlock(lockId=1928723)    
コード例 #2
0
def test_ttlock_no_mock_request():

    with pytest.raises(TTlockAPIError) as ttLock_error:
        response = TTLock(clientId=FAKE_CLIENT_ID
                    ,accessToken=FAKE_ACCESS_TOKEN).lock_state()
        assert response.status_code in constants.GOOD_HTTP_CODES
        assert not ttLock_error.error_code == INVALID_CURRENT_TIMESTAMP_ERROR
    
    with pytest.raises(TTlockAPIError) as ttLock_error:
        response = TTLock(clientId=FAKE_CLIENT_ID
                    ,accessToken=FAKE_ACCESS_TOKEN).lock_electric_quantity()
        assert response.status_code in constants.GOOD_HTTP_CODES
        assert not ttLock_error.error_code == INVALID_CURRENT_TIMESTAMP_ERROR
コード例 #3
0
def test_ttlock_no_mock_invalid_date_current():
    with pytest.raises(TTlockAPIError) as ttLock_error:
        _url_request = constants.GATEWAY_LIST_URL.format(
            constants.API_URI,
            constants.GATEWAY_LIST_RESOURCE,
            FAKE_CLIENT_ID,
            FAKE_ACCESS_TOKEN,
            1,
            20,
            TTLock().__get_current_millis__()-10000,
        )
        TTLock(clientId=FAKE_CLIENT_ID
                    ,accessToken=FAKE_ACCESS_TOKEN).lock_electric_quantity()
        assert ttLock_error.error_code==INVALID_CURRENT_TIMESTAMP_ERROR
コード例 #4
0
def test_ttlock_get_lock_records_list_paginated():
    with requests_mock.Mocker() as m:
        m.register_uri('GET', re.compile(constants.LOCK_RECORDS_RESOURCE), text=response_lock_records_list_callback)
        response_generator = TTLock(clientId=FAKE_CLIENT_ID
                     ,accessToken=FAKE_ACCESS_TOKEN).get_lock_records_generator(lockId=1928723,pageSize=20)
        record_list = [record for record in response_generator]
        assert len(record_list)==80
コード例 #5
0
def test_ttlock_get_gateways_list_paginated():
    """Tests API call to get a gateways from a account""" 
    with requests_mock.Mocker() as m:
        m.register_uri('GET', re.compile(constants.GATEWAY_LIST_RESOURCE), text=response_gateway_list_callback)
        response_generator = TTLock(clientId=FAKE_CLIENT_ID
                     ,accessToken=FAKE_ACCESS_TOKEN).get_gateway_generator(pageSize=1)
        gateways = [gateway for gateway in response_generator]
        assert len(gateways)==2
コード例 #6
0
def createClients(broker, port, broker_user, broker_pass, ttlock_client,
                  ttlock_token, state_delay, battery_delay):
    ttlock = TTLock(ttlock_client, ttlock_token)
    ttlock2MqttClient = None
    for gateway in ttlock.get_gateway_generator():
        ttlock2MqttClient = TTLock2MQTTClientGateway(
            gateway, ttlock, broker, port, broker_user, broker_pass,
            battery_delay, DELAY_BETWEEN_LOCK_PUBLISH_INFOS * 2)
        create_futures(gateway.get(constants.GATEWAY_ID_FIELD),
                       ttlock2MqttClient)
        for lock in ttlock.get_locks_per_gateway_generator(
                gateway.get(constants.GATEWAY_ID_FIELD)):
            ttlock2MqttClient = TTLock2MQTTClientLock(
                lock, gateway, ttlock, broker, port, broker_user, broker_pass,
                state_delay, battery_delay,
                DELAY_BETWEEN_LOCK_PUBLISH_INFOS * 2)
            create_futures(lock.get(constants.LOCK_ID_FIELD),
                           ttlock2MqttClient)
コード例 #7
0
def test_ttlock_get_gateways_stop_iteration(mocker):
    """Tests API call to get a gateways from a account""" 
    with requests_mock.Mocker() as m:
        mocker.patch.object(TTLock, '__verify_page__') 
        TTLock.__verify_page__.return_value = False 
        m.register_uri('GET', re.compile(constants.GATEWAY_LIST_RESOURCE), text=response_gateway_list_callback)
        response_generator = TTLock(clientId=FAKE_CLIENT_ID
                   ,accessToken=FAKE_ACCESS_TOKEN).get_gateway_generator(pageSize=1)
        with pytest.raises(StopIteration): 
            next(response_generator) 
コード例 #8
0
def test_ttlock_get_locks_gateway_list():
    with open(MOCK_JSON_PATH+'gateway_lock_list_response.json', 'r') as json_file:
        mock_response = json_file.read() 
    with requests_mock.Mocker() as m:
        m.register_uri('GET', re.compile(constants.LOCKS_PER_GATEWAY_RESOURCE), text=mock_response)
        response_generator = TTLock(clientId=FAKE_CLIENT_ID
                     ,accessToken=FAKE_ACCESS_TOKEN).get_locks_per_gateway_generator(gatewayId=35155)
        lock_list = [lock for lock in response_generator]
    
    assert isinstance(lock_list, list)
    assert lock_list[0].get('lockId' )==3879122
    assert lock_list[1].get('lockId' )==1928723
コード例 #9
0
import sys
sys.path.append(".")

from ttlockwrapper import TTLock
""" 
To call this script:
- Copy this file to your computer
$ pip3 install ttlockio
$ python3 locks_states_from_account.py YOUR_TTLOCK_CLIENT_ID YOUR_TTLOCK_TOKEN
"""

if __name__ == "__main__":
    clientId = str(sys.argv[1])
    token = str(sys.argv[2])
    ttlock = TTLock(clientId, token)

    gateways = list(ttlock.get_gateway_generator())

    locks = []
    for gateway in gateways:
        locks += list(
            ttlock.get_locks_per_gateway_generator(gateway.get("gatewayId")))

    for lock in locks:
        print('Eletrict quantity: {}%'.format(
            ttlock.lock_electric_quantity(lock.get('lockId'))))
        print('Lock State: {}'.format(ttlock.lock_state(lock.get('lockId'))))
コード例 #10
0
def test_ttlock_get_lock_electric_quantity():
    with requests_mock.Mocker() as m:
        m.register_uri('GET', re.compile(constants.LOCK_ELECTRIC_QUANTITY_RESOURCE), text=LOCK_ELECTRIC_QUANTITY_RESPONSE)
        electric_quantity = TTLock(clientId=FAKE_CLIENT_ID
        ,accessToken=FAKE_ACCESS_TOKEN).lock_electric_quantity(lockId=1928723)
    assert electric_quantity == 68 
コード例 #11
0
def test_ttlock_get_lock_state():
    with requests_mock.Mocker() as m:
        m.register_uri('GET', re.compile(constants.LOCK_STATE_RESOURCE), text=LOCK_STATE_RESPONSE)
        state = TTLock(clientId=FAKE_CLIENT_ID
        ,accessToken=FAKE_ACCESS_TOKEN).lock_state(lockId=1928723)
    assert state == 1 
コード例 #12
0
def test_unlock():
    with requests_mock.Mocker() as m:
        m.register_uri('GET', re.compile(constants.UNLOCK_RESOURCE), text='{"errcode": 0,"errmsg": "none error message or means yes","description": "表示成功或是"}')
        assert TTLock(clientId=FAKE_CLIENT_ID
        ,accessToken=FAKE_ACCESS_TOKEN).unlock(lockId=1928723)