diff --git a/custom_components/teslemetry/lock.py b/custom_components/teslemetry/lock.py index f3bb0a4..9970006 100644 --- a/custom_components/teslemetry/lock.py +++ b/custom_components/teslemetry/lock.py @@ -15,7 +15,7 @@ TeslemetryVehicleEntity, ) from .models import TeslemetryVehicleData - +from .context import handle_command async def async_setup_entry( hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback @@ -50,15 +50,17 @@ def is_locked(self) -> bool | None: async def async_lock(self, **kwargs: Any) -> None: """Lock the doors.""" self.raise_for_scope() - await self.wake_up_if_asleep() - await self.api.door_lock() + with handle_command(): + await self.wake_up_if_asleep() + await self.api.door_lock() self.set((self.key, True)) async def async_unlock(self, **kwargs: Any) -> None: """Unlock the doors.""" self.raise_for_scope() - await self.wake_up_if_asleep() - await self.api.door_unlock() + with handle_command(): + await self.wake_up_if_asleep() + await self.api.door_unlock() self.set((self.key, False)) @@ -93,8 +95,9 @@ async def async_lock(self, **kwargs: Any) -> None: async def async_unlock(self, **kwargs: Any) -> None: """Unlock charge cable lock.""" self.raise_for_scope() - await self.wake_up_if_asleep() - await self.api.charge_port_door_open() + with handle_command(): + await self.wake_up_if_asleep() + await self.api.charge_port_door_open() self.set((self.key, TeslemetryChargeCableLockStates.DISENGAGED)) @@ -122,8 +125,9 @@ async def async_lock(self, **kwargs: Any) -> None: code: str | None = kwargs.get(ATTR_CODE) if code: self.raise_for_scope() - await self.wake_up_if_asleep() - await self.api.speed_limit_activate(code) + with handle_command(): + await self.wake_up_if_asleep() + await self.api.speed_limit_activate(code) self.set((self.key, True)) async def async_unlock(self, **kwargs: Any) -> None: @@ -131,6 +135,7 @@ async def async_unlock(self, **kwargs: Any) -> None: code: str | None = kwargs.get(ATTR_CODE) if code: self.raise_for_scope() - await self.wake_up_if_asleep() - await self.api.speed_limit_deactivate(code) + with handle_command(): + await self.wake_up_if_asleep() + await self.api.speed_limit_deactivate(code) self.set((self.key, False)) diff --git a/custom_components/teslemetry/media_player.py b/custom_components/teslemetry/media_player.py index 01ca0c4..fac9aab 100644 --- a/custom_components/teslemetry/media_player.py +++ b/custom_components/teslemetry/media_player.py @@ -17,6 +17,7 @@ TeslemetryVehicleEntity, ) from .models import TeslemetryVehicleData +from .context import handle_command STATES = { "Playing": MediaPlayerState.PLAYING, @@ -131,31 +132,36 @@ def source(self) -> str | None: async def async_set_volume_level(self, volume: float) -> None: """Set volume level, range 0..1.""" self.raise_for_scope() - await self.wake_up_if_asleep() - await self.api.adjust_volume(int(volume * self.max_volume)) + with handle_command(): + await self.wake_up_if_asleep() + await self.api.adjust_volume(int(volume * self.max_volume)) async def async_media_play(self) -> None: """Send play command.""" if self.state != MediaPlayerState.PLAYING: self.raise_for_scope() - await self.wake_up_if_asleep() - await self.api.media_toggle_playback() + with handle_command(): + await self.wake_up_if_asleep() + await self.api.media_toggle_playback() async def async_media_pause(self) -> None: """Send pause command.""" if self.state == MediaPlayerState.PLAYING: self.raise_for_scope() - await self.wake_up_if_asleep() - await self.api.media_toggle_playback() + with handle_command(): + await self.wake_up_if_asleep() + await self.api.media_toggle_playback() async def async_media_next_track(self) -> None: """Send next track command.""" self.raise_for_scope() - await self.wake_up_if_asleep() - await self.api.media_next_track() + with handle_command(): + await self.wake_up_if_asleep() + await self.api.media_next_track() async def async_media_previous_track(self) -> None: """Send previous track command.""" self.raise_for_scope() - await self.wake_up_if_asleep() - await self.api.media_previous_track() + with handle_command(): + await self.wake_up_if_asleep() + await self.api.media_previous_track() diff --git a/custom_components/teslemetry/number.py b/custom_components/teslemetry/number.py index 64d3ad6..614a7b2 100644 --- a/custom_components/teslemetry/number.py +++ b/custom_components/teslemetry/number.py @@ -29,6 +29,7 @@ TeslemetryEnergyInfoEntity, ) from .models import TeslemetryVehicleData, TeslemetryEnergyData +from .context import handle_command @dataclass(frozen=True, kw_only=True) @@ -184,8 +185,9 @@ def __init__( async def async_set_native_value(self, value: float) -> None: """Set new value.""" self.raise_for_scope() - await self.wake_up_if_asleep() - await self.entity_description.func(self.api, value) + with handle_command(): + await self.wake_up_if_asleep() + await self.entity_description.func(self.api, value) self.set((self.key, value)) @@ -208,5 +210,6 @@ def __init__( async def async_set_native_value(self, value: float) -> None: """Set new value.""" self.raise_for_scope() - await self.entity_description.func(self.api, value) + with handle_command(): + await self.entity_description.func(self.api, value) self.set((self.key, value)) diff --git a/custom_components/teslemetry/select.py b/custom_components/teslemetry/select.py index 87c6b35..87454ed 100644 --- a/custom_components/teslemetry/select.py +++ b/custom_components/teslemetry/select.py @@ -16,6 +16,7 @@ TeslemetryEnergyInfoEntity, ) from .models import TeslemetryEnergyData, TeslemetryVehicleData +from .context import handle_command SEAT_HEATERS = { "climate_state_seat_heater_left": "front_left", @@ -137,9 +138,10 @@ def current_option(self) -> str | None: async def async_select_option(self, option: str) -> None: """Change the selected option.""" self.raise_for_scope() - await self.wake_up_if_asleep() level = self._attr_options.index(option) - await self.api.remote_seat_heater_request(SEAT_HEATERS[self.key], level) + with handle_command(): + await self.wake_up_if_asleep() + await self.api.remote_seat_heater_request(SEAT_HEATERS[self.key], level) self.set((self.key, level)) @@ -165,5 +167,6 @@ def current_option(self) -> str | None: async def async_select_option(self, option: str) -> None: """Change the selected option.""" self.raise_for_scope() - await self.entity_description.func(self.api, option) + with handle_command(): + await self.entity_description.func(self.api, option) self.set((self.key, option)) diff --git a/custom_components/teslemetry/switch.py b/custom_components/teslemetry/switch.py index c75155a..88b9c29 100644 --- a/custom_components/teslemetry/switch.py +++ b/custom_components/teslemetry/switch.py @@ -26,6 +26,7 @@ TeslemetryVehicleData, TeslemetryEnergyData, ) +from .context import handle_command @dataclass(frozen=True, kw_only=True) @@ -160,13 +161,15 @@ def is_on(self) -> bool: async def async_turn_on(self, **kwargs: Any) -> None: """Turn on the Switch.""" self.raise_for_scope() - await self.entity_description.on_func(self.api) + with handle_command(): + await self.entity_description.on_func(self.api) self.set((self.entity_description.key, True)) async def async_turn_off(self, **kwargs: Any) -> None: """Turn off the Switch.""" self.raise_for_scope() - await self.entity_description.off_func(self.api) + with handle_command(): + await self.entity_description.off_func(self.api) self.set((self.entity_description.key, False)) @@ -187,15 +190,17 @@ def __init__( async def async_turn_on(self, **kwargs: Any) -> None: """Turn on the Switch.""" self.raise_for_scope() - await self.wake_up_if_asleep() - await self.entity_description.on_func(self.api) + with handle_command(): + await self.wake_up_if_asleep() + await self.entity_description.on_func(self.api) self.set((self.entity_description.key, True)) async def async_turn_off(self, **kwargs: Any) -> None: """Turn off the Switch.""" self.raise_for_scope() - await self.wake_up_if_asleep() - await self.entity_description.off_func(self.api) + with handle_command(): + await self.wake_up_if_asleep() + await self.entity_description.off_func(self.api) self.set((self.entity_description.key, False)) diff --git a/custom_components/teslemetry/update.py b/custom_components/teslemetry/update.py index a294db8..5d3e230 100644 --- a/custom_components/teslemetry/update.py +++ b/custom_components/teslemetry/update.py @@ -13,6 +13,7 @@ from .const import DOMAIN, TeslemetryUpdateStatus from .entity import TeslemetryVehicleEntity from .models import TeslemetryVehicleData +from .context import handle_command async def async_setup_entry( @@ -90,8 +91,9 @@ async def async_install( ) -> None: """Install an update.""" self.raise_for_scope() - await self.wake_up_if_asleep() - await self.api.schedule_software_update(0) + with handle_command(): + await self.wake_up_if_asleep() + await self.api.schedule_software_update(0) self.set( ("vehicle_state_software_update_status", TeslemetryUpdateStatus.INSTALLING) )