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):
"""Base class for Nissan Leaf entity."""
def __init__(self, car: Leaf) -> None:
def __init__(self, car: LeafDataStore) -> None:
"""Store LeafDataStore upon init."""
self.car = car

View file

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

View file

@ -3,7 +3,6 @@ from __future__ import annotations
import logging
from pycarwings2.pycarwings2 import Leaf
from voluptuous.validators import Number
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_system import US_CUSTOMARY_SYSTEM
from . import LeafEntity
from . import LeafDataStore, LeafEntity
from .const import (
DATA_BATTERY,
DATA_CHARGING,
@ -26,8 +25,6 @@ from .const import (
_LOGGER = logging.getLogger(__name__)
ICON_RANGE = "mdi:speedometer"
def setup_platform(
hass: HomeAssistant,
@ -52,7 +49,10 @@ def setup_platform(
class LeafBatterySensor(LeafEntity, SensorEntity):
"""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."""
super().__init__(car)
self._attr_unique_id = f"{self.car.leaf.vin.lower()}_soc"
@ -62,11 +62,6 @@ class LeafBatterySensor(LeafEntity, SensorEntity):
"""Sensor Name."""
return f"{self.car.leaf.nickname} Charge"
@property
def device_class(self) -> str:
"""Return the device class of the sensor."""
return SensorDeviceClass.BATTERY
@property
def native_value(self) -> Number | None:
"""Battery state percentage."""
@ -74,11 +69,6 @@ class LeafBatterySensor(LeafEntity, SensorEntity):
return None
return round(self.car.data[DATA_BATTERY])
@property
def native_unit_of_measurement(self) -> str:
"""Battery state measured in percentage."""
return PERCENTAGE
@property
def icon(self) -> str:
"""Battery state icon handling."""
@ -89,7 +79,9 @@ class LeafBatterySensor(LeafEntity, SensorEntity):
class LeafRangeSensor(LeafEntity, SensorEntity):
"""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."""
self._ac_on = ac_on
super().__init__(car)
@ -115,6 +107,7 @@ class LeafRangeSensor(LeafEntity, SensorEntity):
@property
def native_value(self) -> float | None:
"""Battery range in miles or kms."""
ret: float | None
if self._ac_on:
ret = self.car.data[DATA_RANGE_AC]
else:
@ -134,8 +127,3 @@ class LeafRangeSensor(LeafEntity, SensorEntity):
if self.car.hass.config.units is US_CUSTOMARY_SYSTEM or self.car.force_miles:
return LENGTH_MILES
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
from typing import Any
from pycarwings2.pycarwings2 import Leaf
from homeassistant.components.switch import SwitchEntity
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
from . import LeafEntity
from . import LeafDataStore, LeafEntity
from .const import DATA_CLIMATE, DATA_LEAF
_LOGGER = logging.getLogger(__name__)
@ -38,7 +36,7 @@ def setup_platform(
class LeafClimateSwitch(LeafEntity, SwitchEntity):
"""Nissan Leaf Climate Control switch."""
def __init__(self, car: Leaf) -> None:
def __init__(self, car: LeafDataStore) -> None:
"""Set up climate control switch."""
super().__init__(car)
self._attr_unique_id = f"{self.car.leaf.vin.lower()}_climatecontrol"