From 06cebfebcac54775d4e09f9578f7e75ecf561e98 Mon Sep 17 00:00:00 2001 From: steve Date: Fri, 6 Jan 2023 20:36:01 +1000 Subject: [PATCH 1/3] Add DPF status --- custom_components/fordpass/const.py | 2 ++ custom_components/fordpass/manifest.json | 2 +- custom_components/fordpass/sensor.py | 32 ++++++++++++++++++++---- info.md | 2 ++ 4 files changed, 32 insertions(+), 6 deletions(-) diff --git a/custom_components/fordpass/const.py b/custom_components/fordpass/const.py index 8af76fe..efaa994 100644 --- a/custom_components/fordpass/const.py +++ b/custom_components/fordpass/const.py @@ -49,6 +49,8 @@ "remoteStartStatus": {"icon": "mdi:remote"}, "zoneLighting": {"icon": "mdi:spotlight-beam"}, "messages": {"icon": "mdi:message-text"}, + "dieselSystemStatus": {"icon": "mdi:smoking-pipe"}, + "exhaustFluidLevel": {"icon": "mdi:barrel"} } SWITCHES = {"ignition": {"icon": "hass:power"}, "guardmode": {"icon": "mdi:shield-key"}} diff --git a/custom_components/fordpass/manifest.json b/custom_components/fordpass/manifest.json index f951904..4695988 100644 --- a/custom_components/fordpass/manifest.json +++ b/custom_components/fordpass/manifest.json @@ -5,7 +5,7 @@ "integration_type": "device", "documentation": "https://github.com/itchannel/fordpass-ha", "issue_tracker": "https://github.com/itchannel/fordpass-ha/issues", - "version": "0.1.42", + "version": "0.1.43", "requirements": [], "ssdp": [], "zeroconf": [], diff --git a/custom_components/fordpass/sensor.py b/custom_components/fordpass/sensor.py index 3ea19ba..714cc09 100644 --- a/custom_components/fordpass/sensor.py +++ b/custom_components/fordpass/sensor.py @@ -20,19 +20,25 @@ 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] - + sensors = [] for key, value in SENSORS.items(): sensor = CarSensor(entry, key, config_entry.options) # Add support for only adding compatible sensors for the given vehicle if key == "zoneLighting": if "zoneLighting" in sensor.coordinator.data: - async_add_entities([sensor], True) + sensors.append(sensor) elif key == "elVeh": if sensor.coordinator.data["elVehDTE"] != None: - async_add_entities([sensor], True) + sensors.append(sensor) + elif key == "dieselSystemStatus": + if "dieselSystemStatus" in sensor.coordinator.data and sensor.coordinator.data["dieselSystemStatus"]["filterRegenerationStatus"] != None: + sensors.append(sensor) + elif key == "exhaustFluidLevel": + if "exhaustFluidLevel" in sensor.coordinator.data and sensor.coordinator.data["dieselSystemStatus"]["exhaustFluidLevel"] != None: + sensors.append(sensor) else: - async_add_entities([sensor], True) - + sensors.append(sensor) + async_add_entities(sensors, True) class CarSensor( FordPassEntity, @@ -143,6 +149,16 @@ def get_value(self, ftype): return None else: return len(self.coordinator.data["messages"]) + elif self.sensor == "dieselSystemStatus": + if self.coordinator.data["dieselSystemStatus"]["filterRegenerationStatus"] != None: + return self.coordinator.data["dieselSystemStatus"]["filterRegenerationStatus"] + else: + return "Not Supported" + elif self.sensor == "exhaustFluidLevel": + if "value" in self.coordinator.data["dieselSystemStatus"]["exhaustFluidLevel"]: + return self.coordinator.data["dieselSystemStatus"]["exhaustFluidLevel"]["value"] + else: + return "Not Supported" elif ftype == "measurement": if self.sensor == "odometer": if self.fordoptions[CONF_DISTANCE_UNIT] == "mi": @@ -179,6 +195,8 @@ def get_value(self, ftype): return None elif self.sensor == "messages": return "Messages" + elif self.sensor == "exhaustFluidLevel": + return "%" elif ftype == "attribute": if self.sensor == "odometer": return self.coordinator.data[self.sensor].items() @@ -392,6 +410,10 @@ def get_value(self, ftype): messages[value["messageSubject"]] = value["createdDate"] return messages + elif self.sensor == "dieselSystemStatus": + return self.coordinator.data["dieselSystemStatus"] + elif self.sensor == "exhaustFluidLevel": + return self.coordinator.data["dieselSystemStatus"] @property def name(self): diff --git a/info.md b/info.md index 865ceba..06cf141 100644 --- a/info.md +++ b/info.md @@ -1,4 +1,6 @@ ## **Changelog** +### Version 1.43 +- Add DPF status on supported vehicles ### Version 1.42 - Fix incorrect tire pressure units (Thanks @costr for debugging) ### Version 1.41 From 823ecfeaf9c676479aa6daa86428d7c69edc4000 Mon Sep 17 00:00:00 2001 From: steve Date: Thu, 26 Jan 2023 18:55:38 +1000 Subject: [PATCH 2/3] Add options refresh --- custom_components/fordpass/__init__.py | 16 ++++++++++++++-- custom_components/fordpass/const.py | 2 ++ custom_components/fordpass/device_tracker.py | 4 ++-- custom_components/fordpass/lock.py | 4 ++-- custom_components/fordpass/sensor.py | 4 ++-- custom_components/fordpass/switch.py | 4 ++-- info.md | 2 ++ 7 files changed, 26 insertions(+), 10 deletions(-) diff --git a/custom_components/fordpass/__init__.py b/custom_components/fordpass/__init__.py index 1332186..5af60e4 100644 --- a/custom_components/fordpass/__init__.py +++ b/custom_components/fordpass/__init__.py @@ -26,7 +26,8 @@ VEHICLE, VIN, UPDATE_INTERVAL, - UPDATE_INTERVAL_DEFAULT + UPDATE_INTERVAL_DEFAULT, + COORDINATOR ) from .fordpass_new import Vehicle @@ -66,13 +67,18 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry): await coordinator.async_refresh() # Get initial data + fordpass_options_listener = entry.add_update_listener(options_update_listener) + if not entry.options: await async_update_options(hass, entry) if not coordinator.last_update_success: raise ConfigEntryNotReady - hass.data[DOMAIN][entry.entry_id] = coordinator + hass.data[DOMAIN][entry.entry_id] = { + COORDINATOR : coordinator, + "fordpass_options_listener": fordpass_options_listener + } for component in PLATFORMS: hass.async_create_task( @@ -115,6 +121,11 @@ async def async_update_options(hass, config_entry): ) hass.config_entries.async_update_entry(config_entry, options=options) +async def options_update_listener( + hass: HomeAssistant, entry: ConfigEntry + ): + _LOGGER.debug("OPTIONS CHANGE") + await hass.config_entries.async_reload(entry.entry_id) def refresh_status(hass, service, coordinator): _LOGGER.debug("Running Service") @@ -141,6 +152,7 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry): ] ) ) + hass.data[DOMAIN][entry.entry_id]["fordpass_options_listener"]() if unload_ok: hass.data[DOMAIN].pop(entry.entry_id) diff --git a/custom_components/fordpass/const.py b/custom_components/fordpass/const.py index efaa994..b6b8c07 100644 --- a/custom_components/fordpass/const.py +++ b/custom_components/fordpass/const.py @@ -20,6 +20,8 @@ UPDATE_INTERVAL = "update_interval" UPDATE_INTERVAL_DEFAULT = 900 +COORDINATOR = "coordinator" + REGION = "region" diff --git a/custom_components/fordpass/device_tracker.py b/custom_components/fordpass/device_tracker.py index 4f071ea..3985f8f 100644 --- a/custom_components/fordpass/device_tracker.py +++ b/custom_components/fordpass/device_tracker.py @@ -5,14 +5,14 @@ from homeassistant.components.device_tracker.config_entry import TrackerEntity from . import FordPassEntity -from .const import DOMAIN +from .const import DOMAIN, COORDINATOR _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] + entry = hass.data[DOMAIN][config_entry.entry_id][COORDINATOR] # Added a check to see if the car supports GPS if entry.data["gps"] != None: diff --git a/custom_components/fordpass/lock.py b/custom_components/fordpass/lock.py index 084ca17..d852ef5 100644 --- a/custom_components/fordpass/lock.py +++ b/custom_components/fordpass/lock.py @@ -4,14 +4,14 @@ from homeassistant.components.lock import LockEntity from . import FordPassEntity -from .const import DOMAIN +from .const import DOMAIN, COORDINATOR _LOGGER = logging.getLogger(__name__) async def async_setup_entry(hass, config_entry, async_add_entities): """Add the lock from the config.""" - entry = hass.data[DOMAIN][config_entry.entry_id] + entry = hass.data[DOMAIN][config_entry.entry_id][COORDINATOR] locks = [Lock(entry)] async_add_entities(locks, False) diff --git a/custom_components/fordpass/sensor.py b/custom_components/fordpass/sensor.py index 714cc09..ee7c20e 100644 --- a/custom_components/fordpass/sensor.py +++ b/custom_components/fordpass/sensor.py @@ -11,7 +11,7 @@ ) from . import FordPassEntity -from .const import CONF_DISTANCE_UNIT, CONF_PRESSURE_UNIT, DOMAIN, SENSORS +from .const import CONF_DISTANCE_UNIT, CONF_PRESSURE_UNIT, DOMAIN, SENSORS, COORDINATOR _LOGGER = logging.getLogger(__name__) @@ -19,7 +19,7 @@ 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] + entry = hass.data[DOMAIN][config_entry.entry_id][COORDINATOR] sensors = [] for key, value in SENSORS.items(): sensor = CarSensor(entry, key, config_entry.options) diff --git a/custom_components/fordpass/switch.py b/custom_components/fordpass/switch.py index e620cf2..aa5e762 100644 --- a/custom_components/fordpass/switch.py +++ b/custom_components/fordpass/switch.py @@ -3,14 +3,14 @@ from homeassistant.components.switch import SwitchEntity from . import FordPassEntity -from .const import DOMAIN, SWITCHES +from .const import DOMAIN, SWITCHES, COORDINATOR _LOGGER = logging.getLogger(__name__) async def async_setup_entry(hass, config_entry, async_add_entities): """Add the Switch from the config.""" - entry = hass.data[DOMAIN][config_entry.entry_id] + entry = hass.data[DOMAIN][config_entry.entry_id][COORDINATOR] # switches = [Switch(entry)] # async_add_entities(switches, False) diff --git a/info.md b/info.md index 06cf141..2aca455 100644 --- a/info.md +++ b/info.md @@ -1,6 +1,8 @@ ## **Changelog** ### Version 1.43 - Add DPF status on supported vehicles +- Incorrect vehicle refresh time (@ronytomen) +- Refresh integration automatticly on options changes ### Version 1.42 - Fix incorrect tire pressure units (Thanks @costr for debugging) ### Version 1.41 From f31af10e13da356bbdc13ddde5d7f75d7faec2e4 Mon Sep 17 00:00:00 2001 From: steve Date: Thu, 26 Jan 2023 19:00:17 +1000 Subject: [PATCH 3/3] Fix string literal error --- custom_components/fordpass/sensor.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/custom_components/fordpass/sensor.py b/custom_components/fordpass/sensor.py index ee7c20e..05eec31 100644 --- a/custom_components/fordpass/sensor.py +++ b/custom_components/fordpass/sensor.py @@ -237,7 +237,7 @@ def get_value(self, ftype): decimal = 0 tirepress = {} for key, value in self.coordinator.data["TPMS"].items(): - if "TirePressure" in key and value is not None and value is not '': + if "TirePressure" in key and value is not None and value != '': if "recommended" in key: tirepress[key] = round(float(value["value"]) * rval, decimal) else: @@ -442,9 +442,9 @@ def icon(self): @property def state_class(self): if "state_class" in SENSORS[self.sensor]: - if SENSORS[self.sensor]["state_class"] is "total": + if SENSORS[self.sensor]["state_class"] == "total": return SensorStateClass.TOTAL - elif SENSORS[self.sensor]["state_class"] is "measurement": + elif SENSORS[self.sensor]["state_class"] == "measurement": return SensorStateClass.MEASUREMENT else: return None @@ -454,7 +454,7 @@ def state_class(self): @property def device_class(self): if "device_class" in SENSORS[self.sensor]: - if SENSORS[self.sensor]["device_class"] is "distance": + if SENSORS[self.sensor]["device_class"] == "distance": return SensorDeviceClass.DISTANCE else: return None \ No newline at end of file