Skip to content

Commit

Permalink
修改HTTP头以及增加一个维保记录查询接口
Browse files Browse the repository at this point in the history
  • Loading branch information
georgezhao2010 authored Jun 26, 2022
1 parent ff7c4e6 commit 69a291e
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 28 deletions.
27 changes: 12 additions & 15 deletions custom_components/fordpass_china/ford/fordpass.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,11 @@

DEFAULT_HEADERS = {
"Accept": "*/*",
"Accept-Language": "en-us",
"Accept-Language": "zh-CN,zh-Hans;q=0.9",
"User-Agent": "fordpass-cn/320 CFNetwork/1331.0.7 Darwin/21.4.0",
"Accept-Encoding": "gzip, deflate, br",
"authorization": "Basic ZWFpLWNsaWVudDo=",
"Accept-Encoding": "gzip, deflate",
}

'''
The old application IDs
APPLICATION_ID = {
"Ford": "46409D04-BD1B-40C6-9D51-13A52666E9F9",
"Lincoln": "D14B573E-4C11-404B-89AD-DC1EF8C34C28"
}
'''


APPLICATION_ID = {
"ford": "35F9024B-010E-4FE7-B202-62D941F8681C",
"lincoln": "5EE5E683-1B71-4D6B-BAA8-F344D6672796"
Expand All @@ -42,6 +31,7 @@ class CommandResult(str, Enum):
PENDING = "pending"
FAILED = "failed"


class FordPass(object):
def __init__(self, session: ClientSession, username: str = None, password: str = None,
vehicle_type: str = "ford", refresh_token: str = None):
Expand Down Expand Up @@ -145,14 +135,15 @@ async def check_token(self):
await self.auth()
return self._token is not None

async def safe_call_api(self, url, method="get"):
async def safe_call_api(self, url, method="get", headers=None):
code = -1
response = None
if await self.check_token():
code, response = await self.call_api(
url=url,
method=method,
headers=self.make_api_header())
headers=dict(headers.items(), **self.make_api_header()) if headers else self.make_api_header()
)
return code, response

async def get_user_info(self):
Expand Down Expand Up @@ -185,6 +176,12 @@ async def get_vehicle_auth_status(self, vin):
)
return response["vehicleAuthorizationStatus"]["authorization"] if code == 200 else None

async def get_vehicle_service_history(self, vin):
code, response = await self.safe_call_api(
url=f"{API_URL}/api/servicehistory/v1/service-history?vin={vin}"
)
return response if code == 200 else None

async def _send_command(self, url, method):
_LOGGER.debug(f"Send command URL:{url}, method:{method}")
code, response = await self.safe_call_api(
Expand Down
34 changes: 21 additions & 13 deletions custom_components/fordpass_china/vehicle.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ def __init__(self, hass, fordpass: FordPass, vehicle_info, update_interval):
self._model = vehicle_info["modelName"]
self._year = vehicle_info["modelYear"]
self._vehicle_name = vehicle_info["nickName"]
self._commandid = None

async def _async_update_data(self):
_LOGGER.debug("Data updating...")
Expand Down Expand Up @@ -96,18 +97,25 @@ def year(self) -> str:
def vehicle_name(self) -> str:
return self._vehicle_name

async def _check_command(self, commandid, end_point):
while True:
result = await self._fordpass.async_get_switch_completed(self.vin, end_point, commandid)
if result == CommandResult.PENDING:
await asyncio.sleep(1)
continue
else:
if result == CommandResult.SUCCESS:
await self.async_refresh()
return
async def _check_command(self, end_point):
try:
while True:
result = await self._fordpass.async_get_switch_completed(self.vin, end_point, self._commandid)
if result == CommandResult.PENDING:
await asyncio.sleep(1)
continue
else:
if result == CommandResult.SUCCESS:
await self.async_refresh()
break
except Exception:
pass
self._commandid = None

async def async_set_switch(self, end_point, turn_on):
commandid = await self._fordpass.async_set_switch(self.vin, end_point, turn_on)
if commandid:
self.hass.loop.create_task(self._check_command(commandid, end_point))
if not self._commandid:
self._commandid = await self._fordpass.async_set_switch(self.vin, end_point, turn_on)
if self._commandid:
self.hass.loop.create_task(self._check_command(end_point))
else:
_LOGGER.warning(f"Last command id={self._commandid} is pending")

0 comments on commit 69a291e

Please sign in to comment.