Skip to content

Commit

Permalink
Fix auto bug
Browse files Browse the repository at this point in the history
  • Loading branch information
Bre77 committed Sep 4, 2024
1 parent 4be5ffc commit 3aa384c
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 24 deletions.
35 changes: 23 additions & 12 deletions custom_components/teslemetry/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,27 @@
Platform.UPDATE,
]

class HandleVehicleData:
"""Handle streaming vehicle data."""

def __init__(self, coordinator: TeslemetryVehicleDataCoordinator):
self.coordinator = coordinator

def receive(self, data: dict) -> None:
"""Handle vehicle data from the stream."""
self.coordinator.updated_once = True
self.coordinator.async_set_updated_data(flatten(data["vehicle_data"]))

class HandleVehicleState:
""" Handle streaming vehicle state"""

def __init__(self, coordinator: TeslemetryVehicleDataCoordinator):
self.coordinator = coordinator

def receive(self, data: dict) -> None:
"""Handle state from the stream."""
self.coordinator.data["state"] = data["state"]
self.coordinator.async_set_updated_data(self.coordinator.data)

async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
"""Set up the Telemetry integration."""
Expand Down Expand Up @@ -105,23 +126,13 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
)

# Setup AUTO mode
def _handle_vehicle_data(data: dict) -> None:
"""Handle vehicle data from the stream."""
coordinator.updated_once = True
coordinator.async_set_updated_data(flatten(data["vehicle_data"]))

listener_vehicle_data = stream.async_add_listener(
_handle_vehicle_data,
HandleVehicleData(coordinator).receive,
{"vin": vin, "vehicle_data": None},
)

def _handle_state(data: dict) -> None:
"""Handle state from the stream."""
coordinator.data["state"] = data["state"]
coordinator.async_set_updated_data(coordinator.data)

listener_state = stream.async_add_listener(
_handle_state,
HandleVehicleState(coordinator).receive,
{"vin": vin, "state": None},
)

Expand Down
22 changes: 10 additions & 12 deletions custom_components/teslemetry/cover.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,19 +115,20 @@ class TeslemetryChargePortEntity(TeslemetryVehicleEntity, CoverRestoreEntity):

def __init__(self, vehicle: TeslemetryVehicleData, scopes: list[Scope]) -> None:
"""Initialize the sensor."""
super().__init__(
vehicle,
"charge_state_charge_port_door_open",
timestamp_key=TeslemetryTimestamp.CHARGE_STATE,
streaming_key=TelemetryField.CHARGE_PORT,
)
self.scoped = any(
scope in scopes
for scope in [Scope.VEHICLE_CMDS, Scope.VEHICLE_CHARGING_CMDS]
)
if not self.scoped:
self._attr_supported_features = CoverEntityFeature(0)

super().__init__(
vehicle,
"charge_state_charge_port_door_open",
timestamp_key=TeslemetryTimestamp.CHARGE_STATE,
streaming_key=TelemetryField.CHARGE_PORT,
)

def _async_update_attrs(self) -> None:
"""Update the entity attributes."""
self._attr_is_closed = not self._value
Expand Down Expand Up @@ -161,11 +162,10 @@ class TeslemetryFrontTrunkEntity(TeslemetryVehicleEntity, CoverRestoreEntity):

def __init__(self, vehicle: TeslemetryVehicleData, scopes: list[Scope]) -> None:
"""Initialize the sensor."""
super().__init__(vehicle, "vehicle_state_ft")

self.scoped = Scope.VEHICLE_CMDS in scopes
if not self.scoped:
self._attr_supported_features = CoverEntityFeature(0)
super().__init__(vehicle, "vehicle_state_ft")

def _async_update_attrs(self) -> None:
"""Update the entity attributes."""
Expand All @@ -190,11 +190,10 @@ class TeslemetryRearTrunkEntity(TeslemetryVehicleEntity, CoverRestoreEntity):

def __init__(self, vehicle: TeslemetryVehicleData, scopes: list[Scope]) -> None:
"""Initialize the sensor."""
super().__init__(vehicle, "vehicle_state_rt")

self.scoped = Scope.VEHICLE_CMDS in scopes
if not self.scoped:
self._attr_supported_features = CoverEntityFeature(0)
super().__init__(vehicle, "vehicle_state_rt")

def _async_update_attrs(self) -> None:
"""Update the entity attributes."""
Expand Down Expand Up @@ -233,11 +232,10 @@ class TeslemetrySunroofEntity(TeslemetryVehicleEntity, CoverRestoreEntity):

def __init__(self, vehicle: TeslemetryVehicleData, scopes: list[Scope]) -> None:
"""Initialize the sensor."""
super().__init__(vehicle, "vehicle_state_sun_roof_state")

self.scoped = Scope.VEHICLE_CMDS in scopes
if not self.scoped:
self._attr_supported_features = CoverEntityFeature(0)
super().__init__(vehicle, "vehicle_state_sun_roof_state")

def _async_update_attrs(self) -> None:
"""Update the entity attributes."""
Expand Down

0 comments on commit 3aa384c

Please sign in to comment.