Skip to content

Commit

Permalink
add "Unlock Plug when Charged" switch (#503)
Browse files Browse the repository at this point in the history
* add "Unlock Plug when Charged" switch

* update charging after UPDATE_AUTO_UNLOCK_PLUG event
  • Loading branch information
prvakt authored Dec 31, 2024
1 parent 7448bac commit 4590d54
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 0 deletions.
1 change: 1 addition & 0 deletions custom_components/myskoda/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ async def _on_operation_event(self, event: EventOperation) -> None:
OperationName.UPDATE_CHARGING_CURRENT,
OperationName.START_CHARGING,
OperationName.STOP_CHARGING,
OperationName.UPDATE_AUTO_UNLOCK_PLUG,
]:
await self.update_charging()
if event.operation.operation in [
Expand Down
6 changes: 6 additions & 0 deletions custom_components/myskoda/icons.json
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,12 @@
"on": "mdi:turtle"
}
},
"auto_unlock_plug": {
"default": "mdi:lock",
"state": {
"on": "mdi:lock-open"
}
},
"window_heating": {
"default": "mdi:car-defrost-front",
"state": {
Expand Down
45 changes: 45 additions & 0 deletions custom_components/myskoda/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
ChargingState,
ChargingStatus,
MaxChargeCurrent,
PlugUnlockMode,
Settings,
)
from myskoda.models.air_conditioning import (
Expand Down Expand Up @@ -62,6 +63,7 @@ async def async_setup_entry(
DepartureTimer1,
DepartureTimer2,
DepartureTimer3,
AutoUnlockPlug,
],
coordinators=hass.data[DOMAIN][config.entry_id][COORDINATORS],
async_add_entities=async_add_entities,
Expand Down Expand Up @@ -270,6 +272,49 @@ async def async_turn_on(self, **kwargs): # noqa: D102
_LOGGER.info("Charging started.")


class AutoUnlockPlug(ChargingSwitch):
"""Controls unlock plug when charged."""

entity_description = SwitchEntityDescription(
key="auto_unlock_plug",
name="Auto Unlock Plug",
device_class=SwitchDeviceClass.SWITCH,
translation_key="auto_unlock_plug",
entity_category=EntityCategory.CONFIG,
)

@property
def is_on(self) -> bool | None: # noqa: D102
if settings := self._settings():
return settings.auto_unlock_plug_when_charged != PlugUnlockMode.OFF

@Throttle(timedelta(seconds=API_COOLDOWN_IN_SECONDS))
async def _async_turn_on_off(self, turn_on: bool, **kwargs): # noqa: D102
"""Internal method to have a central location for the Throttle."""
try:
if turn_on:
await self.coordinator.myskoda.set_auto_unlock_plug(
self.vehicle.info.vin, True
)
else:
await self.coordinator.myskoda.set_auto_unlock_plug(
self.vehicle.info.vin, False
)
except OperationFailedError as exc:
_LOGGER.error("Failed to set auto unlock plug: %s", exc)

async def async_turn_off(self, **kwargs): # noqa: D102
await self._async_turn_on_off(turn_on=False)
_LOGGER.info("Auto unlock plug turned off.")

async def async_turn_on(self, **kwargs): # noqa: D102
await self._async_turn_on_off(turn_on=True)
_LOGGER.info("Auto unlock plug turned on.")

def required_capabilities(self) -> list[CapabilityId]:
return [CapabilityId.CHARGING, CapabilityId.EXTENDED_CHARGING_SETTINGS]


class AcAtUnlock(MySkodaSwitch):
"""Enable/disable climatisation when unlocked"""

Expand Down
3 changes: 3 additions & 0 deletions custom_components/myskoda/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,9 @@
"reduced_current": {
"name": "Reduced Current"
},
"auto_unlock_plug": {
"name": "Unlock Plug when Charged"
},
"window_heating": {
"name": "Window Heating"
},
Expand Down

0 comments on commit 4590d54

Please sign in to comment.