Skip to content

Commit

Permalink
Merge pull request #462 from alandtse/dev
Browse files Browse the repository at this point in the history
2022-12-26
  • Loading branch information
alandtse authored Dec 29, 2022
2 parents 857396d + 8d40beb commit 5646d01
Show file tree
Hide file tree
Showing 14 changed files with 2,060 additions and 1,876 deletions.
8 changes: 4 additions & 4 deletions custom_components/tesla_custom/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from teslajsonpy.const import RESOURCE_TYPE_BATTERY
from teslajsonpy.energy import EnergySite

from homeassistant.const import CONF_UNIT_SYSTEM_METRIC, CONF_UNIT_SYSTEM_IMPERIAL
from homeassistant.util.unit_system import METRIC_SYSTEM, US_CUSTOMARY_SYSTEM
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity import DeviceInfo
from homeassistant.helpers.update_coordinator import CoordinatorEntity
Expand Down Expand Up @@ -67,9 +67,9 @@ def __init__(
super().__init__(hass, coordinator)
self._car = car
self._unit_system = (
CONF_UNIT_SYSTEM_METRIC
if self.hass.config.units.is_metric
else CONF_UNIT_SYSTEM_IMPERIAL
METRIC_SYSTEM
if self.hass.config.units is METRIC_SYSTEM
else US_CUSTOMARY_SYSTEM
)

async def update_controller(
Expand Down
14 changes: 13 additions & 1 deletion custom_components/tesla_custom/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,8 +278,12 @@ def is_on(self):
@property
def extra_state_attributes(self):
"""Return device state attributes."""
timestamp = self._car._vehicle_data.get("charge_state", {}).get(
"scheduled_charging_start_time"
)
return {
"Scheduled charging time": self._car.scheduled_charging_start_time_app,
"Scheduled charging timestamp": timestamp,
}


Expand All @@ -301,20 +305,28 @@ def __init__(
@property
def is_on(self):
"""Return True if scheduled departure enebaled."""
if self._car.scheduled_charging_mode == "DepartBy":
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

@property
def extra_state_attributes(self):
"""Return device state attributes."""
timestamp = self._car._vehicle_data.get("charge_state", {}).get(
"scheduled_departure_time"
)
return {
"Departure time": self._car.scheduled_departure_time_minutes,
"Preconditioning enabled": self._car.is_preconditioning_enabled,
"Preconditioning weekdays only": self._car.is_preconditioning_weekday_only,
"Off peak charging enabled": self._car.is_off_peak_charging_enabled,
"Off peak charging weekdays only": self._car.is_off_peak_charging_weekday_only,
"End off peak time": self._car.off_peak_hours_end_time,
"Departure timestamp": timestamp,
}


Expand Down
27 changes: 27 additions & 0 deletions custom_components/tesla_custom/button.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ async def async_setup_entry(hass: HomeAssistant, config_entry, async_add_entitie
entities.append(TeslaCarForceDataUpdate(hass, car, coordinator))
entities.append(TeslaCarTriggerHomelink(hass, car, coordinator))
entities.append(TeslaCarRemoteStart(hass, car, coordinator))
entities.append(TeslaCarEmissionsTest(hass, car, coordinator))

async_add_entities(entities, True)

Expand Down Expand Up @@ -162,3 +163,29 @@ def __init__(
async def async_press(self):
"""Send the command."""
await self._car.remote_start()


class TeslaCarEmissionsTest(TeslaCarEntity, ButtonEntity):
"""Representation of a Tesla car emissions test button."""

def __init__(
self,
hass: HomeAssistant,
car: TeslaCar,
coordinator: TeslaDataUpdateCoordinator,
) -> 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:
"""Handle the button press."""
await self._car.remote_boombox()

@property
def available(self) -> bool:
"""Return True."""
return True
4 changes: 4 additions & 0 deletions custom_components/tesla_custom/device_tracker.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,15 @@ def source_type(self):
@property
def longitude(self):
"""Return destination longitude."""
if self._car.active_route_miles_to_arrival is None:
return None
return self._car.active_route_longitude

@property
def latitude(self):
"""Return destination latitude."""
if self._car.active_route_miles_to_arrival is None:
return None
return self._car.active_route_latitude

@property
Expand Down
5 changes: 3 additions & 2 deletions custom_components/tesla_custom/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"documentation": "https://github.com/alandtse/tesla/wiki",
"issue_tracker": "https://github.com/alandtse/tesla/issues",
"requirements": [
"teslajsonpy==3.5.1"
"teslajsonpy==3.6.0"
],
"codeowners": [
"@alandtse"
Expand All @@ -28,5 +28,6 @@
}
],
"iot_class": "cloud_polling",
"loggers": ["teslajsonpy"],
"version": "3.8.1"
}
}
13 changes: 12 additions & 1 deletion custom_components/tesla_custom/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,8 @@ def __init__(
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

@property
def native_value(self) -> Optional[datetime]:
Expand All @@ -529,8 +531,17 @@ def native_value(self) -> Optional[datetime]:
charge_hours = 0
else:
charge_hours = float(self._car.time_to_full_charge)

if self._last_known_value != charge_hours:
self._last_known_value = charge_hours
self._last_update_time = dt.utcnow()

if self._car.charging_state == "Charging" and charge_hours > 0:
new_value = dt.utcnow() + timedelta(hours=charge_hours)
new_value = (
dt.utcnow()
+ timedelta(hours=charge_hours)
- (dt.utcnow() - self._last_update_time)
)
if self._value is None or (new_value - self._value).total_seconds() >= 60:
self._value = new_value
if self._car.charging_state in ["Charging", "Complete"]:
Expand Down
4 changes: 2 additions & 2 deletions hacs.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "Tesla",
"hacs": "1.6.0",
"homeassistant": "2022.10.0",
"homeassistant": "2022.11.0",
"zip_release": true,
"filename": "tesla_custom.zip"
}
}
Loading

0 comments on commit 5646d01

Please sign in to comment.