Skip to content

Commit

Permalink
Merge pull request #51 from itchannel/1.0.8-Update
Browse files Browse the repository at this point in the history
1.0.8 update
  • Loading branch information
itchannel authored Dec 3, 2020
2 parents e18d365 + 074ab62 commit 4395f4f
Show file tree
Hide file tree
Showing 10 changed files with 107 additions and 22 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ I have added a service to poll the car for updates, due to the battery drain I h
###
Click on options and choose imperial or metric to display in km/miles. Takes effect on next restart of home assistant. Default is Metric

### Clear Tokens
If you are experiencing any sign in issues, please trying clearing your tokens using the "clear_tokens" service call.



## Currently Working
Expand All @@ -40,6 +43,7 @@ Click on options and choose imperial or metric to display in km/miles. Takes eff
- Last Car Refresh status
- Car Tracker
- Supports Multiple Regions
- Electric Vehicle Support


## Coming Soon
Expand Down
21 changes: 21 additions & 0 deletions custom_components/fordpass/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,22 @@ async def async_refresh_status_service(service_call):
refresh_status, hass, service_call, coordinator
)

async def async_clear_tokens_service(service_call):
await hass.async_add_executor_job(clear_tokens, hass, service_call, coordinator)

async def async_clear_tokens_service(service_call):
await hass.async_add_executor_job(clear_tokens, hass, service_call, coordinator)

hass.services.async_register(
DOMAIN,
"refresh_status",
async_refresh_status_service,
)
hass.services.async_register(
DOMAIN,
"clear_tokens",
async_clear_tokens_service,
)

return True

Expand All @@ -88,6 +99,16 @@ def refresh_status(service, hass, coordinator):
coordinator.vehicle.requestUpdate()


def clear_tokens(service, hass, coordinator):
_LOGGER.debug("Clearing Tokens")
coordinator.vehicle.clearToken()


def clear_tokens(service, hass, coordinator):
_LOGGER.debug("Clearing Tokens")
coordinator.vehicle.clearToken()


async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry):
"""Unload a config entry."""
unload_ok = all(
Expand Down
15 changes: 15 additions & 0 deletions custom_components/fordpass/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,18 @@
REGION = "region"

REGION_OPTIONS = ["UK&Europe", "Australia", "North America & Canada"]

SENSORS = {
"odometer": {"icon": "mdi:counter"},
"fuel": {"icon": "mdi:gas-station"},
"battery": {"icon": "mdi:car-battery"},
"oil": {"icon": "mdi:oil"},
"tirePressure": {"icon": "mdi:car-tire-alert"},
"gps": {"icon": "mdi:radar"},
"alarm": {"icon": "mdi:bell"},
"ignitionStatus": {"icon": "hass:power"},
"doorStatus": {"icon": "mdi:car-door"},
"windowPosition": {"icon": "mdi:car-door"},
"lastRefresh": {"icon": "mdi:clock"},
"elVeh": {"icon": "mdi:ev-station"},
}
4 changes: 4 additions & 0 deletions custom_components/fordpass/device_tracker.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,7 @@ def device_id(self):
@property
def device_state_attributes(self):
return self.coordinator.data[self.sensor].items()

@property
def icon(self):
return "mdi:radar"
14 changes: 10 additions & 4 deletions custom_components/fordpass/fordpass_new.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import json
import logging
import os.path
import os
import time

import requests
Expand Down Expand Up @@ -144,6 +144,12 @@ def readToken(self):
with open("/tmp/fordpass_token.txt") as token_file:
return json.load(token_file)

def clearToken(self):
if os.path.isfile("/tmp/fordpass_token.txt"):
os.remove("/tmp/fordpass_token.txt")
if os.path.isfile("/tmp/token.txt"):
os.remove("/tmp/token.txt")

def status(self):
# Get the status of the vehicle

Expand Down Expand Up @@ -214,10 +220,10 @@ def __makeRequest(self, method, url, data, params):
"""

headers = {
**apiHeaders,
**apiHeaders,
"auth-token": self.token,
"Application-Id": self.region
}
"Application-Id": self.region,
}

return getattr(requests, method.lower())(
url, headers=headers, data=data, params=params
Expand Down
4 changes: 4 additions & 0 deletions custom_components/fordpass/lock.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,7 @@ def is_locked(self):
if self.coordinator.data is None or self.coordinator.data["lockStatus"] is None:
return None
return self.coordinator.data["lockStatus"]["value"] == "LOCKED"

@property
def icon(self):
return "mdi:car-door-lock"
55 changes: 37 additions & 18 deletions custom_components/fordpass/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,17 @@
from homeassistant.util import Throttle

from . import FordPassEntity
from .const import CONF_UNIT, DOMAIN
from .const import CONF_UNIT, DOMAIN, SENSORS

_LOGGER = logging.getLogger(__name__)


async def async_setup_entry(hass, config_entry, async_add_entities):
"""Add the Entities from the config."""
entry = hass.data[DOMAIN][config_entry.entry_id]
snrarray = [
"odometer",
"fuel",
"battery",
"oil",
"tirePressure",
"gps",
"alarm",
"ignitionStatus",
"doorStatus",
"windowPosition",
"lastRefresh",
]
sensors = []
for snr in snrarray:
async_add_entities([CarSensor(entry, snr, config_entry.options)], True)
for key, value in SENSORS.items():
async_add_entities([CarSensor(entry, key, config_entry.options)], True)


class CarSensor(
Expand Down Expand Up @@ -68,7 +55,7 @@ def get_value(self, ftype):
return self.coordinator.data[self.sensor]["value"]
elif self.sensor == "doorStatus":
for key, value in self.coordinator.data[self.sensor].items():
if value["value"] != "Closed":
if (value["value"] != "Closed") or (value["value"] != "Invalid"):
return "Open"
return "Closed"
elif self.sensor == "windowPosition":
Expand All @@ -80,6 +67,11 @@ def get_value(self, ftype):
return "Closed"
elif self.sensor == "lastRefresh":
return self.coordinator.data[self.sensor]
elif self.sensor == "elVeh":
if self.coordinator.data["elVehDTE"] != None:
return self.coordinator.data["chargingStatus"]["value"]
else:
return "Unsupported"
elif ftype == "measurement":
if self.sensor == "odometer":
if self.options[CONF_UNIT] == "imperial":
Expand Down Expand Up @@ -112,7 +104,11 @@ def get_value(self, ftype):
elif self.sensor == "fuel":
return self.coordinator.data[self.sensor].items()
elif self.sensor == "battery":
return None
return {
"Battery Voltage": self.coordinator.data[self.sensor][
"batteryStatusActual"
]["value"]
}
elif self.sensor == "oil":
return self.coordinator.data[self.sensor].items()
elif self.sensor == "tirePressure":
Expand All @@ -137,6 +133,25 @@ def get_value(self, ftype):
return windows
elif self.sensor == "lastRefresh":
return None
elif self.sensor == "elVeh":
if self.coordinator.data["elVehDTE"] != None:
return {
"Plug Status": self.coordinator.data["plugStatus"]["value"],
"Charge Start Time": self.coordinator.data["chargeStartTime"][
"value"
],
"Charge End Time": self.coordinator.data["chargeEndTime"][
"value"
],
"Battery Fill Level": self.coordinator.data["batteryFillLevel"][
"value"
],
"Charger Power Type": self.coordinator.data["chargerPowertype"][
"value"
],
}
else:
return None

@property
def name(self):
Expand All @@ -157,3 +172,7 @@ def device_state_attributes(self):
@property
def unit_of_measurement(self):
return self.get_value("measurement")

@property
def icon(self):
return SENSORS[self.sensor]["icon"]
2 changes: 2 additions & 0 deletions custom_components/fordpass/services.yaml
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
refresh_status:
description: "Poll car for latest status (Takes up to 5mins to update once this function has been run!)"
clear_tokens:
description: "Clear the cached tokens"
4 changes: 4 additions & 0 deletions custom_components/fordpass/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,7 @@ def is_on(self):
):
return None
return self.coordinator.data["remoteStartStatus"]["value"]

@property
def icon(self):
return "mdi:key-star"
6 changes: 6 additions & 0 deletions info.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@

# **Changelog**
### Version 1.08
- Added Icons for each entity
- Added "clear_tokens" service call
- Added Electric Vehicle features
- Fixed "Invalid" lock status

### Version 1.07
- Support for multiple regions (Fixes unavaliable bug)
- Token renamed to fordpass_token
Expand Down

0 comments on commit 4395f4f

Please sign in to comment.