Skip to content

Commit

Permalink
fix: don't remove entities when car is asleep
Browse files Browse the repository at this point in the history
  • Loading branch information
carleeno committed Nov 21, 2022
1 parent 00eeaa5 commit 0f770dc
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 14 deletions.
3 changes: 2 additions & 1 deletion custom_components/tesla_custom/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from homeassistant.helpers.entity import DeviceInfo
from homeassistant.helpers.update_coordinator import CoordinatorEntity
from homeassistant.util import slugify
from homeassistant.util.unit_system import METRIC_SYSTEM

from . import TeslaDataUpdateCoordinator
from .const import ATTRIBUTION, DOMAIN
Expand Down Expand Up @@ -68,7 +69,7 @@ def __init__(
self._car = car
self._unit_system = (
CONF_UNIT_SYSTEM_METRIC
if self.hass.config.units.is_metric
if self.hass.config.units is METRIC_SYSTEM
else CONF_UNIT_SYSTEM_IMPERIAL
)

Expand Down
10 changes: 8 additions & 2 deletions custom_components/tesla_custom/button.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ async def async_setup_entry(hass: HomeAssistant, config_entry, async_add_entitie
entities.append(TeslaCarFlashLights(hass, car, coordinator))
entities.append(TeslaCarWakeUp(hass, car, coordinator))
entities.append(TeslaCarForceDataUpdate(hass, car, coordinator))
if car.homelink_device_count:
entities.append(TeslaCarTriggerHomelink(hass, car, coordinator))
entities.append(TeslaCarTriggerHomelink(hass, car, coordinator))

async_add_entities(entities, True)

Expand Down Expand Up @@ -132,6 +131,13 @@ def __init__(
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

@property
def available(self) -> bool:
"""Return True if Homelink devices are nearby"""
return super().available and self._car.homelink_nearby

async def async_press(self):
"""Send the command."""
Expand Down
2 changes: 1 addition & 1 deletion 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.2.0"
"teslajsonpy==3.2.1"
],
"codeowners": [
"@alandtse"
Expand Down
20 changes: 16 additions & 4 deletions custom_components/tesla_custom/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,8 @@ async def async_setup_entry(hass: HomeAssistant, config_entry, async_add_entitie
entities = []

for car in cars.values():
if car.steering_wheel_heater:
entities.append(TeslaCarHeatedSteeringWheel(hass, car, coordinator))
if car.sentry_mode_available:
entities.append(TeslaCarSentryMode(hass, car, coordinator))
entities.append(TeslaCarHeatedSteeringWheel(hass, car, coordinator))
entities.append(TeslaCarSentryMode(hass, car, coordinator))
entities.append(TeslaCarPolling(hass, car, coordinator))
entities.append(TeslaCarCharger(hass, car, coordinator))

Expand All @@ -44,6 +42,13 @@ def __init__(
super().__init__(hass, car, coordinator)
self.type = "heated steering"
self._attr_icon = "mdi:steering"
# Entity is only enabled upon first install if steering wheel heater is available
self._enabled_by_default = self._car.steering_wheel_heater

@property
def available(self) -> bool:
"""Return True if steering wheel heater is available"""
return super().available and self._car.steering_wheel_heater

@property
def is_on(self):
Expand Down Expand Up @@ -140,6 +145,13 @@ def __init__(
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

@property
def available(self) -> bool:
"""Return True if sentry mode switch is available"""
return super().available and self._car.sentry_mode_available

@property
def is_on(self):
Expand Down
8 changes: 4 additions & 4 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ license = "Apache-2.0"

[tool.poetry.dependencies]
python = "^3.10"
teslajsonpy = "^3.2.0"
teslajsonpy = "^3.2.1"

[tool.poetry.dev-dependencies]
homeassistant = ">=2021.10.0"
Expand Down
38 changes: 37 additions & 1 deletion tests/mock_data/car.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,5 +260,41 @@
"charge_state": None,
"gui_settings": None,
"vehicle_state": None,
"vehicle_config": None,
"vehicle_config": {
"can_accept_navigation_requests": True,
"can_actuate_trunks": True,
"car_special_type": "base",
"car_type": "models",
"charge_port_type": "US",
"dashcam_clip_save_supported": False,
"default_charge_to_max": False,
"driver_assist": "MonoCam",
"ece_restrictions": False,
"efficiency_package": "Default",
"eu_vehicle": False,
"exterior_color": "White",
"front_drive_unit": "NoneOrSmall",
"has_air_suspension": False,
"has_ludicrous_mode": False,
"has_seat_cooling": False,
"headlamp_type": "Hid",
"interior_trim_type": "AllBlack",
"motorized_charge_port": True,
"plg": True,
"pws": False,
"rear_drive_unit": "Small",
"rear_seat_heaters": 0,
"rear_seat_type": 1,
"rhd": False,
"roof_color": "Colored",
"seat_type": 1,
"spoiler_type": "None",
"sun_roof_installed": 0,
"third_row_seats": "None",
"timestamp": 1661641175269,
"trim_badging": "85d",
"use_range_badging": False,
"utc_offset": -25200,
"wheel_type": "Base19",
},
}

0 comments on commit 0f770dc

Please sign in to comment.