-
-
Notifications
You must be signed in to change notification settings - Fork 37.5k
Add lock platform to Volvo integration #154168
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
gjohansson-ST
merged 7 commits into
home-assistant:dev
from
thomasddn:volvo/feature/lock
Nov 2, 2025
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
9dc2b99
Add lock
thomasddn cad335e
Refactor raise
thomasddn 3d34a16
Merge remote-tracking branch 'upstream/dev' into volvo/feature/lock
thomasddn 8c6c707
Remove lock with reduced guard
thomasddn 2f782e2
Assure resetting un/lock attributes
thomasddn 0263570
Assert method call parameters
thomasddn 1fb833f
Use common entity name for lock
thomasddn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,6 +7,7 @@ | |
| Platform.BINARY_SENSOR, | ||
| Platform.BUTTON, | ||
| Platform.DEVICE_TRACKER, | ||
| Platform.LOCK, | ||
| Platform.SENSOR, | ||
| ] | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,136 @@ | ||
| """Volvo locks.""" | ||
|
|
||
| from dataclasses import dataclass | ||
| import logging | ||
| from typing import Any, cast | ||
|
|
||
| from volvocarsapi.models import VolvoApiException, VolvoCarsApiBaseModel, VolvoCarsValue | ||
|
|
||
| from homeassistant.components.lock import LockEntity, LockEntityDescription | ||
| from homeassistant.core import HomeAssistant | ||
| from homeassistant.exceptions import HomeAssistantError | ||
| from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback | ||
|
|
||
| from .const import DOMAIN | ||
| from .coordinator import VolvoConfigEntry | ||
| from .entity import VolvoEntity, VolvoEntityDescription | ||
|
|
||
| PARALLEL_UPDATES = 0 | ||
| _LOGGER = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| @dataclass(frozen=True, kw_only=True) | ||
| class VolvoLockDescription(VolvoEntityDescription, LockEntityDescription): | ||
| """Describes a Volvo lock entity.""" | ||
|
|
||
| api_lock_value: str = "LOCKED" | ||
| api_unlock_value: str = "UNLOCKED" | ||
| lock_command: str | ||
| unlock_command: str | ||
| required_command_key: str | ||
|
|
||
|
|
||
| _DESCRIPTIONS: tuple[VolvoLockDescription, ...] = ( | ||
| VolvoLockDescription( | ||
| key="lock", | ||
| api_field="centralLock", | ||
| lock_command="lock", | ||
| unlock_command="unlock", | ||
| required_command_key="LOCK", | ||
| ), | ||
| ) | ||
|
|
||
|
|
||
| async def async_setup_entry( | ||
| hass: HomeAssistant, | ||
| entry: VolvoConfigEntry, | ||
| async_add_entities: AddConfigEntryEntitiesCallback, | ||
| ) -> None: | ||
| """Set up locks.""" | ||
| coordinators = entry.runtime_data.interval_coordinators | ||
| async_add_entities( | ||
| [ | ||
| VolvoLock(coordinator, description) | ||
| for coordinator in coordinators | ||
| for description in _DESCRIPTIONS | ||
| if description.required_command_key | ||
| in entry.runtime_data.context.supported_commands | ||
| and description.api_field in coordinator.data | ||
| ] | ||
| ) | ||
|
|
||
|
|
||
| class VolvoLock(VolvoEntity, LockEntity): | ||
| """Volvo lock.""" | ||
|
|
||
| entity_description: VolvoLockDescription | ||
|
|
||
| async def async_lock(self, **kwargs: Any) -> None: | ||
| """Lock the car.""" | ||
| await self._async_handle_command(self.entity_description.lock_command, True) | ||
|
|
||
| async def async_unlock(self, **kwargs: Any) -> None: | ||
| """Unlock the car.""" | ||
| await self._async_handle_command(self.entity_description.unlock_command, False) | ||
|
|
||
| def _update_state(self, api_field: VolvoCarsApiBaseModel | None) -> None: | ||
| """Update the state of the entity.""" | ||
| assert isinstance(api_field, VolvoCarsValue) | ||
| self._attr_is_locked = api_field.value == "LOCKED" | ||
|
|
||
| async def _async_handle_command(self, command: str, locked: bool) -> None: | ||
| _LOGGER.debug("Lock '%s' is %s", command, "locked" if locked else "unlocked") | ||
| if locked: | ||
| self._attr_is_locking = True | ||
| else: | ||
| self._attr_is_unlocking = True | ||
| self.async_write_ha_state() | ||
|
|
||
| try: | ||
| result = await self.coordinator.context.api.async_execute_command(command) | ||
| except VolvoApiException as ex: | ||
| _LOGGER.debug("Lock '%s' error", command) | ||
| error = self._reset_and_create_error(command, message=ex.message) | ||
| raise error from ex | ||
|
|
||
| status = result.invoke_status if result else "" | ||
| _LOGGER.debug("Lock '%s' result: %s", command, status) | ||
|
|
||
| if status.upper() not in ("COMPLETED", "DELIVERED"): | ||
| error = self._reset_and_create_error( | ||
| command, status=status, message=result.message if result else "" | ||
| ) | ||
| raise error | ||
|
|
||
| api_field = cast( | ||
| VolvoCarsValue, | ||
| self.coordinator.get_api_field(self.entity_description.api_field), | ||
| ) | ||
|
|
||
| self._attr_is_locking = False | ||
| self._attr_is_unlocking = False | ||
|
|
||
| if locked: | ||
| api_field.value = self.entity_description.api_lock_value | ||
| else: | ||
| api_field.value = self.entity_description.api_unlock_value | ||
|
|
||
| self._attr_is_locked = locked | ||
| self.async_write_ha_state() | ||
|
|
||
| def _reset_and_create_error( | ||
| self, command: str, *, status: str = "", message: str = "" | ||
| ) -> HomeAssistantError: | ||
| self._attr_is_locking = False | ||
| self._attr_is_unlocking = False | ||
| self.async_write_ha_state() | ||
|
|
||
| return HomeAssistantError( | ||
| translation_domain=DOMAIN, | ||
| translation_key="lock_failure", | ||
| translation_placeholders={ | ||
| "command": command, | ||
| "status": status, | ||
| "message": message, | ||
| }, | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,197 @@ | ||
| # serializer version: 1 | ||
| # name: test_lock[ex30_2024][lock.volvo_ex30_lock-entry] | ||
| EntityRegistryEntrySnapshot({ | ||
| 'aliases': set({ | ||
| }), | ||
| 'area_id': None, | ||
| 'capabilities': None, | ||
| 'config_entry_id': <ANY>, | ||
| 'config_subentry_id': <ANY>, | ||
| 'device_class': None, | ||
| 'device_id': <ANY>, | ||
| 'disabled_by': None, | ||
| 'domain': 'lock', | ||
| 'entity_category': None, | ||
| 'entity_id': 'lock.volvo_ex30_lock', | ||
| 'has_entity_name': True, | ||
| 'hidden_by': None, | ||
| 'icon': None, | ||
| 'id': <ANY>, | ||
| 'labels': set({ | ||
| }), | ||
| 'name': None, | ||
| 'options': dict({ | ||
| }), | ||
| 'original_device_class': None, | ||
| 'original_icon': None, | ||
| 'original_name': 'Lock', | ||
| 'platform': 'volvo', | ||
| 'previous_unique_id': None, | ||
| 'suggested_object_id': None, | ||
| 'supported_features': 0, | ||
| 'translation_key': 'lock', | ||
| 'unique_id': 'yv1abcdefg1234567_lock', | ||
| 'unit_of_measurement': None, | ||
| }) | ||
| # --- | ||
| # name: test_lock[ex30_2024][lock.volvo_ex30_lock-state] | ||
| StateSnapshot({ | ||
| 'attributes': ReadOnlyDict({ | ||
| 'friendly_name': 'Volvo EX30 Lock', | ||
| 'supported_features': <LockEntityFeature: 0>, | ||
| }), | ||
| 'context': <ANY>, | ||
| 'entity_id': 'lock.volvo_ex30_lock', | ||
| 'last_changed': <ANY>, | ||
| 'last_reported': <ANY>, | ||
| 'last_updated': <ANY>, | ||
| 'state': 'locked', | ||
| }) | ||
| # --- | ||
| # name: test_lock[s90_diesel_2018][lock.volvo_s90_lock-entry] | ||
| EntityRegistryEntrySnapshot({ | ||
| 'aliases': set({ | ||
| }), | ||
| 'area_id': None, | ||
| 'capabilities': None, | ||
| 'config_entry_id': <ANY>, | ||
| 'config_subentry_id': <ANY>, | ||
| 'device_class': None, | ||
| 'device_id': <ANY>, | ||
| 'disabled_by': None, | ||
| 'domain': 'lock', | ||
| 'entity_category': None, | ||
| 'entity_id': 'lock.volvo_s90_lock', | ||
| 'has_entity_name': True, | ||
| 'hidden_by': None, | ||
| 'icon': None, | ||
| 'id': <ANY>, | ||
| 'labels': set({ | ||
| }), | ||
| 'name': None, | ||
| 'options': dict({ | ||
| }), | ||
| 'original_device_class': None, | ||
| 'original_icon': None, | ||
| 'original_name': 'Lock', | ||
| 'platform': 'volvo', | ||
| 'previous_unique_id': None, | ||
| 'suggested_object_id': None, | ||
| 'supported_features': 0, | ||
| 'translation_key': 'lock', | ||
| 'unique_id': 'yv1abcdefg1234567_lock', | ||
| 'unit_of_measurement': None, | ||
| }) | ||
| # --- | ||
| # name: test_lock[s90_diesel_2018][lock.volvo_s90_lock-state] | ||
| StateSnapshot({ | ||
| 'attributes': ReadOnlyDict({ | ||
| 'friendly_name': 'Volvo S90 Lock', | ||
| 'supported_features': <LockEntityFeature: 0>, | ||
| }), | ||
| 'context': <ANY>, | ||
| 'entity_id': 'lock.volvo_s90_lock', | ||
| 'last_changed': <ANY>, | ||
| 'last_reported': <ANY>, | ||
| 'last_updated': <ANY>, | ||
| 'state': 'locked', | ||
| }) | ||
| # --- | ||
| # name: test_lock[xc40_electric_2024][lock.volvo_xc40_lock-entry] | ||
| EntityRegistryEntrySnapshot({ | ||
| 'aliases': set({ | ||
| }), | ||
| 'area_id': None, | ||
| 'capabilities': None, | ||
| 'config_entry_id': <ANY>, | ||
| 'config_subentry_id': <ANY>, | ||
| 'device_class': None, | ||
| 'device_id': <ANY>, | ||
| 'disabled_by': None, | ||
| 'domain': 'lock', | ||
| 'entity_category': None, | ||
| 'entity_id': 'lock.volvo_xc40_lock', | ||
| 'has_entity_name': True, | ||
| 'hidden_by': None, | ||
| 'icon': None, | ||
| 'id': <ANY>, | ||
| 'labels': set({ | ||
| }), | ||
| 'name': None, | ||
| 'options': dict({ | ||
| }), | ||
| 'original_device_class': None, | ||
| 'original_icon': None, | ||
| 'original_name': 'Lock', | ||
| 'platform': 'volvo', | ||
| 'previous_unique_id': None, | ||
| 'suggested_object_id': None, | ||
| 'supported_features': 0, | ||
| 'translation_key': 'lock', | ||
| 'unique_id': 'yv1abcdefg1234567_lock', | ||
| 'unit_of_measurement': None, | ||
| }) | ||
| # --- | ||
| # name: test_lock[xc40_electric_2024][lock.volvo_xc40_lock-state] | ||
| StateSnapshot({ | ||
| 'attributes': ReadOnlyDict({ | ||
| 'friendly_name': 'Volvo XC40 Lock', | ||
| 'supported_features': <LockEntityFeature: 0>, | ||
| }), | ||
| 'context': <ANY>, | ||
| 'entity_id': 'lock.volvo_xc40_lock', | ||
| 'last_changed': <ANY>, | ||
| 'last_reported': <ANY>, | ||
| 'last_updated': <ANY>, | ||
| 'state': 'locked', | ||
| }) | ||
| # --- | ||
| # name: test_lock[xc90_petrol_2019][lock.volvo_xc90_lock-entry] | ||
| EntityRegistryEntrySnapshot({ | ||
| 'aliases': set({ | ||
| }), | ||
| 'area_id': None, | ||
| 'capabilities': None, | ||
| 'config_entry_id': <ANY>, | ||
| 'config_subentry_id': <ANY>, | ||
| 'device_class': None, | ||
| 'device_id': <ANY>, | ||
| 'disabled_by': None, | ||
| 'domain': 'lock', | ||
| 'entity_category': None, | ||
| 'entity_id': 'lock.volvo_xc90_lock', | ||
| 'has_entity_name': True, | ||
| 'hidden_by': None, | ||
| 'icon': None, | ||
| 'id': <ANY>, | ||
| 'labels': set({ | ||
| }), | ||
| 'name': None, | ||
| 'options': dict({ | ||
| }), | ||
| 'original_device_class': None, | ||
| 'original_icon': None, | ||
| 'original_name': 'Lock', | ||
| 'platform': 'volvo', | ||
| 'previous_unique_id': None, | ||
| 'suggested_object_id': None, | ||
| 'supported_features': 0, | ||
| 'translation_key': 'lock', | ||
| 'unique_id': 'yv1abcdefg1234567_lock', | ||
| 'unit_of_measurement': None, | ||
| }) | ||
| # --- | ||
| # name: test_lock[xc90_petrol_2019][lock.volvo_xc90_lock-state] | ||
| StateSnapshot({ | ||
| 'attributes': ReadOnlyDict({ | ||
| 'friendly_name': 'Volvo XC90 Lock', | ||
| 'supported_features': <LockEntityFeature: 0>, | ||
| }), | ||
| 'context': <ANY>, | ||
| 'entity_id': 'lock.volvo_xc90_lock', | ||
| 'last_changed': <ANY>, | ||
| 'last_reported': <ANY>, | ||
| 'last_updated': <ANY>, | ||
| 'state': 'locked', | ||
| }) | ||
| # --- |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.