Skip to content

Commit

Permalink
Merge pull request #14 from itchannel/active_dev
Browse files Browse the repository at this point in the history
Active dev
  • Loading branch information
itchannel authored Nov 4, 2020
2 parents 11d35d8 + bff78bb commit dc83f80
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 50 deletions.
1 change: 1 addition & 0 deletions custom_components/fordpass/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@
"homekit": {},
"dependencies": [],
"codeowners": ["@itchannel"]

}

123 changes: 73 additions & 50 deletions custom_components/fordpass/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
from . import FordPassEntity
from .const import DOMAIN
from datetime import timedelta
from homeassistant.util import Throttle


_LOGGER = logging.getLogger(__name__)
SCAN_INTERVAL = timedelta(seconds=300)


async def async_setup_entry(hass, config_entry, async_add_entities):
"""Add the Entities from the config."""
Expand All @@ -24,56 +25,78 @@ async def async_setup_entry(hass, config_entry, async_add_entities):

class CarSensor(FordPassEntity,Entity):
def __init__(self, coordinator, sensor):

self.sensor = sensor
self._attr = {}
self.coordinator = coordinator
self._device_id = "fordpass_" + sensor
self._state = None
self._measurement = None

async def async_update(self):
await self.coordinator.async_request_refresh()
if self.coordinator.data is None or self.coordinator.data[self.sensor] is None:
return None

if self.sensor == "odometer":
self._state = self.coordinator.data[self.sensor]["value"]
self._measurement = "km"
for key, value in self.coordinator.data[self.sensor].items():
self._attr[key] = value
elif self.sensor == "fuel":
self._state = self.coordinator.data[self.sensor]["fuelLevel"]
self._measurement = "L"
for key, value in self.coordinator.data[self.sensor].items():
self._attr[key] = value
elif self.sensor == "battery":
self._state = self.coordinator.data[self.sensor]["batteryHealth"]["value"]
elif self.sensor == "oil":
self._state = self.coordinator.data[self.sensor]["oilLife"]
for key, value in self.coordinator.data[self.sensor].items():
self._attr[key] = value
elif self.sensor == "tirePressure":
self._state = self.coordinator.data[self.sensor]["value"]
for key, value in self.coordinator.data[self.sensor].items():
self._attr[key] = value
elif self.sensor == "gps":
self._state = self.coordinator.data[self.sensor]["gpsState"]
for key, value in self.coordinator.data[self.sensor].items():
self._attr[key] = value
elif self.sensor == "alarm":
self._state = self.coordinator.data[self.sensor]["value"]
for key, value in self.coordinator.data[self.sensor].items():
self._attr[key] = value
elif self.sensor == "ignitionStatus":
self._state = self.coordinator.data[self.sensor]["value"]
for key, value in self.coordinator.data[self.sensor].items():
self._attr[key] = value
elif self.sensor == "doorStatus":
self._state = "Closed"
for key,value in self.coordinator.data[self.sensor].items():
if value["value"] != "Closed":
self._state = "Open"
self._attr[key] = value["value"]


def get_value(self, ftype):
if ftype == "state":
if self.sensor == "odometer":
return self.coordinator.data[self.sensor]["value"]
elif self.sensor == "fuel":
return self.coordinator.data[self.sensor]["fuelLevel"]
elif self.sensor == "battery":
return self.coordinator.data[self.sensor]["batteryHealth"]["value"]
elif self.sensor == "oil":
return self.coordinator.data[self.sensor]["oilLife"]
elif self.sensor == "tirePressure":
return self.coordinator.data[self.sensor]["value"]
elif self.sensor == "gps":
return self.coordinator.data[self.sensor]["gpsState"]
elif self.sensor == "alarm":
return self.coordinator.data[self.sensor]["value"]
elif self.sensor == "ignitionStatus":
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":
return "Open"
return "Closed"
elif ftype == "measurement":
if self.sensor == "odometer":
return "km"
elif self.sensor == "fuel":
return "L"
elif self.sensor == "battery":
return None
elif self.sensor == "oil":
return None
elif self.sensor == "tirePressure":
return None
elif self.sensor == "gps":
return None
elif self.sensor == "alarm":
return None
elif self.sensor == "ignitionStatus":
return None
elif self.sensor == "doorStatus":
return None
elif ftype == "attribute":
if self.sensor == "odometer":
return self.coordinator.data[self.sensor].items()
elif self.sensor == "fuel":
return self.coordinator.data[self.sensor].items()
elif self.sensor == "battery":
return None
elif self.sensor == "oil":
return self.coordinator.data[self.sensor].items()
elif self.sensor == "tirePressure":
return None
elif self.sensor == "gps":
return self.coordinator.data[self.sensor].items()
elif self.sensor == "alarm":
return self.coordinator.data[self.sensor].items()
elif self.sensor == "ignitionStatus":
return self.coordinator.data[self.sensor].items()
elif self.sensor == "doorStatus":
doors = dict()
for key,value in self.coordinator.data[self.sensor].items():
doors[key] = value["value"]
return doors




Expand All @@ -85,19 +108,19 @@ def name(self):

@property
def state(self):
return self._state
return self.get_value("state")

@property
def device_id(self):
return self.device_id

@property
def device_state_attributes(self):
return self._attr
return self.get_value("attribute")

@property
def unit_of_measurement(self):
return self._measurement
return self.get_value("measurement")



Expand Down

0 comments on commit dc83f80

Please sign in to comment.