├── .gitignore ├── LICENSE.md ├── README.md ├── fordpass ├── __init__.py ├── bin │ └── demo.py └── fordpass.py └── setup.py /.gitignore: -------------------------------------------------------------------------------- 1 | 2 | # Created by https://www.toptal.com/developers/gitignore/api/python 3 | # Edit at https://www.toptal.com/developers/gitignore?templates=python 4 | 5 | ### Python ### 6 | # Byte-compiled / optimized / DLL files 7 | __pycache__/ 8 | *.py[cod] 9 | *$py.class 10 | 11 | # C extensions 12 | *.so 13 | 14 | # Distribution / packaging 15 | .Python 16 | build/ 17 | develop-eggs/ 18 | dist/ 19 | downloads/ 20 | eggs/ 21 | .eggs/ 22 | lib/ 23 | lib64/ 24 | parts/ 25 | sdist/ 26 | var/ 27 | wheels/ 28 | pip-wheel-metadata/ 29 | share/python-wheels/ 30 | *.egg-info/ 31 | .installed.cfg 32 | *.egg 33 | MANIFEST 34 | 35 | # PyInstaller 36 | # Usually these files are written by a python script from a template 37 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 38 | *.manifest 39 | *.spec 40 | 41 | # Installer logs 42 | pip-log.txt 43 | pip-delete-this-directory.txt 44 | 45 | # Unit test / coverage reports 46 | htmlcov/ 47 | .tox/ 48 | .nox/ 49 | .coverage 50 | .coverage.* 51 | .cache 52 | nosetests.xml 53 | coverage.xml 54 | *.cover 55 | *.py,cover 56 | .hypothesis/ 57 | .pytest_cache/ 58 | pytestdebug.log 59 | 60 | # Translations 61 | *.mo 62 | *.pot 63 | 64 | # Django stuff: 65 | *.log 66 | local_settings.py 67 | db.sqlite3 68 | db.sqlite3-journal 69 | 70 | # Flask stuff: 71 | instance/ 72 | .webassets-cache 73 | 74 | # Scrapy stuff: 75 | .scrapy 76 | 77 | # Sphinx documentation 78 | docs/_build/ 79 | doc/_build/ 80 | 81 | # PyBuilder 82 | target/ 83 | 84 | # Jupyter Notebook 85 | .ipynb_checkpoints 86 | 87 | # IPython 88 | profile_default/ 89 | ipython_config.py 90 | 91 | # pyenv 92 | .python-version 93 | 94 | # pipenv 95 | # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. 96 | # However, in case of collaboration, if having platform-specific dependencies or dependencies 97 | # having no cross-platform support, pipenv may install dependencies that don't work, or not 98 | # install all needed dependencies. 99 | #Pipfile.lock 100 | 101 | # PEP 582; used by e.g. github.com/David-OConnor/pyflow 102 | __pypackages__/ 103 | 104 | # Celery stuff 105 | celerybeat-schedule 106 | celerybeat.pid 107 | 108 | # SageMath parsed files 109 | *.sage.py 110 | 111 | # Environments 112 | .env 113 | .venv 114 | env/ 115 | venv/ 116 | ENV/ 117 | env.bak/ 118 | venv.bak/ 119 | pythonenv* 120 | 121 | # Spyder project settings 122 | .spyderproject 123 | .spyproject 124 | 125 | # Rope project settings 126 | .ropeproject 127 | 128 | # mkdocs documentation 129 | /site 130 | 131 | # mypy 132 | .mypy_cache/ 133 | .dmypy.json 134 | dmypy.json 135 | 136 | # Pyre type checker 137 | .pyre/ 138 | 139 | # pytype static type analyzer 140 | .pytype/ 141 | 142 | # profiling data 143 | .prof 144 | 145 | # End of https://www.toptal.com/developers/gitignore/api/python -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Dave Clarke 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![PyPI version](https://badge.fury.io/py/fordpass.svg)](https://badge.fury.io/py/fordpass) 2 | 3 | # fordpass-python 4 | 5 | This is a basic Python wrapper around the FordPass APIs. The wrapper provides methods to return vehicle status as well as some basic commands, e.g. start/stop, lock/unlock. 6 | 7 | It's more or less a straight port of @d4v3y0rk's NPM module [d4v3y0rk/ffpass](https://github.com/d4v3y0rk/ffpass-module) - props to him for his work figuring out the relevant API requests needed. 8 | 9 | ## Features 10 | 11 | * Automatically auth & re-fetches tokens once expired 12 | * Get status of the vehicle (this returns a ton of info about the car: lat/long, oil, battery, fuel, odometer, tire pressures, open windows and a bunch of other stuff that may/may not apply to your car, e.g. charge level, diesel filters.) 13 | * Start the engine (if supported) 14 | * Stop the engine (if supported) 15 | * Lock the doors 16 | * Unlock the doors 17 | 18 | ## Install 19 | Install using pip: 20 | 21 | ``` 22 | pip install fordpass 23 | ``` 24 | 25 | ## Demo 26 | 27 | To test the libary there is a demo script `demo.py`. 28 | 29 | ``` 30 | demo.py USERNAME PASSWORD VIN 31 | ``` 32 | 33 | e.g. 34 | 35 | ``` 36 | demo.py test@test.com mypassword WX12345678901234 37 | ``` 38 | 39 | ## Publishing new versions of this package 40 | 41 | 1. Bump the version number inside `setup.py`. 42 | 2. Build the package: `python setup.py sdist bdist_wheel`. 43 | 3. Upload to TestPyPi using `twine upload --repository-url https://test.pypi.org/legacy/ dist/*` and verify everything is as expected. 44 | 4. Upload to PyPi using `twine upload dist/*`. 45 | 5. All done! -------------------------------------------------------------------------------- /fordpass/__init__.py: -------------------------------------------------------------------------------- 1 | from .fordpass import Vehicle -------------------------------------------------------------------------------- /fordpass/bin/demo.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | """ 4 | Simple script to demo the API 5 | """ 6 | 7 | import sys, os, logging, time 8 | from fordpass import Vehicle 9 | 10 | if __name__ == "__main__": 11 | 12 | if len(sys.argv) != 4: 13 | raise Exception('Must specify Username, Password and VIN as arguments, e.g. demo.py test@test.com password123 WX231231232') 14 | else: 15 | r = Vehicle(sys.argv[1], sys.argv[2], sys.argv[3]) # Username, Password, VIN 16 | 17 | print(r.status()) # Print the status of the car 18 | 19 | # r.unlock() # Unlock the doors 20 | 21 | # time.sleep(10) # Wait 10 seconds 22 | 23 | # r.lock() # Lock the doors -------------------------------------------------------------------------------- /fordpass/fordpass.py: -------------------------------------------------------------------------------- 1 | import requests 2 | import logging 3 | import time 4 | 5 | defaultHeaders = { 6 | 'Accept': '*/*', 7 | 'Accept-Language': 'en-us', 8 | 'User-Agent': 'fordpass-na/353 CFNetwork/1121.2.2 Darwin/19.3.0', 9 | 'Accept-Encoding': 'gzip, deflate, br', 10 | } 11 | 12 | apiHeaders = { 13 | **defaultHeaders, 14 | 'Application-Id': '71A3AD0A-CF46-4CCF-B473-FC7FE5BC4592', 15 | 'Content-Type': 'application/json', 16 | } 17 | 18 | baseUrl = 'https://usapi.cv.ford.com/api' 19 | 20 | class Vehicle(object): 21 | '''Represents a Ford vehicle, with methods for status and issuing commands''' 22 | 23 | def __init__(self, username, password, vin): 24 | self.username = username 25 | self.password = password 26 | self.vin = vin 27 | self.token = None 28 | self.expires = None 29 | 30 | def auth(self): 31 | '''Authenticate and store the token''' 32 | 33 | data = { 34 | 'client_id': '9fb503e0-715b-47e8-adfd-ad4b7770f73b', 35 | 'grant_type': 'password', 36 | 'username': self.username, 37 | 'password': self.password 38 | } 39 | 40 | headers = { 41 | **defaultHeaders, 42 | 'Content-Type': 'application/x-www-form-urlencoded' 43 | } 44 | 45 | r = requests.post('https://fcis.ice.ibmcloud.com/v1.0/endpoint/default/token', data=data, headers=headers) 46 | 47 | if r.status_code == 200: 48 | logging.info('Succesfully fetched token') 49 | result = r.json() 50 | self.token = result['access_token'] 51 | self.expiresAt = time.time() + result['expires_in'] 52 | return True 53 | else: 54 | r.raise_for_status() 55 | 56 | def __acquireToken(self): 57 | '''Fetch and refresh token as needed''' 58 | 59 | if (self.token == None) or (time.time() >= self.expiresAt): 60 | logging.info('No token, or has expired, requesting new token') 61 | self.auth() 62 | else: 63 | logging.info('Token is valid, continuing') 64 | pass 65 | 66 | def status(self): 67 | '''Get the status of the vehicle''' 68 | 69 | self.__acquireToken() 70 | 71 | params = { 72 | 'lrdt': '01-01-1970 00:00:00' 73 | } 74 | 75 | headers = { 76 | **apiHeaders, 77 | 'auth-token': self.token 78 | } 79 | 80 | r = requests.get(f'{baseUrl}/vehicles/v4/{self.vin}/status', params=params, headers=headers) 81 | 82 | if r.status_code == 200: 83 | result = r.json() 84 | return result['vehiclestatus'] 85 | else: 86 | r.raise_for_status() 87 | 88 | def start(self): 89 | ''' 90 | Issue a start command to the engine 91 | ''' 92 | return self.__requestAndPoll('PUT', f'{baseUrl}/vehicles/v2/{self.vin}/engine/start') 93 | 94 | def stop(self): 95 | ''' 96 | Issue a stop command to the engine 97 | ''' 98 | return self.__requestAndPoll('DELETE', f'{baseUrl}/vehicles/v2/{self.vin}/engine/start') 99 | 100 | 101 | def lock(self): 102 | ''' 103 | Issue a lock command to the doors 104 | ''' 105 | return self.__requestAndPoll('PUT', f'{baseUrl}/vehicles/v2/{self.vin}/doors/lock') 106 | 107 | 108 | def unlock(self): 109 | ''' 110 | Issue an unlock command to the doors 111 | ''' 112 | return self.__requestAndPoll('DELETE', f'{baseUrl}/vehicles/v2/{self.vin}/doors/lock') 113 | 114 | def __makeRequest(self, method, url, data, params): 115 | ''' 116 | Make a request to the given URL, passing data/params as needed 117 | ''' 118 | 119 | headers = { 120 | **apiHeaders, 121 | 'auth-token': self.token 122 | } 123 | 124 | return getattr(requests, method.lower())(url, headers=headers, data=data, params=params) 125 | 126 | def __pollStatus(self, url, id): 127 | ''' 128 | Poll the given URL with the given command ID until the command is completed 129 | ''' 130 | status = self.__makeRequest('GET', f'{url}/{id}', None, None) 131 | result = status.json() 132 | if result['status'] == 552: 133 | logging.info('Command is pending') 134 | time.sleep(5) 135 | return self.__pollStatus(url, id) # retry after 5s 136 | elif result['status'] == 200: 137 | logging.info('Command completed succesfully') 138 | return True 139 | else: 140 | logging.info('Command failed') 141 | return False 142 | 143 | def __requestAndPoll(self, method, url): 144 | self.__acquireToken() 145 | command = self.__makeRequest(method, url, None, None) 146 | 147 | if command.status_code == 200: 148 | result = command.json() 149 | return self.__pollStatus(url, result['commandId']) 150 | else: 151 | command.raise_for_status() 152 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | from setuptools import setup 2 | 3 | with open("README.md", "r") as fh: 4 | long_description = fh.read() 5 | 6 | setup( 7 | name='fordpass', 8 | version='0.0.5', 9 | author="Dave Clarke", 10 | author_email="info@daveclarke.me", 11 | description="Python wrapper for the FordPass API for Ford vehicle information and control: start, stop, lock, unlock.", 12 | long_description=long_description, 13 | long_description_content_type="text/markdown", 14 | url="https://github.com/clarkd/fordpass-python", 15 | license="MIT", 16 | packages=['fordpass'], 17 | scripts=['fordpass/bin/demo.py'], 18 | classifiers=[ 19 | "Programming Language :: Python :: 3.9", 20 | "Programming Language :: Python :: 3.8", 21 | "Programming Language :: Python :: 3.7", 22 | "Programming Language :: Python :: 3", 23 | "License :: OSI Approved :: MIT License", 24 | "Operating System :: OS Independent", 25 | ], 26 | install_requires=['requests'] 27 | ) --------------------------------------------------------------------------------