Skip to content

Commit

Permalink
Finish adding context manager
Browse files Browse the repository at this point in the history
  • Loading branch information
Bre77 committed Feb 8, 2024
1 parent 2d86ce9 commit 8fd60a1
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 35 deletions.
27 changes: 16 additions & 11 deletions custom_components/teslemetry/lock.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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))


Expand Down Expand Up @@ -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))


Expand Down Expand Up @@ -122,15 +125,17 @@ 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:
"""Disable speed limit with pin."""
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))
26 changes: 16 additions & 10 deletions custom_components/teslemetry/media_player.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
TeslemetryVehicleEntity,
)
from .models import TeslemetryVehicleData
from .context import handle_command

STATES = {
"Playing": MediaPlayerState.PLAYING,
Expand Down Expand Up @@ -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()
9 changes: 6 additions & 3 deletions custom_components/teslemetry/number.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
TeslemetryEnergyInfoEntity,
)
from .models import TeslemetryVehicleData, TeslemetryEnergyData
from .context import handle_command


@dataclass(frozen=True, kw_only=True)
Expand Down Expand Up @@ -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))


Expand All @@ -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))
9 changes: 6 additions & 3 deletions custom_components/teslemetry/select.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
TeslemetryEnergyInfoEntity,
)
from .models import TeslemetryEnergyData, TeslemetryVehicleData
from .context import handle_command

SEAT_HEATERS = {
"climate_state_seat_heater_left": "front_left",
Expand Down Expand Up @@ -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))


Expand All @@ -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))
17 changes: 11 additions & 6 deletions custom_components/teslemetry/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
TeslemetryVehicleData,
TeslemetryEnergyData,
)
from .context import handle_command


@dataclass(frozen=True, kw_only=True)
Expand Down Expand Up @@ -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))


Expand All @@ -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))


Expand Down
6 changes: 4 additions & 2 deletions custom_components/teslemetry/update.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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)
)

0 comments on commit 8fd60a1

Please sign in to comment.