Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: small cleanups to entity construction #670

Merged
merged 8 commits into from
Jul 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 16 additions & 12 deletions custom_components/tesla_custom/base.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -17,22 +17,23 @@ 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
) -> None:
"""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
alandtse marked this conversation as resolved.
Show resolved Hide resolved

@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.
Expand Down Expand Up @@ -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
Expand All @@ -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
)


Expand Down
212 changes: 52 additions & 160 deletions custom_components/tesla_custom/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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):
Expand All @@ -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):
Expand All @@ -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):
Expand All @@ -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):
Expand All @@ -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):
Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -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):
Expand All @@ -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):
Expand All @@ -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):
Expand All @@ -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):
Expand All @@ -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):
Expand Down
Loading
Loading