Use entity class attributes for Mazda integration (#59869)

This commit is contained in:
Brandon Rothweiler 2022-01-04 10:08:54 -05:00 committed by GitHub
parent f028079815
commit 06f05d2302
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 78 additions and 170 deletions

View File

@ -227,8 +227,14 @@ class MazdaEntity(CoordinatorEntity):
super().__init__(coordinator)
self.client = client
self.index = index
self.vin = self.coordinator.data[self.index]["vin"]
self.vehicle_id = self.coordinator.data[self.index]["id"]
self.vin = self.data["vin"]
self.vehicle_id = self.data["id"]
self._attr_device_info = DeviceInfo(
identifiers={(DOMAIN, self.vin)},
manufacturer="Mazda",
model=f"{self.data['modelYear']} {self.data['carlineName']}",
name=self.vehicle_name,
)
@property
def data(self):
@ -236,16 +242,7 @@ class MazdaEntity(CoordinatorEntity):
return self.coordinator.data[self.index]
@property
def device_info(self) -> DeviceInfo:
"""Return device info for the Mazda entity."""
return DeviceInfo(
identifiers={(DOMAIN, self.vin)},
manufacturer="Mazda",
model=f"{self.data['modelYear']} {self.data['carlineName']}",
name=self.get_vehicle_name(),
)
def get_vehicle_name(self):
def vehicle_name(self):
"""Return the vehicle name, to be used as a prefix for names of other entities."""
if "nickname" in self.data and len(self.data["nickname"]) > 0:
return self.data["nickname"]

View File

@ -29,32 +29,21 @@ async def async_setup_entry(
class MazdaDeviceTracker(MazdaEntity, TrackerEntity):
"""Class for the device tracker."""
@property
def name(self):
"""Return the name of the entity."""
vehicle_name = self.get_vehicle_name()
return f"{vehicle_name} Device Tracker"
_attr_icon = "mdi:car"
_attr_force_update = False
@property
def unique_id(self):
"""Return a unique identifier for this entity."""
return self.vin
def __init__(self, client, coordinator, index) -> None:
"""Initialize Mazda device tracker."""
super().__init__(client, coordinator, index)
@property
def icon(self):
"""Return the icon to use in the frontend."""
return "mdi:car"
self._attr_name = f"{self.vehicle_name} Device Tracker"
self._attr_unique_id = self.vin
@property
def source_type(self):
"""Return the source type, eg gps or router, of the device."""
return SOURCE_TYPE_GPS
@property
def force_update(self):
"""All updates do not need to be written to the state machine."""
return False
@property
def latitude(self):
"""Return latitude value of the device."""

View File

@ -28,16 +28,12 @@ async def async_setup_entry(
class MazdaLock(MazdaEntity, LockEntity):
"""Class for the lock."""
@property
def name(self):
"""Return the name of the entity."""
vehicle_name = self.get_vehicle_name()
return f"{vehicle_name} Lock"
def __init__(self, client, coordinator, index) -> None:
"""Initialize Mazda lock."""
super().__init__(client, coordinator, index)
@property
def unique_id(self):
"""Return a unique identifier for this entity."""
return self.vin
self._attr_name = f"{self.vehicle_name} Lock"
self._attr_unique_id = self.vin
@property
def is_locked(self):

View File

@ -1,7 +1,6 @@
"""Platform for Mazda sensor integration."""
from homeassistant.components.sensor import SensorEntity
from homeassistant.const import (
CONF_UNIT_SYSTEM_IMPERIAL,
LENGTH_KILOMETERS,
LENGTH_MILES,
PERCENTAGE,
@ -34,26 +33,15 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
class MazdaFuelRemainingSensor(MazdaEntity, SensorEntity):
"""Class for the fuel remaining sensor."""
@property
def name(self):
"""Return the name of the sensor."""
vehicle_name = self.get_vehicle_name()
return f"{vehicle_name} Fuel Remaining Percentage"
_attr_native_unit_of_measurement = PERCENTAGE
_attr_icon = "mdi:gas-station"
@property
def unique_id(self):
"""Return a unique identifier for this entity."""
return f"{self.vin}_fuel_remaining_percentage"
def __init__(self, client, coordinator, index) -> None:
"""Initialize Mazda fuel remaining sensor."""
super().__init__(client, coordinator, index)
@property
def native_unit_of_measurement(self):
"""Return the unit of measurement."""
return PERCENTAGE
@property
def icon(self):
"""Return the icon to use in the frontend."""
return "mdi:gas-station"
self._attr_name = f"{self.vehicle_name} Fuel Remaining Percentage"
self._attr_unique_id = f"{self.vin}_fuel_remaining_percentage"
@property
def native_value(self):
@ -64,28 +52,19 @@ class MazdaFuelRemainingSensor(MazdaEntity, SensorEntity):
class MazdaFuelDistanceSensor(MazdaEntity, SensorEntity):
"""Class for the fuel distance sensor."""
@property
def name(self):
"""Return the name of the sensor."""
vehicle_name = self.get_vehicle_name()
return f"{vehicle_name} Fuel Distance Remaining"
_attr_icon = "mdi:gas-station"
@property
def unique_id(self):
"""Return a unique identifier for this entity."""
return f"{self.vin}_fuel_distance_remaining"
def __init__(self, client, coordinator, index) -> None:
"""Initialize Mazda fuel distance sensor."""
super().__init__(client, coordinator, index)
@property
def native_unit_of_measurement(self):
"""Return the unit of measurement."""
if self.hass.config.units.name == CONF_UNIT_SYSTEM_IMPERIAL:
return LENGTH_MILES
return LENGTH_KILOMETERS
@property
def icon(self):
"""Return the icon to use in the frontend."""
return "mdi:gas-station"
self._attr_name = f"{self.vehicle_name} Fuel Distance Remaining"
self._attr_unique_id = f"{self.vin}_fuel_distance_remaining"
self._attr_native_unit_of_measurement = (
LENGTH_KILOMETERS
if coordinator.hass.config.units.is_metric
else LENGTH_MILES
)
@property
def native_value(self):
@ -103,28 +82,19 @@ class MazdaFuelDistanceSensor(MazdaEntity, SensorEntity):
class MazdaOdometerSensor(MazdaEntity, SensorEntity):
"""Class for the odometer sensor."""
@property
def name(self):
"""Return the name of the sensor."""
vehicle_name = self.get_vehicle_name()
return f"{vehicle_name} Odometer"
_attr_icon = "mdi:speedometer"
@property
def unique_id(self):
"""Return a unique identifier for this entity."""
return f"{self.vin}_odometer"
def __init__(self, client, coordinator, index) -> None:
"""Initialize Mazda odometer sensor."""
super().__init__(client, coordinator, index)
@property
def native_unit_of_measurement(self):
"""Return the unit of measurement."""
if self.hass.config.units.name == CONF_UNIT_SYSTEM_IMPERIAL:
return LENGTH_MILES
return LENGTH_KILOMETERS
@property
def icon(self):
"""Return the icon to use in the frontend."""
return "mdi:speedometer"
self._attr_name = f"{self.vehicle_name} Odometer"
self._attr_unique_id = f"{self.vin}_odometer"
self._attr_native_unit_of_measurement = (
LENGTH_KILOMETERS
if coordinator.hass.config.units.is_metric
else LENGTH_MILES
)
@property
def native_value(self):
@ -140,26 +110,15 @@ class MazdaOdometerSensor(MazdaEntity, SensorEntity):
class MazdaFrontLeftTirePressureSensor(MazdaEntity, SensorEntity):
"""Class for the front left tire pressure sensor."""
@property
def name(self):
"""Return the name of the sensor."""
vehicle_name = self.get_vehicle_name()
return f"{vehicle_name} Front Left Tire Pressure"
_attr_native_unit_of_measurement = PRESSURE_PSI
_attr_icon = "mdi:car-tire-alert"
@property
def unique_id(self):
"""Return a unique identifier for this entity."""
return f"{self.vin}_front_left_tire_pressure"
def __init__(self, client, coordinator, index) -> None:
"""Initialize Mazda front left tire pressure sensor."""
super().__init__(client, coordinator, index)
@property
def native_unit_of_measurement(self):
"""Return the unit of measurement."""
return PRESSURE_PSI
@property
def icon(self):
"""Return the icon to use in the frontend."""
return "mdi:car-tire-alert"
self._attr_name = f"{self.vehicle_name} Front Left Tire Pressure"
self._attr_unique_id = f"{self.vin}_front_left_tire_pressure"
@property
def native_value(self):
@ -171,26 +130,15 @@ class MazdaFrontLeftTirePressureSensor(MazdaEntity, SensorEntity):
class MazdaFrontRightTirePressureSensor(MazdaEntity, SensorEntity):
"""Class for the front right tire pressure sensor."""
@property
def name(self):
"""Return the name of the sensor."""
vehicle_name = self.get_vehicle_name()
return f"{vehicle_name} Front Right Tire Pressure"
_attr_native_unit_of_measurement = PRESSURE_PSI
_attr_icon = "mdi:car-tire-alert"
@property
def unique_id(self):
"""Return a unique identifier for this entity."""
return f"{self.vin}_front_right_tire_pressure"
def __init__(self, client, coordinator, index) -> None:
"""Initialize Mazda front right tire pressure sensor."""
super().__init__(client, coordinator, index)
@property
def native_unit_of_measurement(self):
"""Return the unit of measurement."""
return PRESSURE_PSI
@property
def icon(self):
"""Return the icon to use in the frontend."""
return "mdi:car-tire-alert"
self._attr_name = f"{self.vehicle_name} Front Right Tire Pressure"
self._attr_unique_id = f"{self.vin}_front_right_tire_pressure"
@property
def native_value(self):
@ -202,26 +150,15 @@ class MazdaFrontRightTirePressureSensor(MazdaEntity, SensorEntity):
class MazdaRearLeftTirePressureSensor(MazdaEntity, SensorEntity):
"""Class for the rear left tire pressure sensor."""
@property
def name(self):
"""Return the name of the sensor."""
vehicle_name = self.get_vehicle_name()
return f"{vehicle_name} Rear Left Tire Pressure"
_attr_native_unit_of_measurement = PRESSURE_PSI
_attr_icon = "mdi:car-tire-alert"
@property
def unique_id(self):
"""Return a unique identifier for this entity."""
return f"{self.vin}_rear_left_tire_pressure"
def __init__(self, client, coordinator, index) -> None:
"""Initialize Mazda rear left tire pressure sensor."""
super().__init__(client, coordinator, index)
@property
def native_unit_of_measurement(self):
"""Return the unit of measurement."""
return PRESSURE_PSI
@property
def icon(self):
"""Return the icon to use in the frontend."""
return "mdi:car-tire-alert"
self._attr_name = f"{self.vehicle_name} Rear Left Tire Pressure"
self._attr_unique_id = f"{self.vin}_rear_left_tire_pressure"
@property
def native_value(self):
@ -233,26 +170,15 @@ class MazdaRearLeftTirePressureSensor(MazdaEntity, SensorEntity):
class MazdaRearRightTirePressureSensor(MazdaEntity, SensorEntity):
"""Class for the rear right tire pressure sensor."""
@property
def name(self):
"""Return the name of the sensor."""
vehicle_name = self.get_vehicle_name()
return f"{vehicle_name} Rear Right Tire Pressure"
_attr_native_unit_of_measurement = PRESSURE_PSI
_attr_icon = "mdi:car-tire-alert"
@property
def unique_id(self):
"""Return a unique identifier for this entity."""
return f"{self.vin}_rear_right_tire_pressure"
def __init__(self, client, coordinator, index) -> None:
"""Initialize Mazda rear right tire pressure sensor."""
super().__init__(client, coordinator, index)
@property
def native_unit_of_measurement(self):
"""Return the unit of measurement."""
return PRESSURE_PSI
@property
def icon(self):
"""Return the icon to use in the frontend."""
return "mdi:car-tire-alert"
self._attr_name = f"{self.vehicle_name} Rear Right Tire Pressure"
self._attr_unique_id = f"{self.vin}_rear_right_tire_pressure"
@property
def native_value(self):