Code quality improvements for nissan_leaf (#82442)

Code quality improvements for nissan_leaf
This commit is contained in:
Marc Mueller 2022-11-21 09:06:30 +01:00 committed by GitHub
parent 36e38841a4
commit aa3bd78f7e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 15 additions and 31 deletions

View File

@ -500,7 +500,7 @@ class LeafDataStore:
class LeafEntity(Entity): class LeafEntity(Entity):
"""Base class for Nissan Leaf entity.""" """Base class for Nissan Leaf entity."""
def __init__(self, car: Leaf) -> None: def __init__(self, car: LeafDataStore) -> None:
"""Store LeafDataStore upon init.""" """Store LeafDataStore upon init."""
self.car = car self.car = car

View File

@ -3,8 +3,6 @@ from __future__ import annotations
import logging import logging
from pycarwings2.pycarwings2 import Leaf
from homeassistant.components.binary_sensor import ( from homeassistant.components.binary_sensor import (
BinarySensorDeviceClass, BinarySensorDeviceClass,
BinarySensorEntity, BinarySensorEntity,
@ -13,7 +11,7 @@ from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
from . import LeafEntity from . import LeafDataStore, LeafEntity
from .const import DATA_CHARGING, DATA_LEAF, DATA_PLUGGED_IN from .const import DATA_CHARGING, DATA_LEAF, DATA_PLUGGED_IN
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
@ -43,7 +41,7 @@ class LeafPluggedInSensor(LeafEntity, BinarySensorEntity):
_attr_device_class = BinarySensorDeviceClass.PLUG _attr_device_class = BinarySensorDeviceClass.PLUG
def __init__(self, car: Leaf) -> None: def __init__(self, car: LeafDataStore) -> None:
"""Set up plug status sensor.""" """Set up plug status sensor."""
super().__init__(car) super().__init__(car)
self._attr_unique_id = f"{self.car.leaf.vin.lower()}_plugstatus" self._attr_unique_id = f"{self.car.leaf.vin.lower()}_plugstatus"
@ -69,7 +67,7 @@ class LeafChargingSensor(LeafEntity, BinarySensorEntity):
_attr_device_class = BinarySensorDeviceClass.BATTERY_CHARGING _attr_device_class = BinarySensorDeviceClass.BATTERY_CHARGING
def __init__(self, car: Leaf) -> None: def __init__(self, car: LeafDataStore) -> None:
"""Set up charging status sensor.""" """Set up charging status sensor."""
super().__init__(car) super().__init__(car)
self._attr_unique_id = f"{self.car.leaf.vin.lower()}_chargingstatus" self._attr_unique_id = f"{self.car.leaf.vin.lower()}_chargingstatus"

View File

@ -3,7 +3,6 @@ from __future__ import annotations
import logging import logging
from pycarwings2.pycarwings2 import Leaf
from voluptuous.validators import Number from voluptuous.validators import Number
from homeassistant.components.sensor import SensorDeviceClass, SensorEntity from homeassistant.components.sensor import SensorDeviceClass, SensorEntity
@ -15,7 +14,7 @@ from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
from homeassistant.util.unit_conversion import DistanceConverter from homeassistant.util.unit_conversion import DistanceConverter
from homeassistant.util.unit_system import US_CUSTOMARY_SYSTEM from homeassistant.util.unit_system import US_CUSTOMARY_SYSTEM
from . import LeafEntity from . import LeafDataStore, LeafEntity
from .const import ( from .const import (
DATA_BATTERY, DATA_BATTERY,
DATA_CHARGING, DATA_CHARGING,
@ -26,8 +25,6 @@ from .const import (
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
ICON_RANGE = "mdi:speedometer"
def setup_platform( def setup_platform(
hass: HomeAssistant, hass: HomeAssistant,
@ -52,7 +49,10 @@ def setup_platform(
class LeafBatterySensor(LeafEntity, SensorEntity): class LeafBatterySensor(LeafEntity, SensorEntity):
"""Nissan Leaf Battery Sensor.""" """Nissan Leaf Battery Sensor."""
def __init__(self, car: Leaf) -> None: _attr_device_class = SensorDeviceClass.BATTERY
_attr_native_unit_of_measurement = PERCENTAGE
def __init__(self, car: LeafDataStore) -> None:
"""Set up battery sensor.""" """Set up battery sensor."""
super().__init__(car) super().__init__(car)
self._attr_unique_id = f"{self.car.leaf.vin.lower()}_soc" self._attr_unique_id = f"{self.car.leaf.vin.lower()}_soc"
@ -62,11 +62,6 @@ class LeafBatterySensor(LeafEntity, SensorEntity):
"""Sensor Name.""" """Sensor Name."""
return f"{self.car.leaf.nickname} Charge" return f"{self.car.leaf.nickname} Charge"
@property
def device_class(self) -> str:
"""Return the device class of the sensor."""
return SensorDeviceClass.BATTERY
@property @property
def native_value(self) -> Number | None: def native_value(self) -> Number | None:
"""Battery state percentage.""" """Battery state percentage."""
@ -74,11 +69,6 @@ class LeafBatterySensor(LeafEntity, SensorEntity):
return None return None
return round(self.car.data[DATA_BATTERY]) return round(self.car.data[DATA_BATTERY])
@property
def native_unit_of_measurement(self) -> str:
"""Battery state measured in percentage."""
return PERCENTAGE
@property @property
def icon(self) -> str: def icon(self) -> str:
"""Battery state icon handling.""" """Battery state icon handling."""
@ -89,7 +79,9 @@ class LeafBatterySensor(LeafEntity, SensorEntity):
class LeafRangeSensor(LeafEntity, SensorEntity): class LeafRangeSensor(LeafEntity, SensorEntity):
"""Nissan Leaf Range Sensor.""" """Nissan Leaf Range Sensor."""
def __init__(self, car: Leaf, ac_on: bool) -> None: _attr_icon = "mdi:speedometer"
def __init__(self, car: LeafDataStore, ac_on: bool) -> None:
"""Set up range sensor. Store if AC on.""" """Set up range sensor. Store if AC on."""
self._ac_on = ac_on self._ac_on = ac_on
super().__init__(car) super().__init__(car)
@ -115,6 +107,7 @@ class LeafRangeSensor(LeafEntity, SensorEntity):
@property @property
def native_value(self) -> float | None: def native_value(self) -> float | None:
"""Battery range in miles or kms.""" """Battery range in miles or kms."""
ret: float | None
if self._ac_on: if self._ac_on:
ret = self.car.data[DATA_RANGE_AC] ret = self.car.data[DATA_RANGE_AC]
else: else:
@ -134,8 +127,3 @@ class LeafRangeSensor(LeafEntity, SensorEntity):
if self.car.hass.config.units is US_CUSTOMARY_SYSTEM or self.car.force_miles: if self.car.hass.config.units is US_CUSTOMARY_SYSTEM or self.car.force_miles:
return LENGTH_MILES return LENGTH_MILES
return LENGTH_KILOMETERS return LENGTH_KILOMETERS
@property
def icon(self) -> str:
"""Nice icon for range."""
return ICON_RANGE

View File

@ -4,14 +4,12 @@ from __future__ import annotations
import logging import logging
from typing import Any from typing import Any
from pycarwings2.pycarwings2 import Leaf
from homeassistant.components.switch import SwitchEntity from homeassistant.components.switch import SwitchEntity
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
from . import LeafEntity from . import LeafDataStore, LeafEntity
from .const import DATA_CLIMATE, DATA_LEAF from .const import DATA_CLIMATE, DATA_LEAF
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
@ -38,7 +36,7 @@ def setup_platform(
class LeafClimateSwitch(LeafEntity, SwitchEntity): class LeafClimateSwitch(LeafEntity, SwitchEntity):
"""Nissan Leaf Climate Control switch.""" """Nissan Leaf Climate Control switch."""
def __init__(self, car: Leaf) -> None: def __init__(self, car: LeafDataStore) -> None:
"""Set up climate control switch.""" """Set up climate control switch."""
super().__init__(car) super().__init__(car)
self._attr_unique_id = f"{self.car.leaf.vin.lower()}_climatecontrol" self._attr_unique_id = f"{self.car.leaf.vin.lower()}_climatecontrol"