diff --git a/custom_components/tesla_custom/base.py b/custom_components/tesla_custom/base.py index 14a69769..744110dd 100644 --- a/custom_components/tesla_custom/base.py +++ b/custom_components/tesla_custom/base.py @@ -1,5 +1,5 @@ """Support for Tesla cars and energy sites.""" -from homeassistant.core import HomeAssistant +from homeassistant.core import HomeAssistant, callback from homeassistant.helpers.entity import DeviceInfo from homeassistant.helpers.update_coordinator import CoordinatorEntity from homeassistant.util import slugify @@ -17,6 +17,8 @@ class TeslaBaseEntity(CoordinatorEntity): _attr_attribution = ATTRIBUTION _attr_has_entity_name = True + _enabled_by_default: bool = True + type: str def __init__( self, hass: HomeAssistant, coordinator: TeslaDataUpdateCoordinator @@ -24,15 +26,14 @@ def __init__( """Initialise the Tesla device.""" super().__init__(coordinator) self._coordinator: TeslaDataUpdateCoordinator = coordinator - self._enabled_by_default: bool = True self.hass = hass - self.type = None self._memorized_unique_id = None + @callback def refresh(self) -> None: """Refresh the device data. - This is called by the DataUpdateCoodinator when new data is available. + This is called by the DataUpdateCoordinator when new data is available. This assumes the controller has already been updated. This should be called by inherited classes so the overall device information is updated. @@ -97,11 +98,12 @@ async def update_controller( @property def vehicle_name(self) -> str: """Return vehicle name.""" + display_name = self._car.display_name + vin = self._car.vin return ( - self._car.display_name - if self._car.display_name is not None - and self._car.display_name != self._car.vin[-6:] - else f"Tesla Model {str(self._car.vin[3]).upper()}" + display_name + if display_name is not None and display_name != vin[-6:] + else f"Tesla Model {str(vin[3]).upper()}" ) @property @@ -125,10 +127,12 @@ def device_info(self) -> DeviceInfo: @property def assumed_state(self) -> bool: """Return whether the data is from an online vehicle.""" - return not self._coordinator.controller.is_car_online(vin=self._car.vin) and ( - self._coordinator.controller.get_last_update_time(vin=self._car.vin) - - self._coordinator.controller.get_last_wake_up_time(vin=self._car.vin) - > self._coordinator.controller.update_interval + vin = self._car.vin + controller = self._coordinator.controller + return not controller.is_car_online(vin=vin) and ( + controller.get_last_update_time(vin=vin) + - controller.get_last_wake_up_time(vin=vin) + > controller.update_interval ) diff --git a/custom_components/tesla_custom/binary_sensor.py b/custom_components/tesla_custom/binary_sensor.py index 6998afbe..8abf31eb 100644 --- a/custom_components/tesla_custom/binary_sensor.py +++ b/custom_components/tesla_custom/binary_sensor.py @@ -6,11 +6,8 @@ BinarySensorEntity, ) from homeassistant.core import HomeAssistant -from teslajsonpy.car import TeslaCar from teslajsonpy.const import GRID_ACTIVE, RESOURCE_TYPE_BATTERY -from teslajsonpy.energy import PowerwallSite -from . import TeslaDataUpdateCoordinator from .base import TeslaCarEntity, TeslaEnergyEntity from .const import DOMAIN @@ -50,17 +47,9 @@ async def async_setup_entry(hass: HomeAssistant, config_entry, async_add_entitie class TeslaCarParkingBrake(TeslaCarEntity, BinarySensorEntity): """Representation of a Tesla car parking brake binary sensor.""" - def __init__( - self, - hass: HomeAssistant, - car: TeslaCar, - coordinator: TeslaDataUpdateCoordinator, - ) -> None: - """Initialize parking brake entity.""" - super().__init__(hass, car, coordinator) - self.type = "parking brake" - self._attr_icon = "mdi:car-brake-parking" - self._attr_device_class = None + type = "parking brake" + _attr_icon = "mdi:car-brake-parking" + _attr_device_class = None @property def is_on(self): @@ -72,17 +61,9 @@ def is_on(self): class TeslaCarChargerConnection(TeslaCarEntity, BinarySensorEntity): """Representation of a Tesla car charger connection binary sensor.""" - def __init__( - self, - hass: HomeAssistant, - car: TeslaCar, - coordinator: TeslaDataUpdateCoordinator, - ) -> None: - """Initialize charger connection entity.""" - super().__init__(hass, car, coordinator) - self.type = "charger" - self._attr_icon = "mdi:ev-station" - self._attr_device_class = BinarySensorDeviceClass.PLUG + type = "charger" + _attr_icon = "mdi:ev-station" + _attr_device_class = BinarySensorDeviceClass.PLUG @property def is_on(self): @@ -104,17 +85,9 @@ def extra_state_attributes(self): class TeslaCarCharging(TeslaCarEntity, BinarySensorEntity): """Representation of Tesla car charging binary sensor.""" - def __init__( - self, - hass: HomeAssistant, - car: TeslaCar, - coordinator: TeslaDataUpdateCoordinator, - ) -> None: - """Initialize charging entity.""" - super().__init__(hass, car, coordinator) - self.type = "charging" - self._attr_icon = "mdi:ev-station" - self._attr_device_class = BinarySensorDeviceClass.BATTERY_CHARGING + type = "charging" + _attr_icon = "mdi:ev-station" + _attr_device_class = BinarySensorDeviceClass.BATTERY_CHARGING @property def is_on(self): @@ -125,16 +98,8 @@ def is_on(self): class TeslaCarOnline(TeslaCarEntity, BinarySensorEntity): """Representation of a Tesla car online binary sensor.""" - def __init__( - self, - hass: HomeAssistant, - car: TeslaCar, - coordinator: TeslaDataUpdateCoordinator, - ) -> None: - """Initialize car online entity.""" - super().__init__(hass, car, coordinator) - self.type = "online" - self._attr_device_class = BinarySensorDeviceClass.CONNECTIVITY + _attr_device_class = BinarySensorDeviceClass.CONNECTIVITY + type = "online" @property def is_on(self): @@ -155,17 +120,9 @@ def extra_state_attributes(self): class TeslaCarAsleep(TeslaCarEntity, BinarySensorEntity): """Representation of a Tesla car asleep binary sensor.""" - def __init__( - self, - hass: HomeAssistant, - car: TeslaCar, - coordinator: TeslaDataUpdateCoordinator, - ) -> None: - """Initialize car asleep entity.""" - super().__init__(hass, car, coordinator) - self.type = "asleep" - self._attr_device_class = None - self._attr_icon = "mdi:sleep" + type = "asleep" + _attr_device_class = None + _attr_icon = "mdi:sleep" @property def is_on(self): @@ -176,17 +133,9 @@ def is_on(self): class TeslaEnergyBatteryCharging(TeslaEnergyEntity, BinarySensorEntity): """Representation of a Tesla energy charging binary sensor.""" - def __init__( - self, - hass: HomeAssistant, - energysite: PowerwallSite, - coordinator: TeslaDataUpdateCoordinator, - ) -> None: - """Initialize battery charging entity.""" - super().__init__(hass, energysite, coordinator) - self.type = "battery charging" - self._attr_device_class = BinarySensorDeviceClass.BATTERY_CHARGING - self._attr_icon = "mdi:battery-charging" + _attr_device_class = BinarySensorDeviceClass.BATTERY_CHARGING + _attr_icon = "mdi:battery-charging" + type = "battery charging" @property def is_on(self) -> bool: @@ -197,16 +146,8 @@ def is_on(self) -> bool: class TeslaEnergyGridStatus(TeslaEnergyEntity, BinarySensorEntity): """Representation of the Tesla energy grid status binary sensor.""" - def __init__( - self, - hass: HomeAssistant, - energysite: PowerwallSite, - coordinator: TeslaDataUpdateCoordinator, - ) -> None: - """Initialize grid status entity.""" - super().__init__(hass, energysite, coordinator) - self.type = "grid status" - self._attr_device_class = BinarySensorDeviceClass.POWER + type = "grid status" + _attr_device_class = BinarySensorDeviceClass.POWER @property def is_on(self) -> bool: @@ -217,27 +158,15 @@ def is_on(self) -> bool: class TeslaCarDoors(TeslaCarEntity, BinarySensorEntity): """Representation of a Tesla car door sensor.""" - def __init__( - self, - hass: HomeAssistant, - car: TeslaCar, - coordinator: TeslaDataUpdateCoordinator, - ) -> None: - """Initialize car door entity.""" - super().__init__(hass, car, coordinator) - self.type = "doors" - self._attr_device_class = BinarySensorDeviceClass.DOOR - self._attr_icon = "mdi:car-door" + type = "doors" + _attr_device_class = BinarySensorDeviceClass.DOOR + _attr_icon = "mdi:car-door" @property - def is_on(self): + def is_on(self) -> bool: """Return True if a car door is open.""" - return ( - self._car.door_df - or self._car.door_dr - or self._car.door_pf - or self._car.door_pr - ) + car = self._car + return car.door_df or car.door_dr or car.door_pf or car.door_pr @property def extra_state_attributes(self): @@ -259,27 +188,15 @@ def _open_or_closed(self, door): class TeslaCarWindows(TeslaCarEntity, BinarySensorEntity): """Representation of a Tesla window door sensor.""" - def __init__( - self, - hass: HomeAssistant, - car: TeslaCar, - coordinator: TeslaDataUpdateCoordinator, - ) -> None: - """Initialize car windows entity.""" - super().__init__(hass, car, coordinator) - self.type = "windows" - self._attr_device_class = BinarySensorDeviceClass.WINDOW - self._attr_icon = "mdi:car-door" + type = "windows" + _attr_device_class = BinarySensorDeviceClass.WINDOW + _attr_icon = "mdi:car-door" @property def is_on(self): """Return True if a car window is open.""" - return ( - self._car.window_fd - or self._car.window_fp - or self._car.window_rd - or self._car.window_rp - ) + car = self._car + return car.window_fd or car.window_fp or car.window_rd or car.window_rp @property def extra_state_attributes(self): @@ -301,24 +218,14 @@ def _open_or_closed(self, window): class TeslaCarScheduledCharging(TeslaCarEntity, BinarySensorEntity): """Representation of a Tesla car scheduled charging binary sensor.""" - def __init__( - self, - hass: HomeAssistant, - car: TeslaCar, - coordinator: TeslaDataUpdateCoordinator, - ) -> None: - """Initialize scheduled charging entity.""" - super().__init__(hass, car, coordinator) - self.type = "scheduled charging" - self._attr_icon = "mdi:calendar-plus" - self._attr_device_class = None + type = "scheduled charging" + _attr_icon = "mdi:calendar-plus" + _attr_device_class = None @property - def is_on(self): - """Return True if scheduled charging enebaled.""" - if self._car.scheduled_charging_mode == "StartAt": - return True - return False + def is_on(self) -> bool: + """Return True if scheduled charging enabled.""" + return self._car.scheduled_charging_mode == "StartAt" @property def extra_state_attributes(self): @@ -336,28 +243,19 @@ def extra_state_attributes(self): class TeslaCarScheduledDeparture(TeslaCarEntity, BinarySensorEntity): """Representation of a Tesla car scheduled departure binary sensor.""" - def __init__( - self, - hass: HomeAssistant, - car: TeslaCar, - coordinator: TeslaDataUpdateCoordinator, - ) -> None: - """Initialize scheduled departure entity.""" - super().__init__(hass, car, coordinator) - self.type = "scheduled departure" - self._attr_icon = "mdi:calendar-plus" - self._attr_device_class = None + type = "scheduled departure" + _attr_icon = "mdi:calendar-plus" + _attr_device_class = None @property def is_on(self): """Return True if scheduled departure enebaled.""" - if ( - self._car.scheduled_charging_mode == "DepartBy" - or self._car.is_preconditioning_enabled - or self._car.is_off_peak_charging_enabled - ): - return True - return False + car = self._car + return bool( + car.scheduled_charging_mode == "DepartBy" + or car.is_preconditioning_enabled + or car.is_off_peak_charging_enabled + ) @property def extra_state_attributes(self): @@ -380,23 +278,17 @@ def extra_state_attributes(self): class TeslaCarUserPresent(TeslaCarEntity, BinarySensorEntity): """Representation of a Tesla car user present binary sensor.""" - def __init__( - self, - hass: HomeAssistant, - car: TeslaCar, - coordinator: TeslaDataUpdateCoordinator, - ) -> None: - """Initialize user present entity.""" - super().__init__(hass, car, coordinator) - self.type = "user present" - self._attr_icon = "mdi:account-check" - self._attr_device_class = None + type = "user present" + _attr_icon = "mdi:account-check" + _attr_device_class = None @property - def is_on(self): + def is_on(self) -> bool: """Return True if user present enebaled.""" # pylint: disable=protected-access - return self._car._vehicle_data.get("vehicle_state", {}).get("is_user_present") + return bool( + self._car._vehicle_data.get("vehicle_state", {}).get("is_user_present") + ) @property def extra_state_attributes(self): diff --git a/custom_components/tesla_custom/button.py b/custom_components/tesla_custom/button.py index d5431541..40f673d2 100644 --- a/custom_components/tesla_custom/button.py +++ b/custom_components/tesla_custom/button.py @@ -36,16 +36,8 @@ async def async_setup_entry(hass: HomeAssistant, config_entry, async_add_entitie class TeslaCarHorn(TeslaCarEntity, ButtonEntity): """Representation of a Tesla car horn button.""" - def __init__( - self, - hass: HomeAssistant, - car: TeslaCar, - coordinator: TeslaDataUpdateCoordinator, - ) -> None: - """Initialize horn entity.""" - super().__init__(hass, car, coordinator) - self.type = "horn" - self._attr_icon = "mdi:bullhorn" + type = "horn" + _attr_icon = "mdi:bullhorn" async def async_press(self) -> None: """Handle the button press.""" @@ -55,16 +47,8 @@ async def async_press(self) -> None: class TeslaCarFlashLights(TeslaCarEntity, ButtonEntity): """Representation of a Tesla car flash lights button.""" - def __init__( - self, - hass: HomeAssistant, - car: TeslaCar, - coordinator: TeslaDataUpdateCoordinator, - ) -> None: - """Initialize flash light entity.""" - super().__init__(hass, car, coordinator) - self.type = "flash lights" - self._attr_icon = "mdi:car-light-high" + type = "flash lights" + _attr_icon = "mdi:car-light-high" async def async_press(self) -> None: """Handle the button press.""" @@ -74,17 +58,9 @@ async def async_press(self) -> None: class TeslaCarWakeUp(TeslaCarEntity, ButtonEntity): """Representation of a Tesla car wake up button.""" - def __init__( - self, - hass: HomeAssistant, - car: TeslaCar, - coordinator: TeslaDataUpdateCoordinator, - ) -> None: - """Initialize wake up button.""" - super().__init__(hass, car, coordinator) - self.type = "wake up" - self._attr_icon = "mdi:moon-waning-crescent" - self._attr_entity_category = EntityCategory.DIAGNOSTIC + type = "wake up" + _attr_icon = "mdi:moon-waning-crescent" + _attr_entity_category = EntityCategory.DIAGNOSTIC async def async_press(self) -> None: """Handle the button press.""" @@ -99,17 +75,9 @@ def available(self) -> bool: class TeslaCarForceDataUpdate(TeslaCarEntity, ButtonEntity): """Representation of a Tesla car force data update button.""" - def __init__( - self, - hass: HomeAssistant, - car: TeslaCar, - coordinator: TeslaDataUpdateCoordinator, - ) -> None: - """Initialize force data update button.""" - super().__init__(hass, car, coordinator) - self.type = "force data update" - self._attr_icon = "mdi:database-sync" - self._attr_entity_category = EntityCategory.DIAGNOSTIC + type = "force data update" + _attr_icon = "mdi:database-sync" + _attr_entity_category = EntityCategory.DIAGNOSTIC async def async_press(self) -> None: """Handle the button press.""" @@ -124,6 +92,9 @@ def available(self) -> bool: class TeslaCarTriggerHomelink(TeslaCarEntity, ButtonEntity): """Representation of a Tesla car Homelink button.""" + type = "homelink" + _attr_icon = "mdi:garage" + def __init__( self, hass: HomeAssistant, @@ -132,8 +103,6 @@ def __init__( ) -> None: """Initialise Homelink button.""" super().__init__(hass, car, coordinator) - self.type = "homelink" - self._attr_icon = "mdi:garage" # Entity is only enabled upon first install if garages have been paired to homelink self._enabled_by_default = self._car.homelink_device_count @@ -150,16 +119,8 @@ async def async_press(self): class TeslaCarRemoteStart(TeslaCarEntity, ButtonEntity): """Representation of a Tesla car remote start button.""" - def __init__( - self, - hass: HomeAssistant, - car: TeslaCar, - coordinator: TeslaDataUpdateCoordinator, - ) -> None: - """Initialise remote start button.""" - super().__init__(hass, car, coordinator) - self.type = "remote start" - self._attr_icon = "mdi:power" + type = "remote start" + _attr_icon = "mdi:power" async def async_press(self): """Send the command.""" @@ -169,6 +130,10 @@ async def async_press(self): class TeslaCarEmissionsTest(TeslaCarEntity, ButtonEntity): """Representation of a Tesla car emissions test button.""" + type = "emissions test" + _attr_icon = "mdi:weather-windy" + _attr_entity_category = EntityCategory.DIAGNOSTIC + def __init__( self, hass: HomeAssistant, @@ -177,9 +142,6 @@ def __init__( ) -> None: """Initialize emissions test button.""" super().__init__(hass, car, coordinator) - self.type = "emissions test" - self._attr_icon = "mdi:weather-windy" - self._attr_entity_category = EntityCategory.DIAGNOSTIC self._enabled_by_default = self._car.pedestrian_speaker async def async_press(self) -> None: diff --git a/custom_components/tesla_custom/climate.py b/custom_components/tesla_custom/climate.py index b30a902b..2ee0927e 100644 --- a/custom_components/tesla_custom/climate.py +++ b/custom_components/tesla_custom/climate.py @@ -12,9 +12,7 @@ ) from homeassistant.const import ATTR_TEMPERATURE, TEMP_CELSIUS from homeassistant.core import HomeAssistant -from teslajsonpy.car import TeslaCar -from . import TeslaDataUpdateCoordinator from .base import TeslaCarEntity from .const import DOMAIN @@ -52,15 +50,7 @@ async def async_setup_entry( class TeslaCarClimate(TeslaCarEntity, ClimateEntity): """Representation of a Tesla car climate.""" - def __init__( - self, - hass: HomeAssistant, - car: TeslaCar, - coordinator: TeslaDataUpdateCoordinator, - ) -> None: - """Initialize climate entity.""" - super().__init__(hass, car, coordinator) - self.type = "HVAC (climate) system" + type = "HVAC (climate) system" @property def supported_features(self): diff --git a/custom_components/tesla_custom/cover.py b/custom_components/tesla_custom/cover.py index 81bcd8fe..7b2c453e 100644 --- a/custom_components/tesla_custom/cover.py +++ b/custom_components/tesla_custom/cover.py @@ -7,9 +7,7 @@ CoverEntityFeature, ) from homeassistant.core import HomeAssistant -from teslajsonpy.car import TeslaCar -from . import TeslaDataUpdateCoordinator from .base import TeslaCarEntity from .const import DOMAIN @@ -36,20 +34,10 @@ async def async_setup_entry(hass: HomeAssistant, config_entry, async_add_entitie class TeslaCarChargerDoor(TeslaCarEntity, CoverEntity): """Representation of a Tesla car charger door cover.""" - def __init__( - self, - hass: HomeAssistant, - car: TeslaCar, - coordinator: TeslaDataUpdateCoordinator, - ) -> None: - """Initialize charger door cover entity.""" - super().__init__(hass, car, coordinator) - self.type = "charger door" - self._attr_device_class = CoverDeviceClass.DOOR - self._attr_icon = "mdi:ev-plug-tesla" - self._attr_supported_features = ( - CoverEntityFeature.OPEN | CoverEntityFeature.CLOSE - ) + type = "charger door" + _attr_device_class = CoverDeviceClass.DOOR + _attr_icon = "mdi:ev-plug-tesla" + _attr_supported_features = CoverEntityFeature.OPEN | CoverEntityFeature.CLOSE async def async_close_cover(self, **kwargs): """Send close cover command.""" @@ -72,14 +60,9 @@ def is_closed(self): class TeslaCarFrunk(TeslaCarEntity, CoverEntity): """Representation of a Tesla car frunk lock.""" - def __init__( - self, hass: HomeAssistant, car: dict, coordinator: TeslaDataUpdateCoordinator - ) -> None: - """Initialize frunk lock entity.""" - super().__init__(hass, car, coordinator) - self.type = "frunk" - self._attr_device_class = CoverDeviceClass.DOOR - self._attr_icon = "mdi:car" + type = "frunk" + _attr_device_class = CoverDeviceClass.DOOR + _attr_icon = "mdi:car" async def async_close_cover(self, **kwargs): """Send close cover command.""" @@ -113,17 +96,9 @@ def supported_features(self) -> int: class TeslaCarTrunk(TeslaCarEntity, CoverEntity): """Representation of a Tesla car trunk cover.""" - def __init__( - self, - hass: HomeAssistant, - car: TeslaCar, - coordinator: TeslaDataUpdateCoordinator, - ) -> None: - """Initialize trunk cover entity.""" - super().__init__(hass, car, coordinator) - self.type = "trunk" - self._attr_device_class = CoverDeviceClass.DOOR - self._attr_icon = "mdi:car-back" + type = "trunk" + _attr_device_class = CoverDeviceClass.DOOR + _attr_icon = "mdi:car-back" async def async_close_cover(self, **kwargs): """Send close cover command.""" @@ -156,20 +131,10 @@ def supported_features(self) -> int: class TeslaCarWindows(TeslaCarEntity, CoverEntity): """Representation of a Tesla car window cover.""" - def __init__( - self, - hass: HomeAssistant, - car: TeslaCar, - coordinator: TeslaDataUpdateCoordinator, - ) -> None: - """Initialize window cover entity.""" - super().__init__(hass, car, coordinator) - self.type = "windows" - self._attr_device_class = CoverDeviceClass.AWNING - self._attr_icon = "mdi:car-door" - self._attr_supported_features = ( - CoverEntityFeature.OPEN | CoverEntityFeature.CLOSE - ) + type = "windows" + _attr_device_class = CoverDeviceClass.AWNING + _attr_icon = "mdi:car-door" + _attr_supported_features = CoverEntityFeature.OPEN | CoverEntityFeature.CLOSE async def async_close_cover(self, **kwargs): """Send close cover command.""" diff --git a/custom_components/tesla_custom/device_tracker.py b/custom_components/tesla_custom/device_tracker.py index ccfcaa36..2696c26e 100644 --- a/custom_components/tesla_custom/device_tracker.py +++ b/custom_components/tesla_custom/device_tracker.py @@ -4,9 +4,7 @@ from homeassistant.components.device_tracker import SOURCE_TYPE_GPS from homeassistant.components.device_tracker.config_entry import TrackerEntity from homeassistant.core import HomeAssistant -from teslajsonpy.car import TeslaCar -from . import TeslaDataUpdateCoordinator from .base import TeslaCarEntity from .const import DOMAIN @@ -31,15 +29,7 @@ async def async_setup_entry(hass: HomeAssistant, config_entry, async_add_entitie class TeslaCarLocation(TeslaCarEntity, TrackerEntity): """Representation of a Tesla car location device tracker.""" - def __init__( - self, - hass: HomeAssistant, - car: TeslaCar, - coordinator: TeslaDataUpdateCoordinator, - ) -> None: - """Initialize car location entity.""" - super().__init__(hass, car, coordinator) - self.type = "location tracker" + type = "location tracker" @property def source_type(self): @@ -73,15 +63,7 @@ def force_update(self): class TeslaCarDestinationLocation(TeslaCarEntity, TrackerEntity): """Representation of a Tesla car destination location device tracker.""" - def __init__( - self, - hass: HomeAssistant, - car: TeslaCar, - coordinator: TeslaDataUpdateCoordinator, - ) -> None: - """Initialize car destination location entity.""" - super().__init__(hass, car, coordinator) - self.type = "destination location tracker" + type = "destination location tracker" @property def source_type(self): diff --git a/custom_components/tesla_custom/lock.py b/custom_components/tesla_custom/lock.py index 6e3b5012..4ff54334 100644 --- a/custom_components/tesla_custom/lock.py +++ b/custom_components/tesla_custom/lock.py @@ -3,9 +3,7 @@ from homeassistant.components.lock import LockEntity, LockEntityFeature from homeassistant.core import HomeAssistant -from teslajsonpy.car import TeslaCar -from . import TeslaDataUpdateCoordinator from .base import TeslaCarEntity from .const import DOMAIN @@ -30,15 +28,7 @@ async def async_setup_entry(hass: HomeAssistant, config_entry, async_add_entitie class TeslaCarDoors(TeslaCarEntity, LockEntity): """Representation of a Tesla car door lock.""" - def __init__( - self, - hass: HomeAssistant, - car: TeslaCar, - coordinator: TeslaDataUpdateCoordinator, - ) -> None: - """Initialize door lock entity.""" - super().__init__(hass, car, coordinator) - self.type = "doors" + type = "doors" async def async_lock(self, **kwargs): """Send lock command.""" @@ -61,17 +51,9 @@ def is_locked(self): class TeslaCarChargePortLatch(TeslaCarEntity, LockEntity): """Representation of a Tesla charge port latch.""" - def __init__( - self, - hass: HomeAssistant, - car: TeslaCar, - coordinator: TeslaDataUpdateCoordinator, - ) -> None: - """Initialize charge port latch (lock) entity.""" - super().__init__(hass, car, coordinator) - self.type = "charge port latch" - self._attr_icon = "mdi:ev-plug-tesla" - self._attr_supported_features = LockEntityFeature.OPEN + type = "charge port latch" + _attr_icon = "mdi:ev-plug-tesla" + _attr_supported_features = LockEntityFeature.OPEN async def async_open(self, **kwargs): """Send open command.""" @@ -90,8 +72,6 @@ async def async_lock(self, **kwargs): _LOGGER.debug("Locking charge port latch not possible with Tesla's API.") @property - def is_locked(self): + def is_locked(self) -> bool: """Return True if charge port latch is engaged.""" - if self._car.charge_port_latch == "Engaged": - return True - return False + return self._car.charge_port_latch == "Engaged" diff --git a/custom_components/tesla_custom/number.py b/custom_components/tesla_custom/number.py index 22224b21..d4d7c30d 100644 --- a/custom_components/tesla_custom/number.py +++ b/custom_components/tesla_custom/number.py @@ -3,16 +3,13 @@ from homeassistant.const import ELECTRIC_CURRENT_AMPERE, PERCENTAGE from homeassistant.core import HomeAssistant from homeassistant.helpers.icon import icon_for_battery_level -from teslajsonpy.car import TeslaCar from teslajsonpy.const import ( BACKUP_RESERVE_MAX, BACKUP_RESERVE_MIN, CHARGE_CURRENT_MIN, RESOURCE_TYPE_BATTERY, ) -from teslajsonpy.energy import PowerwallSite -from . import TeslaDataUpdateCoordinator from .base import TeslaCarEntity, TeslaEnergyEntity from .const import DOMAIN @@ -41,18 +38,10 @@ async def async_setup_entry(hass: HomeAssistant, config_entry, async_add_entitie class TeslaCarChargeLimit(TeslaCarEntity, NumberEntity): """Representation of a Tesla car charge limit number.""" - def __init__( - self, - hass: HomeAssistant, - car: TeslaCar, - coordinator: TeslaDataUpdateCoordinator, - ) -> None: - """Initialize charge limit entity.""" - super().__init__(hass, car, coordinator) - self.type = "charge limit" - self._attr_icon = "mdi:ev-station" - self._attr_mode = NumberMode.AUTO - self._attr_native_step = 1 + type = "charge limit" + _attr_icon = "mdi:ev-station" + _attr_mode = NumberMode.AUTO + _attr_native_step = 1 async def async_set_native_value(self, value: int) -> None: """Update charge limit.""" @@ -83,18 +72,10 @@ def native_unit_of_measurement(self) -> str: class TeslaCarChargingAmps(TeslaCarEntity, NumberEntity): """Representation of a Tesla car charging amps number.""" - def __init__( - self, - hass: HomeAssistant, - car: TeslaCar, - coordinator: TeslaDataUpdateCoordinator, - ) -> None: - """Initialize charging amps entity.""" - super().__init__(hass, car, coordinator) - self.type = "charging amps" - self._attr_icon = "mdi:ev-station" - self._attr_mode = NumberMode.AUTO - self._attr_native_step = 1 + type = "charging amps" + _attr_icon = "mdi:ev-station" + _attr_mode = NumberMode.AUTO + _attr_native_step = 1 async def async_set_native_value(self, value: int) -> None: """Update charging amps.""" @@ -125,18 +106,10 @@ def native_unit_of_measurement(self) -> str: class TeslaEnergyBackupReserve(TeslaEnergyEntity, NumberEntity): """Representation of a Tesla energy backup reserve number.""" - def __init__( - self, - hass: HomeAssistant, - energysite: PowerwallSite, - coordinator: TeslaDataUpdateCoordinator, - ) -> None: - """Initialize backup reserve entity.""" - super().__init__(hass, energysite, coordinator) - self.type = "backup reserve" - self._attr_icon = "mdi:battery" - self._attr_mode = NumberMode.AUTO - self._attr_native_step = 1 + type = "backup reserve" + _attr_icon = "mdi:battery" + _attr_mode = NumberMode.AUTO + _attr_native_step = 1 async def async_set_native_value(self, value: int) -> None: """Update backup reserve percentage.""" diff --git a/custom_components/tesla_custom/select.py b/custom_components/tesla_custom/select.py index 54536c01..ba19e45c 100644 --- a/custom_components/tesla_custom/select.py +++ b/custom_components/tesla_custom/select.py @@ -6,7 +6,6 @@ from homeassistant.helpers.entity import EntityCategory from teslajsonpy.car import TeslaCar from teslajsonpy.const import RESOURCE_TYPE_BATTERY -from teslajsonpy.energy import PowerwallSite, SolarPowerwallSite from . import TeslaDataUpdateCoordinator from .base import TeslaCarEntity, TeslaEnergyEntity @@ -115,6 +114,8 @@ async def async_setup_entry(hass: HomeAssistant, config_entry, async_add_entitie class TeslaCarHeatedSeat(TeslaCarEntity, SelectEntity): """Representation of a Tesla car heated seat select.""" + _attr_icon = "mdi:car-seat-heater" + def __init__( self, hass: HomeAssistant, @@ -126,7 +127,6 @@ def __init__( super().__init__(hass, car, coordinator) self._seat_name = seat_name self.type = f"heated seat {seat_name}" - self._attr_icon = "mdi:car-seat-heater" if SEAT_ID_MAP[self._seat_name] < 2: self._is_auto_available = True else: @@ -187,6 +187,9 @@ def options(self): class TeslaCarHeatedSteeringWheel(TeslaCarEntity, SelectEntity): """Representation of a Tesla car heated steering wheel select.""" + type = "heated steering wheel" + _attr_icon = "mdi:steering" + def __init__( self, hass: HomeAssistant, @@ -195,8 +198,6 @@ def __init__( ): """Initialize heated seat entity.""" super().__init__(hass, car, coordinator) - self.type = "heated steering wheel" - self._attr_icon = "mdi:steering" self._enabled_by_default = self._car.steering_wheel_heater async def async_select_option(self, option: str, **kwargs): @@ -252,18 +253,10 @@ def available(self) -> bool: class TeslaCarCabinOverheatProtection(TeslaCarEntity, SelectEntity): """Representation of a Tesla car cabin overheat protection select.""" - def __init__( - self, - hass: HomeAssistant, - car: TeslaCar, - coordinator: TeslaDataUpdateCoordinator, - ): - """Initialize cabin overheat protection entity.""" - super().__init__(hass, car, coordinator) - self.type = "cabin overheat protection" - self._attr_options = CABIN_OPTIONS - self._attr_entity_category = EntityCategory.CONFIG - self._attr_icon = "mdi:sun-thermometer" + type = "cabin overheat protection" + _attr_options = CABIN_OPTIONS + _attr_entity_category = EntityCategory.CONFIG + _attr_icon = "mdi:sun-thermometer" async def async_select_option(self, option: str, **kwargs): """Change the selected option.""" @@ -279,16 +272,8 @@ def current_option(self): class TeslaEnergyGridCharging(TeslaEnergyEntity, SelectEntity): """Representation of a Tesla energy site grid charging select.""" - def __init__( - self, - hass: HomeAssistant, - energysite: SolarPowerwallSite, - coordinator: TeslaDataUpdateCoordinator, - ): - """Initialize grid charging entity.""" - super().__init__(hass, energysite, coordinator) - self.type = "grid charging" - self._attr_options = GRID_CHARGING + type = "grid charging" + _attr_options = GRID_CHARGING async def async_select_option(self, option: str, **kwargs): """Change the selected option.""" @@ -317,17 +302,9 @@ def icon(self): class TeslaEnergyExportRule(TeslaEnergyEntity, SelectEntity): """Representation of a Tesla energy site energy export rule select.""" - def __init__( - self, - hass: HomeAssistant, - energysite: SolarPowerwallSite, - coordinator: TeslaDataUpdateCoordinator, - ): - """Initialize energy export rule entity.""" - super().__init__(hass, energysite, coordinator) - self.type = "energy exports" - self._attr_options = EXPORT_RULE - self._attr_icon = "mdi:home-export-outline" + type = "energy exports" + _attr_options = EXPORT_RULE + _attr_icon = "mdi:home-export-outline" async def async_select_option(self, option: str, **kwargs): """Change the selected option.""" @@ -351,17 +328,9 @@ def current_option(self) -> str: class TeslaEnergyOperationMode(TeslaEnergyEntity, SelectEntity): """Representation of a Tesla energy site operation mode select.""" - def __init__( - self, - hass: HomeAssistant, - energysite: PowerwallSite, - coordinator: TeslaDataUpdateCoordinator, - ): - """Initialize operation mode entity.""" - super().__init__(hass, energysite, coordinator) - self.type = "operation mode" - self._attr_options = OPERATION_MODE - self._attr_icon = "mdi:home-battery" + type = "operation mode" + _attr_options = OPERATION_MODE + _attr_icon = "mdi:home-battery" async def async_select_option(self, option: str, **kwargs): """Change the selected option.""" diff --git a/custom_components/tesla_custom/sensor.py b/custom_components/tesla_custom/sensor.py index 65e511ad..1c5985ba 100644 --- a/custom_components/tesla_custom/sensor.py +++ b/custom_components/tesla_custom/sensor.py @@ -28,7 +28,7 @@ from homeassistant.util.unit_conversion import DistanceConverter from teslajsonpy.car import TeslaCar from teslajsonpy.const import RESOURCE_TYPE_BATTERY, RESOURCE_TYPE_SOLAR -from teslajsonpy.energy import EnergySite, PowerwallSite +from teslajsonpy.energy import EnergySite from . import TeslaDataUpdateCoordinator from .base import TeslaCarEntity, TeslaEnergyEntity @@ -110,19 +110,11 @@ async def async_setup_entry(hass: HomeAssistant, config_entry, async_add_entitie class TeslaCarBattery(TeslaCarEntity, SensorEntity): """Representation of the Tesla car battery sensor.""" - def __init__( - self, - hass: HomeAssistant, - car: TeslaCar, - coordinator: TeslaDataUpdateCoordinator, - ) -> None: - """Initialize the Sensor Entity.""" - super().__init__(hass, car, coordinator) - self.type = "battery" - self._attr_device_class = SensorDeviceClass.BATTERY - self._attr_state_class = SensorStateClass.MEASUREMENT - self._attr_native_unit_of_measurement = PERCENTAGE - self._attr_icon = "mdi:battery" + type = "battery" + _attr_device_class = SensorDeviceClass.BATTERY + _attr_state_class = SensorStateClass.MEASUREMENT + _attr_native_unit_of_measurement = PERCENTAGE + _attr_icon = "mdi:battery" @staticmethod def has_battery() -> bool: @@ -155,19 +147,11 @@ def extra_state_attributes(self): class TeslaCarChargerEnergy(TeslaCarEntity, SensorEntity): """Representation of a Tesla car energy added sensor.""" - def __init__( - self, - hass: HomeAssistant, - car: TeslaCar, - coordinator: TeslaDataUpdateCoordinator, - ) -> None: - """Initialize energy added entity.""" - super().__init__(hass, car, coordinator) - self.type = "energy added" - self._attr_device_class = SensorDeviceClass.ENERGY - self._attr_state_class = SensorStateClass.TOTAL_INCREASING - self._attr_native_unit_of_measurement = ENERGY_KILO_WATT_HOUR - self._attr_icon = "mdi:lightning-bolt" + type = "energy added" + _attr_device_class = SensorDeviceClass.ENERGY + _attr_state_class = SensorStateClass.TOTAL_INCREASING + _attr_native_unit_of_measurement = ENERGY_KILO_WATT_HOUR + _attr_icon = "mdi:lightning-bolt" @property def native_value(self) -> float: @@ -202,18 +186,10 @@ def extra_state_attributes(self): class TeslaCarChargerPower(TeslaCarEntity, SensorEntity): """Representation of a Tesla car charger power.""" - def __init__( - self, - hass: HomeAssistant, - car: TeslaCar, - coordinator: TeslaDataUpdateCoordinator, - ) -> None: - """Initialize energy added entity.""" - super().__init__(hass, car, coordinator) - self.type = "charger power" - self._attr_device_class = SensorDeviceClass.POWER - self._attr_state_class = SensorStateClass.MEASUREMENT - self._attr_native_unit_of_measurement = POWER_KILO_WATT + type = "charger power" + _attr_device_class = SensorDeviceClass.POWER + _attr_state_class = SensorStateClass.MEASUREMENT + _attr_native_unit_of_measurement = POWER_KILO_WATT @property def native_value(self) -> int: @@ -234,19 +210,11 @@ def extra_state_attributes(self): class TeslaCarChargerRate(TeslaCarEntity, SensorEntity): """Representation of the Tesla car charging rate.""" - def __init__( - self, - hass: HomeAssistant, - car: TeslaCar, - coordinator: TeslaDataUpdateCoordinator, - ) -> None: - """Initialize charging rate entity.""" - super().__init__(hass, car, coordinator) - self.type = "charging rate" - self._attr_device_class = SensorDeviceClass.SPEED - self._attr_state_class = SensorStateClass.MEASUREMENT - self._attr_native_unit_of_measurement = SPEED_MILES_PER_HOUR - self._attr_icon = "mdi:speedometer" + type = "charging rate" + _attr_device_class = SensorDeviceClass.SPEED + _attr_state_class = SensorStateClass.MEASUREMENT + _attr_native_unit_of_measurement = SPEED_MILES_PER_HOUR + _attr_icon = "mdi:speedometer" @property def native_value(self) -> float: @@ -269,19 +237,11 @@ def extra_state_attributes(self): class TeslaCarOdometer(TeslaCarEntity, SensorEntity): """Representation of the Tesla car odometer sensor.""" - def __init__( - self, - hass: HomeAssistant, - car: TeslaCar, - coordinator: TeslaDataUpdateCoordinator, - ) -> None: - """Initialize odometer entity.""" - super().__init__(hass, car, coordinator) - self.type = "odometer" - self._attr_device_class = SensorDeviceClass.DISTANCE - self._attr_state_class = SensorStateClass.TOTAL_INCREASING - self._attr_native_unit_of_measurement = LENGTH_MILES - self._attr_icon = "mdi:counter" + type = "odometer" + _attr_device_class = SensorDeviceClass.DISTANCE + _attr_state_class = SensorStateClass.TOTAL_INCREASING + _attr_native_unit_of_measurement = LENGTH_MILES + _attr_icon = "mdi:counter" @property def native_value(self) -> float: @@ -297,17 +257,9 @@ def native_value(self) -> float: class TeslaCarShiftState(TeslaCarEntity, SensorEntity): """Representation of the Tesla car Shift State sensor.""" - def __init__( - self, - hass: HomeAssistant, - car: TeslaCar, - coordinator: TeslaDataUpdateCoordinator, - ) -> None: - """Initialize odometer entity.""" - super().__init__(hass, car, coordinator) - self.type = "shift state" - self._attr_device_class = SensorDeviceClass.ENUM - self._attr_icon = "mdi:car-shift-pattern" + type = "shift state" + _attr_device_class = SensorDeviceClass.ENUM + _attr_icon = "mdi:car-shift-pattern" @property def native_value(self) -> float: @@ -339,19 +291,11 @@ def extra_state_attributes(self): class TeslaCarRange(TeslaCarEntity, SensorEntity): """Representation of the Tesla car range sensor.""" - def __init__( - self, - hass: HomeAssistant, - car: TeslaCar, - coordinator: TeslaDataUpdateCoordinator, - ) -> None: - """Initialize range entity.""" - super().__init__(hass, car, coordinator) - self.type = "range" - self._attr_device_class = SensorDeviceClass.DISTANCE - self._attr_state_class = SensorStateClass.MEASUREMENT - self._attr_native_unit_of_measurement = LENGTH_MILES - self._attr_icon = "mdi:gauge" + type = "range" + _attr_device_class = SensorDeviceClass.DISTANCE + _attr_state_class = SensorStateClass.MEASUREMENT + _attr_native_unit_of_measurement = LENGTH_MILES + _attr_icon = "mdi:gauge" @property def native_value(self) -> float: @@ -389,6 +333,12 @@ def extra_state_attributes(self): class TeslaCarTemp(TeslaCarEntity, SensorEntity): """Representation of a Tesla car temp sensor.""" + type = "temperature" + _attr_device_class = SensorDeviceClass.TEMPERATURE + _attr_state_class = SensorStateClass.MEASUREMENT + _attr_native_unit_of_measurement = TEMP_CELSIUS + _attr_icon = "mdi:thermometer" + def __init__( self, hass: HomeAssistant, @@ -399,19 +349,12 @@ def __init__( ) -> None: """Initialize temp entity.""" super().__init__(hass, car, coordinator) - self.type = "temperature" self.inside = inside - if inside is True: self.type += " (inside)" else: self.type += " (outside)" - self._attr_device_class = SensorDeviceClass.TEMPERATURE - self._attr_state_class = SensorStateClass.MEASUREMENT - self._attr_native_unit_of_measurement = TEMP_CELSIUS - self._attr_icon = "mdi:thermometer" - @property def native_value(self) -> float: """Return car temperature.""" @@ -424,6 +367,10 @@ def native_value(self) -> float: class TeslaEnergyPowerSensor(TeslaEnergyEntity, SensorEntity): """Representation of a Tesla energy power sensor.""" + _attr_device_class = SensorDeviceClass.POWER + _attr_state_class = SensorStateClass.MEASUREMENT + _attr_native_unit_of_measurement = POWER_WATT + def __init__( self, hass: HomeAssistant, @@ -434,10 +381,6 @@ def __init__( """Initialize power sensor.""" super().__init__(hass, energysite, coordinator) self.type = sensor_type - self._attr_device_class = SensorDeviceClass.POWER - self._attr_state_class = SensorStateClass.MEASUREMENT - self._attr_native_unit_of_measurement = POWER_WATT - if self.type == "solar power": self._attr_icon = "mdi:solar-power-variant" if self.type == "grid power": @@ -464,18 +407,10 @@ def native_value(self) -> float: class TeslaEnergyBattery(TeslaEnergyEntity, SensorEntity): """Representation of the Tesla energy battery sensor.""" - def __init__( - self, - hass: HomeAssistant, - energysite: PowerwallSite, - coordinator: TeslaDataUpdateCoordinator, - ) -> None: - """Initialize battery sensor entity.""" - super().__init__(hass, energysite, coordinator) - self.type = "battery" - self._attr_device_class = SensorDeviceClass.BATTERY - self._attr_state_class = SensorStateClass.MEASUREMENT - self._attr_native_unit_of_measurement = PERCENTAGE + type = "battery" + _attr_device_class = SensorDeviceClass.BATTERY + _attr_state_class = SensorStateClass.MEASUREMENT + _attr_native_unit_of_measurement = PERCENTAGE @staticmethod def has_battery() -> bool: @@ -500,18 +435,10 @@ def icon(self): class TeslaEnergyBatteryRemaining(TeslaEnergyEntity, SensorEntity): """Representation of a Tesla energy battery remaining sensor.""" - def __init__( - self, - hass: HomeAssistant, - energysite: PowerwallSite, - coordinator: TeslaDataUpdateCoordinator, - ) -> None: - """Initialize battery remaining entity.""" - super().__init__(hass, energysite, coordinator) - self.type = "battery remaining" - self._attr_device_class = SensorDeviceClass.ENERGY_STORAGE - self._attr_state_class = SensorStateClass.MEASUREMENT - self._attr_native_unit_of_measurement = ENERGY_WATT_HOUR + type = "battery remaining" + _attr_device_class = SensorDeviceClass.ENERGY_STORAGE + _attr_state_class = SensorStateClass.MEASUREMENT + _attr_native_unit_of_measurement = ENERGY_WATT_HOUR @property def native_value(self) -> int: @@ -531,18 +458,10 @@ def icon(self): class TeslaEnergyBackupReserve(TeslaEnergyEntity, SensorEntity): """Representation of a Tesla energy backup reserve sensor.""" - def __init__( - self, - hass: HomeAssistant, - energysite: PowerwallSite, - coordinator: TeslaDataUpdateCoordinator, - ) -> None: - """Initialize backup energy reserve entity.""" - super().__init__(hass, energysite, coordinator) - self.type = "backup reserve" - self._attr_device_class = SensorDeviceClass.BATTERY - self._attr_state_class = SensorStateClass.MEASUREMENT - self._attr_native_unit_of_measurement = PERCENTAGE + type = "backup reserve" + _attr_device_class = SensorDeviceClass.BATTERY + _attr_state_class = SensorStateClass.MEASUREMENT + _attr_native_unit_of_measurement = PERCENTAGE @property def native_value(self) -> int: @@ -558,20 +477,12 @@ def icon(self): class TeslaCarTimeChargeComplete(TeslaCarEntity, SensorEntity): """Representation of the Tesla car time charge complete.""" - def __init__( - self, - hass: HomeAssistant, - car: TeslaCar, - coordinator: TeslaDataUpdateCoordinator, - ) -> None: - """Initialize time charge complete entity.""" - super().__init__(hass, car, coordinator) - self.type = "time charge complete" - self._attr_device_class = SensorDeviceClass.TIMESTAMP - self._attr_icon = "mdi:timer-plus" - self._value: Optional[datetime] = None - self._last_known_value: Optional[int] = None - self._last_update_time: Optional[datetime] = None + type = "time charge complete" + _attr_device_class = SensorDeviceClass.TIMESTAMP + _attr_icon = "mdi:timer-plus" + _value: Optional[datetime] = None + _last_known_value: Optional[int] = None + _last_update_time: Optional[datetime] = None @property def native_value(self) -> Optional[datetime]: @@ -616,6 +527,12 @@ def extra_state_attributes(self): class TeslaCarTpmsPressureSensor(TeslaCarEntity, SensorEntity): """Representation of the Tesla car TPMS Pressure sensor.""" + _attr_device_class = SensorDeviceClass.PRESSURE + _attr_state_class = SensorStateClass.MEASUREMENT + _attr_native_unit_of_measurement = PRESSURE_BAR + _attr_suggested_unit_of_measurement = PRESSURE_PSI + _attr_icon = "mdi:gauge-full" + def __init__( self, hass: HomeAssistant, @@ -627,11 +544,6 @@ def __init__( super().__init__(hass, car, coordinator) self._tpms_sensor = tpms_sensor self.type = tpms_sensor - self._attr_device_class = SensorDeviceClass.PRESSURE - self._attr_state_class = SensorStateClass.MEASUREMENT - self._attr_native_unit_of_measurement = PRESSURE_BAR - self._attr_suggested_unit_of_measurement = PRESSURE_PSI - self._attr_icon = "mdi:gauge-full" @property def native_value(self) -> float: @@ -657,20 +569,12 @@ def extra_state_attributes(self): class TeslaCarArrivalTime(TeslaCarEntity, SensorEntity): """Representation of the Tesla car route arrival time.""" - def __init__( - self, - hass: HomeAssistant, - car: TeslaCar, - coordinator: TeslaDataUpdateCoordinator, - ) -> None: - """Initialize time charge complete entity.""" - super().__init__(hass, car, coordinator) - self.type = "arrival time" - self._attr_device_class = SensorDeviceClass.TIMESTAMP - self._attr_icon = "mdi:timer-sand" - self._datetime_value: Optional[datetime] = None - self._last_known_value: Optional[int] = None - self._last_update_time: Optional[datetime] = None + type = "arrival time" + _attr_device_class = SensorDeviceClass.TIMESTAMP + _attr_icon = "mdi:timer-sand" + _datetime_value: Optional[datetime] = None + _last_known_value: Optional[int] = None + _last_update_time: Optional[datetime] = None @property def native_value(self) -> Optional[datetime]: @@ -715,19 +619,11 @@ def extra_state_attributes(self): class TeslaCarDistanceToArrival(TeslaCarEntity, SensorEntity): """Representation of the Tesla distance to arrival.""" - def __init__( - self, - hass: HomeAssistant, - car: TeslaCar, - coordinator: TeslaDataUpdateCoordinator, - ) -> None: - """Initialize distance to arrival entity.""" - super().__init__(hass, car, coordinator) - self.type = "distance to arrival" - self._attr_device_class = SensorDeviceClass.DISTANCE - self._attr_state_class = SensorStateClass.MEASUREMENT - self._attr_native_unit_of_measurement = LENGTH_MILES - self._attr_icon = "mdi:map-marker-distance" + type = "distance to arrival" + _attr_device_class = SensorDeviceClass.DISTANCE + _attr_state_class = SensorStateClass.MEASUREMENT + _attr_native_unit_of_measurement = LENGTH_MILES + _attr_icon = "mdi:map-marker-distance" @property def native_value(self) -> float: @@ -740,18 +636,10 @@ def native_value(self) -> float: class TeslaCarDataUpdateTime(TeslaCarEntity, SensorEntity): """Representation of the TeslajsonPy Last Data Update time.""" - def __init__( - self, - hass: HomeAssistant, - car: TeslaCar, - coordinator: TeslaDataUpdateCoordinator, - ) -> None: - """Initialize Last Data Update entity.""" - super().__init__(hass, car, coordinator) - self.type = "data last update time" - self._attr_device_class = SensorDeviceClass.TIMESTAMP - self._attr_entity_category = EntityCategory.DIAGNOSTIC - self._attr_icon = "mdi:timer" + type = "data last update time" + _attr_device_class = SensorDeviceClass.TIMESTAMP + _attr_entity_category = EntityCategory.DIAGNOSTIC + _attr_icon = "mdi:timer" @property def native_value(self) -> datetime: diff --git a/custom_components/tesla_custom/switch.py b/custom_components/tesla_custom/switch.py index 71ac69a2..0655ff79 100644 --- a/custom_components/tesla_custom/switch.py +++ b/custom_components/tesla_custom/switch.py @@ -34,6 +34,9 @@ async def async_setup_entry(hass: HomeAssistant, config_entry, async_add_entitie class TeslaCarHeatedSteeringWheel(TeslaCarEntity, SwitchEntity): """Representation of a Tesla car heated steering wheel switch.""" + type = "heated steering" + _attr_icon = "mdi:steering" + def __init__( self, hass: HomeAssistant, @@ -42,8 +45,6 @@ def __init__( ) -> None: """Initialize heated steering wheel entity.""" super().__init__(hass, car, coordinator) - self.type = "heated steering" - self._attr_icon = "mdi:steering" # Entity is disabled for cars with variable heated steering wheel. self._enabled_by_default = car.get_heated_steering_wheel_level() is not None @@ -71,25 +72,18 @@ async def async_turn_off(self, **kwargs): class TeslaCarPolling(TeslaCarEntity, SwitchEntity): """Representation of a polling switch.""" - def __init__( - self, - hass: HomeAssistant, - car: TeslaCar, - coordinator: TeslaDataUpdateCoordinator, - ) -> None: - """Initialize polling entity.""" - super().__init__(hass, car, coordinator) - self.type = "polling" - self._attr_icon = "mdi:car-connected" - self._attr_entity_category = EntityCategory.DIAGNOSTIC + type = "polling" + _attr_icon = "mdi:car-connected" + _attr_entity_category = EntityCategory.DIAGNOSTIC @property - def is_on(self): + def is_on(self) -> bool | None: """Return True if updates available.""" - if self._coordinator.controller.get_updates(vin=self._car.vin) is None: + controller = self._coordinator.controller + get_updates = controller.get_updates(vin=self._car.vin) + if get_updates is None: return None - - return bool(self._coordinator.controller.get_updates(vin=self._car.vin)) + return bool(get_updates) async def async_turn_on(self, **kwargs): """Send the on command.""" @@ -107,16 +101,8 @@ async def async_turn_off(self, **kwargs): class TeslaCarCharger(TeslaCarEntity, SwitchEntity): """Representation of a Tesla car charger switch.""" - def __init__( - self, - hass: HomeAssistant, - car: TeslaCar, - coordinator: TeslaDataUpdateCoordinator, - ) -> None: - """Initialize charger switch entity.""" - super().__init__(hass, car, coordinator) - self.type = "charger" - self._attr_icon = "mdi:ev-station" + type = "charger" + _attr_icon = "mdi:ev-station" @property def is_on(self): @@ -137,6 +123,9 @@ async def async_turn_off(self, **kwargs): class TeslaCarSentryMode(TeslaCarEntity, SwitchEntity): """Representation of a Tesla car sentry mode switch.""" + type = "sentry mode" + _attr_icon = "mdi:shield-car" + def __init__( self, hass: HomeAssistant, @@ -145,8 +134,6 @@ def __init__( ) -> None: """Initialize sentry mode entity.""" super().__init__(hass, car, coordinator) - self.type = "sentry mode" - self._attr_icon = "mdi:shield-car" # Entity is only enabled upon first install if sentry mode is available self._enabled_by_default = self._car.sentry_mode_available @@ -160,11 +147,7 @@ def is_on(self): """Return True if sentry mode is on.""" sentry_mode_available = self._car.sentry_mode_available sentry_mode_status = self._car.sentry_mode - - if sentry_mode_available is True and sentry_mode_status is True: - return True - - return False + return bool(sentry_mode_available and sentry_mode_status) async def async_turn_on(self, **kwargs): """Send the on command.""" @@ -180,16 +163,8 @@ async def async_turn_off(self, **kwargs): class TeslaCarValetMode(TeslaCarEntity, SwitchEntity): """Representation of a Tesla car valet mode switch.""" - def __init__( - self, - hass: HomeAssistant, - car: TeslaCar, - coordinator: TeslaDataUpdateCoordinator, - ) -> None: - """Initialize valet mode switch entity.""" - super().__init__(hass, car, coordinator) - self.type = "valet mode" - self._attr_icon = "mdi:room-service" + type = "valet mode" + _attr_icon = "mdi:room-service" @property def is_on(self): diff --git a/custom_components/tesla_custom/text.py b/custom_components/tesla_custom/text.py index 0b66d569..445d5c32 100644 --- a/custom_components/tesla_custom/text.py +++ b/custom_components/tesla_custom/text.py @@ -28,6 +28,12 @@ async def async_setup_entry(hass: HomeAssistant, config_entry, async_add_entitie class TeslaCarTeslaMateID(TeslaCarEntity, TextEntity): """Representation of a Tesla car charge limit number.""" + type = "teslamate id" + _attr_icon = "mdi:ev-station" + _attr_mode = TextMode.TEXT + _enabled_by_default = False + _attr_entity_category = EntityCategory.CONFIG + def __init__( self, hass: HomeAssistant, @@ -37,12 +43,6 @@ def __init__( ) -> None: """Initialize charge limit entity.""" super().__init__(hass, car, coordinator) - self.type = "teslamate id" - self._attr_icon = "mdi:ev-station" - self._attr_mode = TextMode.TEXT - self._enabled_by_default = False - self._attr_entity_category = EntityCategory.CONFIG - self.teslsmate = teslamate self._state = None diff --git a/custom_components/tesla_custom/update.py b/custom_components/tesla_custom/update.py index e53112e1..475a682e 100644 --- a/custom_components/tesla_custom/update.py +++ b/custom_components/tesla_custom/update.py @@ -3,9 +3,7 @@ from homeassistant.components.update import UpdateEntity, UpdateEntityFeature from homeassistant.core import HomeAssistant -from teslajsonpy.car import TeslaCar -from . import TeslaDataUpdateCoordinator from .base import TeslaCarEntity from .const import DOMAIN @@ -41,15 +39,7 @@ async def async_setup_entry(hass: HomeAssistant, config_entry, async_add_entitie class TeslaCarUpdate(TeslaCarEntity, UpdateEntity): """Representation of a Tesla car update.""" - def __init__( - self, - hass: HomeAssistant, - car: TeslaCar, - coordinator: TeslaDataUpdateCoordinator, - ) -> None: - """Initialize update entity.""" - super().__init__(hass, car, coordinator) - self.type = "software update" + type = "software update" @property def supported_features(self):