Fix Nissan Leaf default states (#59866)

* Fix default states and add device_class.

Car data is initialised to zero, which means that graphs have an ugly
drop to zero in them when HA is restarted. We should report "None" when
the state is unknown.

We need to use availability to signal whether binary_sensors have
sensible data or not.

We can remove the custom icons and use the defaults provided by using
appropriate device_class.

* Make isort happy.

* Explicitly return None

* Remove feature from bugfix PR.
This commit is contained in:
bwduncan 2021-11-18 14:18:25 +00:00 committed by GitHub
parent b13e4e9953
commit 9ab8622d72
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 21 additions and 6 deletions

View File

@ -213,12 +213,12 @@ class LeafDataStore:
self.car_config = car_config self.car_config = car_config
self.force_miles = car_config[CONF_FORCE_MILES] self.force_miles = car_config[CONF_FORCE_MILES]
self.data = {} self.data = {}
self.data[DATA_CLIMATE] = False self.data[DATA_CLIMATE] = None
self.data[DATA_BATTERY] = 0 self.data[DATA_BATTERY] = None
self.data[DATA_CHARGING] = False self.data[DATA_CHARGING] = None
self.data[DATA_RANGE_AC] = 0 self.data[DATA_RANGE_AC] = None
self.data[DATA_RANGE_AC_OFF] = 0 self.data[DATA_RANGE_AC_OFF] = None
self.data[DATA_PLUGGED_IN] = False self.data[DATA_PLUGGED_IN] = None
self.next_update = None self.next_update = None
self.last_check = None self.last_check = None
self.request_in_progress = False self.request_in_progress = False

View File

@ -30,6 +30,11 @@ class LeafPluggedInSensor(LeafEntity, BinarySensorEntity):
"""Sensor name.""" """Sensor name."""
return f"{self.car.leaf.nickname} Plug Status" return f"{self.car.leaf.nickname} Plug Status"
@property
def available(self):
"""Sensor availability."""
return self.car.data[DATA_PLUGGED_IN] is not None
@property @property
def is_on(self): def is_on(self):
"""Return true if plugged in.""" """Return true if plugged in."""
@ -51,6 +56,11 @@ class LeafChargingSensor(LeafEntity, BinarySensorEntity):
"""Sensor name.""" """Sensor name."""
return f"{self.car.leaf.nickname} Charging Status" return f"{self.car.leaf.nickname} Charging Status"
@property
def available(self):
"""Sensor availability."""
return self.car.data[DATA_CHARGING] is not None
@property @property
def is_on(self): def is_on(self):
"""Return true if charging.""" """Return true if charging."""

View File

@ -52,6 +52,8 @@ class LeafBatterySensor(LeafEntity, SensorEntity):
@property @property
def native_value(self): def native_value(self):
"""Battery state percentage.""" """Battery state percentage."""
if self.car.data[DATA_BATTERY] is None:
return None
return round(self.car.data[DATA_BATTERY]) return round(self.car.data[DATA_BATTERY])
@property @property
@ -96,6 +98,9 @@ class LeafRangeSensor(LeafEntity, SensorEntity):
else: else:
ret = self.car.data[DATA_RANGE_AC_OFF] ret = self.car.data[DATA_RANGE_AC_OFF]
if ret is None:
return None
if not self.car.hass.config.units.is_metric or self.car.force_miles: if not self.car.hass.config.units.is_metric or self.car.force_miles:
ret = IMPERIAL_SYSTEM.length(ret, METRIC_SYSTEM.length_unit) ret = IMPERIAL_SYSTEM.length(ret, METRIC_SYSTEM.length_unit)