Skip to content

Commit

Permalink
Add debug sensors for each API entity
Browse files Browse the repository at this point in the history
  • Loading branch information
itchannel committed Dec 30, 2023
1 parent e2381ff commit c4f8d2c
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 3 deletions.
9 changes: 7 additions & 2 deletions custom_components/fordpass/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"ignitionStatus": {"icon": "hass:power", "api_key": "ignitionStatus"},
"doorStatus": {"icon": "mdi:car-door", "api_key": "doorStatus"},
"windowPosition": {"icon": "mdi:car-door", "api_key": "windowStatus"},
"lastRefresh": {"icon": "mdi:clock", "device_class": "timestamp", "api_key": "lastRefresh"},
"lastRefresh": {"icon": "mdi:clock", "device_class": "timestamp", "api_key": "lastRefresh" , "sensor_type": "single"},
"elVeh": {"icon": "mdi:ev-station", "api_key": "xevBatteryRange", "device_class": "distance", "state_class": "measurement", "measurement": "km"},
"elVehCharging": {"icon": "mdi:ev-station", "api_key": "xevBatteryChargeDisplayStatus"},
"speed": {"icon": "mdi:speedometer", "device_class": "speed", "state_class": "measurement", "api_key": "speed", "measurement": "km/h"},
Expand All @@ -55,9 +55,14 @@
# },
"remoteStartStatus": {"icon": "mdi:remote", "api_key": "remoteStartCountdownTimer"},
# "zoneLighting": {"icon": "mdi:spotlight-beam"},
"messages": {"icon": "mdi:message-text", "api_key": "messages", "measurement": "messages"},
"messages": {"icon": "mdi:message-text", "api_key": "messages", "measurement": "messages", "sensor_type": "single"},
"dieselSystemStatus": {"icon": "mdi:smoking-pipe", "api_key": "dieselExhaustFilterStatus"},
"exhaustFluidLevel": {"icon": "mdi:barrel", "api_key": "dieselExhaustFluidLevel", "measurement": "%"},
# Debug Sensors (Disabled by default)
"events": {"icon": "mdi:calendar", "api_key": "events", "sensor_type": "single", "debug": True},
"metrics": {"icon": "mdi:chart-line", "api_key": "metrics", "sensor_type": "single", "debug": True},
"states": {"icon": "mdi:car", "api_key": "states", "sensor_type": "single", "debug": True},
"vehicles": {"icon": "mdi:car-multiple", "api_key": "vehicles", "sensor_type": "single", "debug": True},
}

SWITCHES = {"ignition": {"icon": "hass:power"}, "guardmode": {"icon": "mdi:shield-key"}}
Expand Down
4 changes: 4 additions & 0 deletions custom_components/fordpass/fordpass_new.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

from requests.adapters import HTTPAdapter
from urllib3.util.retry import Retry
from homeassistant import exceptions

_LOGGER = logging.getLogger(__name__)
defaultHeaders = {
Expand Down Expand Up @@ -163,6 +164,9 @@ def auth2_step1(self):
cookie_dict = step1_session.cookies.get_dict()
_LOGGER.debug(cookie_dict)

if step1post.status_code == 400:
raise exceptions.HomeAssistantError(step1post.json()["message"])




Expand Down
26 changes: 25 additions & 1 deletion custom_components/fordpass/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
sensor = CarSensor(entry, key, config_entry.options)
api_key = value["api_key"]
api_class = value.get("api_class", None)
sensor_type = value.get("sensor_type", None)
string = isinstance(api_key, str)
if string and api_key == "messages" or api_key == "lastRefresh":
if string and sensor_type == "single":
sensors.append(sensor)
elif string:
if api_key and api_class and api_key in sensor.coordinator.data.get(api_class, {}):
Expand Down Expand Up @@ -159,6 +160,14 @@ def get_value(self, ftype):
return "DISABLED"
else:
return state
if self.sensor == "events":
return len(self.events)
if self.sensor == "states":
return len(self.states)
if self.sensor == "vehicles":
return len(self.coordinator.data.get("vehicles", {}))
if self.sensor == "metrics":
return len(self.data)
return None
if ftype == "measurement":
return SENSORS.get(self.sensor, {}).get("measurement", None)
Expand Down Expand Up @@ -470,6 +479,14 @@ def get_value(self, ftype):
if value.get("value") is not None:
alerts[key] = value["value"]
return alerts or None
if self.sensor == "events":
return self.events
if self.sensor == "states":
return self.states
if self.sensor == "vehicles":
return self.coordinator.data.get("vehicles", {})
if self.sensor == "metrics":
return self.data
return None


Expand Down Expand Up @@ -537,3 +554,10 @@ def device_class(self):
if SENSORS[self.sensor]["device_class"] == "speed":
return SensorDeviceClass.SPEED
return None

@property
def entity_registry_enabled_default(self):
"""Return if entity should be enabled when first added to the entity registry."""
if "debug" in SENSORS[self.sensor]:
return False
return True

0 comments on commit c4f8d2c

Please sign in to comment.