From f1e9857a280972f8d22b93706626f01b3d5d2a14 Mon Sep 17 00:00:00 2001 From: Steve Date: Wed, 4 Nov 2020 10:31:48 +1000 Subject: [PATCH 1/3] modified manifest --- custom_components/fordpass/manifest.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/custom_components/fordpass/manifest.json b/custom_components/fordpass/manifest.json index b1c059b..ba4a474 100644 --- a/custom_components/fordpass/manifest.json +++ b/custom_components/fordpass/manifest.json @@ -9,7 +9,7 @@ "zeroconf": [], "homekit": {}, "dependencies": [], - "codeowners": ["@clarkd", "@itchannel"], + "codeowners": ["@itchannel"], "quality_scale": "silver" } \ No newline at end of file From 41dfa4cc3052eb91038c2402ca968f9f5b27acf3 Mon Sep 17 00:00:00 2001 From: Steve Date: Wed, 4 Nov 2020 20:49:30 +1000 Subject: [PATCH 2/3] Modified update throttle --- custom_components/fordpass/sensor.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/custom_components/fordpass/sensor.py b/custom_components/fordpass/sensor.py index 41f7f8d..90eb99e 100644 --- a/custom_components/fordpass/sensor.py +++ b/custom_components/fordpass/sensor.py @@ -5,10 +5,12 @@ 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) +TIME_BETWEEN_UPDATES = timedelta(seconds=300) async def async_setup_entry(hass, config_entry, async_add_entities): """Add the Entities from the config.""" @@ -16,7 +18,7 @@ async def async_setup_entry(hass, config_entry, async_add_entities): snrarray = [ "odometer", "fuel", "battery", "oil", "tirePressure", "gps", "alarm", "ignitionStatus", "doorStatus"] sensors = [] for snr in snrarray: - async_add_entities([CarSensor(entry, snr)], True) + async_add_entities([CarSensor(entry, snr)], False) @@ -31,6 +33,7 @@ def __init__(self, coordinator, sensor): self._state = None self._measurement = None + @Throttle(TIME_BETWEEN_UPDATES) 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: From c6e3a9bc015fa430cf248568b4c1bcd18a025ad1 Mon Sep 17 00:00:00 2001 From: Steve Date: Wed, 4 Nov 2020 23:43:50 +1000 Subject: [PATCH 3/3] Refactored data retrieval --- custom_components/fordpass/sensor.py | 126 ++++++++++++++++----------- 1 file changed, 73 insertions(+), 53 deletions(-) diff --git a/custom_components/fordpass/sensor.py b/custom_components/fordpass/sensor.py index 90eb99e..0f861c1 100644 --- a/custom_components/fordpass/sensor.py +++ b/custom_components/fordpass/sensor.py @@ -9,8 +9,7 @@ _LOGGER = logging.getLogger(__name__) -SCAN_INTERVAL = timedelta(seconds=300) -TIME_BETWEEN_UPDATES = timedelta(seconds=300) + async def async_setup_entry(hass, config_entry, async_add_entities): """Add the Entities from the config.""" @@ -18,7 +17,7 @@ async def async_setup_entry(hass, config_entry, async_add_entities): snrarray = [ "odometer", "fuel", "battery", "oil", "tirePressure", "gps", "alarm", "ignitionStatus", "doorStatus"] sensors = [] for snr in snrarray: - async_add_entities([CarSensor(entry, snr)], False) + async_add_entities([CarSensor(entry, snr)], True) @@ -26,57 +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 - - @Throttle(TIME_BETWEEN_UPDATES) - 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 + @@ -88,7 +108,7 @@ def name(self): @property def state(self): - return self._state + return self.get_value("state") @property def device_id(self): @@ -96,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")