async def main(user, passw, url_name): print('Trying to connect with user ' + user) async with aiohttp.ClientSession() as sess: connector = Connector(user, passw, sess) try: await connector.login(True) print('Login successful') except ApiError as err: print(err.message) print(err.response) print(err.status) facilities = await connector.get(urls.facilities_list()) serial = mapper.map_serial_number(facilities) url_method = getattr(urls, url_name) url = url_method(**{'serial': serial}) print(url) try: print(json.dumps(await connector.get(url))) except ApiError as err: print(err.message) print(err.response) print(err.status)
async def test_serial_not_fixed_relogin(session: ClientSession, connector: Connector, resp: aioresponses) -> None: manager = SystemManager("user", "pass", session, "pymultiMATIC") with open(path("files/responses/zone"), "r") as file: raw_zone = json.loads(file.read()) with open(path("files/responses/facilities"), "r") as file: facilities = json.loads(file.read()) facilities["body"]["facilitiesList"][0]["serialNumber"] = "123" url_zone1 = urls.zone(serial=SERIAL, id="zone") url_zone2 = urls.zone(serial="123", id="zone") url_facilities = urls.facilities_list(serial=SERIAL) resp.get(url_zone1, payload=raw_zone, status=200) resp.get(url_zone2, payload=raw_zone, status=200) resp.get(url_facilities, payload=facilities, status=200) mock_auth(resp) await manager.get_zone("zone") assert manager._serial == SERIAL assert not manager._fixed_serial connector._clear_cookies() await manager.get_zone("zone") assert manager._serial == "123"
async def test_get_facility_detail_other_serial(manager: SystemManager, resp: aioresponses) -> None: url = urls.facilities_list(serial=SERIAL, ) with open(path("files/responses/facilities_multiple"), "r") as file: json_raw = json.loads(file.read()) key = None for match in resp._matches.items(): if match[1].url_or_pattern.path in urls.facilities_list(): key = match[0] resp._matches.pop(key) resp.get(url, status=200, payload=json_raw) details = await manager.get_facility_detail("888") assert details.serial_number == "888" _assert_calls(1, manager, [url])
async def test_get_facility_detail_no_serial(manager: SystemManager, resp: aioresponses) -> None: url = urls.facilities_list(serial=SERIAL, ) with open(path("files/responses/facilities"), "r") as file: json_raw = json.loads(file.read()) resp.get(url, status=200, payload=json_raw) details = await manager.get_facility_detail() assert details.serial_number == SERIAL _assert_calls(1, manager, [url])
def _mock_urls(resp: aioresponses, hvacstate_data: Any, livereport_data: Any, rooms_data: Any, system_data: Any, facilities: Any = None, gateway: Any = None) -> None: resp.get(urls.live_report(serial=SERIAL), payload=livereport_data, status=200) resp.get(urls.rooms(serial=SERIAL), payload=rooms_data, status=200) resp.get(urls.system(serial=SERIAL), payload=system_data, status=200) resp.get(urls.hvac(serial=SERIAL), payload=hvacstate_data, status=200) if facilities: resp.get(urls.facilities_list(), payload=facilities, status=200) if gateway: resp.get(urls.gateway_type(serial=SERIAL), payload=gateway, status=200)
async def main(user, passw): print('Trying to connect with user ' + user) async with aiohttp.ClientSession() as sess: shutil.rmtree('./dump_result', ignore_errors=True) os.mkdir('./dump_result') connector = Connector(user, passw, sess) try: await connector.login(True) print('Login successful') except ApiError as err: print('Cannot login: '******'requesting ' + url.__name__) req = connector.get(url(**{'serial': serial})) requests.update({url.__name__: req}) print('did {} requests'.format(len(requests))) responses = {} for key in requests: try: responses.update({key: await requests[key]}) except: print('Cannot get response for {}, skipping it'.format(key)) print('received {} responses'.format(len(responses))) for key in responses: try: with open('./dump_result/{}.json'.format(key), 'w+') as file: data = json.dumps(responses[key])\ .replace(serial, 'SERIAL_NUMBER') json.dump(json.loads(data), file, indent=4) except: print('cannot write to file {}'.format(file.name))
async def test_get(connector: Connector, resp: aioresponses) -> None: url = urls.facilities_list(serial="123") resp.get(url=url, status=200) await connector.get(url)
async def test_delete(connector: Connector, resp: aioresponses) -> None: url = urls.facilities_list(serial='123') resp.delete(url=url, status=200) await connector.delete(url)
async def fixture_resp( resp: aioresponses) -> AsyncGenerator[aioresponses, None]: with open(path("files/responses/facilities"), "r") as file: facilities = json.loads(file.read()) resp.get(urls.facilities_list(), payload=facilities, status=200) yield resp