From 9ab8622d7260dc6cf4a65e4ccebd74d841759f21 Mon Sep 17 00:00:00 2001 From: bwduncan Date: Thu, 18 Nov 2021 14:18:25 +0000 Subject: [PATCH] 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. --- homeassistant/components/nissan_leaf/__init__.py | 12 ++++++------ .../components/nissan_leaf/binary_sensor.py | 10 ++++++++++ homeassistant/components/nissan_leaf/sensor.py | 5 +++++ 3 files changed, 21 insertions(+), 6 deletions(-) diff --git a/homeassistant/components/nissan_leaf/__init__.py b/homeassistant/components/nissan_leaf/__init__.py index 44727ebccdd4..d450eaf7dad8 100644 --- a/homeassistant/components/nissan_leaf/__init__.py +++ b/homeassistant/components/nissan_leaf/__init__.py @@ -213,12 +213,12 @@ class LeafDataStore: self.car_config = car_config self.force_miles = car_config[CONF_FORCE_MILES] self.data = {} - self.data[DATA_CLIMATE] = False - self.data[DATA_BATTERY] = 0 - self.data[DATA_CHARGING] = False - self.data[DATA_RANGE_AC] = 0 - self.data[DATA_RANGE_AC_OFF] = 0 - self.data[DATA_PLUGGED_IN] = False + self.data[DATA_CLIMATE] = None + self.data[DATA_BATTERY] = None + self.data[DATA_CHARGING] = None + self.data[DATA_RANGE_AC] = None + self.data[DATA_RANGE_AC_OFF] = None + self.data[DATA_PLUGGED_IN] = None self.next_update = None self.last_check = None self.request_in_progress = False diff --git a/homeassistant/components/nissan_leaf/binary_sensor.py b/homeassistant/components/nissan_leaf/binary_sensor.py index 3d2064dad4c6..d2387e0e9a23 100644 --- a/homeassistant/components/nissan_leaf/binary_sensor.py +++ b/homeassistant/components/nissan_leaf/binary_sensor.py @@ -30,6 +30,11 @@ class LeafPluggedInSensor(LeafEntity, BinarySensorEntity): """Sensor name.""" 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 def is_on(self): """Return true if plugged in.""" @@ -51,6 +56,11 @@ class LeafChargingSensor(LeafEntity, BinarySensorEntity): """Sensor name.""" return f"{self.car.leaf.nickname} Charging Status" + @property + def available(self): + """Sensor availability.""" + return self.car.data[DATA_CHARGING] is not None + @property def is_on(self): """Return true if charging.""" diff --git a/homeassistant/components/nissan_leaf/sensor.py b/homeassistant/components/nissan_leaf/sensor.py index 4074cd47f50c..bed4d264bd48 100644 --- a/homeassistant/components/nissan_leaf/sensor.py +++ b/homeassistant/components/nissan_leaf/sensor.py @@ -52,6 +52,8 @@ class LeafBatterySensor(LeafEntity, SensorEntity): @property def native_value(self): """Battery state percentage.""" + if self.car.data[DATA_BATTERY] is None: + return None return round(self.car.data[DATA_BATTERY]) @property @@ -96,6 +98,9 @@ class LeafRangeSensor(LeafEntity, SensorEntity): else: 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: ret = IMPERIAL_SYSTEM.length(ret, METRIC_SYSTEM.length_unit)