Skip to content

Commit

Permalink
Add scoped supported features
Browse files Browse the repository at this point in the history
  • Loading branch information
Bre77 committed Feb 1, 2024
1 parent 3c2dc1b commit 6c30ae5
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 17 deletions.
3 changes: 3 additions & 0 deletions custom_components/teslemetry/button.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
from collections.abc import Callable
from dataclasses import dataclass

from tesla_fleet_api.const import Scopes

from homeassistant.components.button import ButtonEntity, ButtonEntityDescription
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
Expand Down Expand Up @@ -60,6 +62,7 @@ async def async_setup_entry(
TeslemetryButtonEntity(vehicle, description)
for vehicle in data.vehicles
for description in DESCRIPTIONS
if Scopes.VEHICLE_CMDS in data.scopes
)


Expand Down
23 changes: 17 additions & 6 deletions custom_components/teslemetry/climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from __future__ import annotations

from typing import Any

from tesla_fleet_api.const import Scopes
from homeassistant.components.climate import (
ClimateEntity,
ClimateEntityFeature,
Expand All @@ -16,7 +16,7 @@
from .const import DOMAIN, TeslemetryClimateSide
from .context import handle_command
from .entity import TeslemetryVehicleEntity

from .models import TeslemetryVehicleData

async def async_setup_entry(
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
Expand All @@ -25,7 +25,7 @@ async def async_setup_entry(
data = hass.data[DOMAIN][entry.entry_id]

async_add_entities(
TeslemetryClimateEntity(vehicle, TeslemetryClimateSide.DRIVER)
TeslemetryClimateEntity(vehicle, TeslemetryClimateSide.DRIVER, data.scopes)
for vehicle in data.vehicles
)

Expand All @@ -38,11 +38,22 @@ class TeslemetryClimateEntity(TeslemetryVehicleEntity, ClimateEntity):
_attr_max_temp = 28
_attr_temperature_unit = UnitOfTemperature.CELSIUS
_attr_hvac_modes = [HVACMode.HEAT_COOL, HVACMode.OFF]
_attr_supported_features = (
ClimateEntityFeature.TARGET_TEMPERATURE | ClimateEntityFeature.PRESET_MODE
)
_attr_preset_modes = ["off", "keep", "dog", "camp"]

def __init__(
self,
vehicle: TeslemetryVehicleData,
side: TeslemetryClimateSide,
scopes: Scopes,
) -> None:
"""Initialize the climate."""
super().__init__(vehicle, side)
print(scopes)
print(Scopes.VEHICLE_CMDS in scopes)
if Scopes.VEHICLE_CMDS in scopes:
self._attr_supported_features = ClimateEntityFeature.TARGET_TEMPERATURE | ClimateEntityFeature.PRESET_MODE
print(self._attr_supported_features)

@property
def hvac_mode(self) -> HVACMode | None:
"""Return hvac operation ie. heat, cool mode."""
Expand Down
25 changes: 15 additions & 10 deletions custom_components/teslemetry/cover.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from typing import Any

from tesla_fleet_api.const import WindowCommands, Trunks
from tesla_fleet_api.const import WindowCommands, Trunks, Scopes

from homeassistant.components.cover import (
CoverDeviceClass,
Expand All @@ -26,7 +26,7 @@ async def async_setup_entry(
data = hass.data[DOMAIN][entry.entry_id]

async_add_entities(
klass(vehicle)
klass(vehicle, data.scopes)
for klass in (
TeslemetryWindowEntity,
TeslemetryChargePortEntity,
Expand All @@ -41,11 +41,13 @@ class TeslemetryWindowEntity(TeslemetryVehicleEntity, CoverEntity):
"""Cover entity for current charge."""

_attr_device_class = CoverDeviceClass.WINDOW
_attr_supported_features = CoverEntityFeature.OPEN | CoverEntityFeature.CLOSE

def __init__(self, vehicle: TeslemetryVehicleData) -> None:

def __init__(self, vehicle: TeslemetryVehicleData, scopes: list[Scopes]) -> None:
"""Initialize the sensor."""
super().__init__(vehicle, "windows")
if Scopes.VEHICLE_CMDS in scopes:
self._attr_supported_features = CoverEntityFeature.OPEN | CoverEntityFeature.CLOSE

@property
def is_closed(self) -> bool | None:
Expand Down Expand Up @@ -82,11 +84,12 @@ class TeslemetryChargePortEntity(TeslemetryVehicleEntity, CoverEntity):
"""Cover entity for the charge port."""

_attr_device_class = CoverDeviceClass.DOOR
_attr_supported_features = CoverEntityFeature.OPEN | CoverEntityFeature.CLOSE

def __init__(self, vehicle: TeslemetryVehicleData) -> None:
def __init__(self, vehicle: TeslemetryVehicleData, scopes: list[Scopes]) -> None:
"""Initialize the sensor."""
super().__init__(vehicle, "charge_state_charge_port_door_open")
if Scopes.VEHICLE_CMDS in scopes:
self._attr_supported_features = CoverEntityFeature.OPEN | CoverEntityFeature.CLOSE

@property
def is_closed(self) -> bool | None:
Expand All @@ -108,11 +111,12 @@ class TeslemetryFrontTrunkEntity(TeslemetryVehicleEntity, CoverEntity):
"""Cover entity for the charge port."""

_attr_device_class = CoverDeviceClass.DOOR
_attr_supported_features = CoverEntityFeature.OPEN

def __init__(self, vehicle: TeslemetryVehicleData) -> None:
def __init__(self, vehicle: TeslemetryVehicleData, scopes: list[Scopes]) -> None:
"""Initialize the sensor."""
super().__init__(vehicle, "vehicle_state_ft")
if Scopes.VEHICLE_CMDS in scopes:
self._attr_supported_features = CoverEntityFeature.OPEN

@property
def is_closed(self) -> bool | None:
Expand All @@ -129,11 +133,12 @@ class TeslemetryRearTrunkEntity(TeslemetryVehicleEntity, CoverEntity):
"""Cover entity for the charge port."""

_attr_device_class = CoverDeviceClass.DOOR
_attr_supported_features = CoverEntityFeature.OPEN | CoverEntityFeature.CLOSE

def __init__(self, vehicle: TeslemetryVehicleData) -> None:
def __init__(self, vehicle: TeslemetryVehicleData, scopes: list[Scopes]) -> None:
"""Initialize the sensor."""
super().__init__(vehicle, "vehicle_state_rt")
if Scopes.VEHICLE_CMDS in scopes:
self._attr_supported_features = CoverEntityFeature.OPEN | CoverEntityFeature.CLOSE

@property
def is_closed(self) -> bool | None:
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ colorlog==6.8.2
homeassistant==2024.1.0
pip>=21.0,<23.4
ruff==0.1.15
tesla-fleet-api==0.2.4
tesla-fleet-api==0.2.5

0 comments on commit 6c30ae5

Please sign in to comment.