diff --git a/custom_components/fordpass/manifest.json b/custom_components/fordpass/manifest.json index 8156a0e..bb7e883 100644 --- a/custom_components/fordpass/manifest.json +++ b/custom_components/fordpass/manifest.json @@ -10,5 +10,6 @@ "homekit": {}, "dependencies": [], "codeowners": ["@itchannel"] + } diff --git a/custom_components/fordpass/sensor.py b/custom_components/fordpass/sensor.py index 41f7f8d..0f861c1 100644 --- a/custom_components/fordpass/sensor.py +++ b/custom_components/fordpass/sensor.py @@ -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.""" @@ -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 + @@ -85,7 +108,7 @@ def name(self): @property def state(self): - return self._state + return self.get_value("state") @property def device_id(self): @@ -93,11 +116,11 @@ def device_id(self): @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")