async def async_setup_scanner(opp, config, async_see, discovery_info=None): """Validate the configuration and return a Traccar scanner.""" session = async_get_clientsession(opp, config[CONF_VERIFY_SSL]) api = API( opp.loop, session, config[CONF_USERNAME], config[CONF_PASSWORD], config[CONF_HOST], config[CONF_PORT], config[CONF_SSL], ) scanner = TraccarScanner( api, opp, async_see, config.get(CONF_SCAN_INTERVAL, SCAN_INTERVAL), config[CONF_MAX_ACCURACY], config[CONF_SKIP_ACCURACY_ON], config[CONF_MONITORED_CONDITIONS], config[CONF_EVENT], ) return await scanner.async_init()
async def async_setup_scanner( hass: HomeAssistant, config: ConfigType, async_see: Callable[..., Awaitable[None]], discovery_info: DiscoveryInfoType | None = None, ) -> bool: """Validate the configuration and return a Traccar scanner.""" session = async_get_clientsession(hass, config[CONF_VERIFY_SSL]) api = API( hass.loop, session, config[CONF_USERNAME], config[CONF_PASSWORD], config[CONF_HOST], config[CONF_PORT], config[CONF_SSL], ) scanner = TraccarScanner( api, hass, async_see, config.get(CONF_SCAN_INTERVAL, SCAN_INTERVAL), config[CONF_MAX_ACCURACY], config[CONF_SKIP_ACCURACY_ON], config[CONF_MONITORED_CONDITIONS], config[CONF_EVENT], ) return await scanner.async_init()
async def test_events(aresponses, event_loop, event_response): """Test events.""" aresponses.add( "example.com:7728", "/api/reports/events", "get", aresponses.Response(text=json.dumps(event_response), status=200, headers=HEADERS), ) async with aiohttp.ClientSession(loop=event_loop) as session: traccar = API(event_loop, session, TEST_USER, TEST_PASS, TEST_HOST, TEST_PORT) await traccar.get_events([1, 2]) assert isinstance(traccar.events, list) for event in traccar.events: assert isinstance(event["id"], int) assert isinstance(event["attributes"], dict) assert isinstance(event["deviceId"], int) assert isinstance(event["type"], str) assert isinstance(event["serverTime"], str) assert isinstance(event["positionId"], int) assert isinstance(event["geofenceId"], int) assert isinstance(event["maintenanceId"], int)
async def test(): """Example usage of pytraccar.""" async with aiohttp.ClientSession() as session: data = API(LOOP, session, 'admin', 'admin', HOST) await data.get_device_info() print("Device info:", data.device_info)
async def async_setup_scanner(hass, config, async_see, discovery_info=None): """Validate the configuration and return a Traccar scanner.""" from pytraccar.api import API session = async_get_clientsession(hass, config[CONF_VERIFY_SSL]) api = API(hass.loop, session, config[CONF_USERNAME], config[CONF_PASSWORD], config[CONF_HOST], config[CONF_PORT], config[CONF_SSL]) scanner = TraccarScanner(api, hass, async_see) return await scanner.async_init()
async def test_authentication_failed(aresponses, event_loop): """Test pypi stable.""" aresponses.add( "example.com:7728", "/api/devices", "get", aresponses.Response(status=401, headers=HEADERS), ) async with aiohttp.ClientSession(loop=event_loop) as session: traccar = API(event_loop, session, TEST_USER, TEST_PASS, TEST_HOST, TEST_PORT) await traccar.test_connection() assert not traccar.authenticated
async def test_device_info(aresponses, event_loop, positions_response, geofence_response, devices_response): """Test positions.""" aresponses.add( "example.com:7728", "/api/positions", "get", aresponses.Response(text=json.dumps(positions_response), status=200, headers=HEADERS), ) aresponses.add( "example.com:7728", "/api/geofences", "get", aresponses.Response(text=json.dumps(geofence_response), status=200, headers=HEADERS), ) aresponses.add( "example.com:7728", "/api/devices", "get", aresponses.Response(text=json.dumps(devices_response), status=200, headers=HEADERS), ) async with aiohttp.ClientSession(loop=event_loop) as session: traccar = API(event_loop, session, TEST_USER, TEST_PASS, TEST_HOST, TEST_PORT) await traccar.get_device_info() assert isinstance(traccar.device_info, dict) assert len(traccar.device_info) == 2 for device in traccar.device_info: assert isinstance(traccar.device_info[device]["address"], str) assert isinstance(traccar.device_info[device]["latitude"], float) assert isinstance(traccar.device_info[device]["longitude"], float) assert isinstance(traccar.device_info[device]["accuracy"], float) assert isinstance(traccar.device_info[device]["altitude"], float) assert isinstance(traccar.device_info[device]["course"], float) assert isinstance(traccar.device_info[device]["speed"], float) assert isinstance(traccar.device_info[device]["traccar_id"], int) assert isinstance(traccar.device_info[device]["device_id"], str) assert isinstance(traccar.device_info[device]["updated"], str) assert isinstance(traccar.device_info[device]["category"], str) assert isinstance(traccar.device_info[device]["battery"], float) assert isinstance(traccar.device_info[device]["motion"], bool) assert isinstance(traccar.device_info[device]["geofence"], str)
async def test_connection(aresponses, event_loop): """Test connection stable.""" aresponses.add( "example.com:7728", "/api/devices", "get", aresponses.Response(status=200), ) async with aiohttp.ClientSession(loop=event_loop) as session: traccar = API(event_loop, session, TEST_USER, TEST_PASS, TEST_HOST, TEST_PORT) await traccar.test_connection() assert traccar.connected
async def test_devices(aresponses, event_loop, devices_response): """Test devices.""" aresponses.add( "example.com:7728", "/api/devices", "get", aresponses.Response( text=json.dumps(devices_response), status=200, headers=HEADERS ), ) async with aiohttp.ClientSession(loop=event_loop) as session: traccar = API(event_loop, session, TEST_USER, TEST_PASS, TEST_HOST, TEST_PORT) await traccar.get_devices() assert isinstance(traccar.devices, list) assert len(traccar.devices) == 2
async def test(): """Example usage of pytraccar.""" async with aiohttp.ClientSession() as session: data = API(LOOP, session, "admin", "admin", HOST, PORT) await data.get_device_info() print() print("device_info:", data.device_info) print() print("positions:", data.positions) print() print("devices:", data.devices) print() print("geofences:", data.geofences) print() print("events:", data.events)
async def runcli(): """Debug of pytraccar.""" async with aiohttp.ClientSession() as session: host = input("IP: ") username = input("Username: "******"Password: "******"\n\n\n") data = API(LOOP, session, username, password, host) await data.test_connection() print("Authenticated:", data.authenticated) if data.authenticated: await data.get_device_info() print("Authentication:", data.authenticated) print("Geofences:", data.geofences) print("Devices:", data.devices) print("Positions:", data.positions) print("Device info:", data.device_info)
async def test_positions(aresponses, event_loop, positions_response): """Test positions.""" aresponses.add( "example.com:7728", "/api/positions", "get", aresponses.Response( text=json.dumps(positions_response), status=200, headers=HEADERS ), ) async with aiohttp.ClientSession(loop=event_loop) as session: traccar = API(event_loop, session, TEST_USER, TEST_PASS, TEST_HOST, TEST_PORT) await traccar.get_positions() for position in traccar.positions: assert isinstance(position["id"], int) assert isinstance(position["attributes"], dict) assert isinstance(position["deviceId"], int) assert isinstance(position["serverTime"], str) assert isinstance(position["latitude"], float) assert isinstance(position["longitude"], float) assert isinstance(position["address"], str)