diff --git a/custom_components/teslemetry/__init__.py b/custom_components/teslemetry/__init__.py index 43fd407..f2b4d31 100644 --- a/custom_components/teslemetry/__init__.py +++ b/custom_components/teslemetry/__init__.py @@ -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.""" @@ -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}, ) diff --git a/custom_components/teslemetry/cover.py b/custom_components/teslemetry/cover.py index 2a5fc73..5b35fd0 100644 --- a/custom_components/teslemetry/cover.py +++ b/custom_components/teslemetry/cover.py @@ -115,12 +115,6 @@ 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] @@ -128,6 +122,13 @@ def __init__(self, vehicle: TeslemetryVehicleData, scopes: list[Scope]) -> None: 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 @@ -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.""" @@ -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.""" @@ -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."""