mirror of
https://github.com/home-assistant/core.git
synced 2025-07-10 06:47:09 +00:00
Small refactoring of BMW lock entity (#77451)
* Refactor entity_description * Fix default attrs not always shown * Simplify further Co-authored-by: @emontnemery Co-authored-by: rikroe <rikroe@users.noreply.github.com>
This commit is contained in:
parent
f98e86d3a6
commit
cfa838b27a
@ -7,7 +7,7 @@ from typing import Any
|
|||||||
from bimmer_connected.vehicle import MyBMWVehicle
|
from bimmer_connected.vehicle import MyBMWVehicle
|
||||||
from bimmer_connected.vehicle.doors_windows import LockState
|
from bimmer_connected.vehicle.doors_windows import LockState
|
||||||
|
|
||||||
from homeassistant.components.lock import LockEntity, LockEntityDescription
|
from homeassistant.components.lock import LockEntity
|
||||||
from homeassistant.config_entries import ConfigEntry
|
from homeassistant.config_entries import ConfigEntry
|
||||||
from homeassistant.core import HomeAssistant, callback
|
from homeassistant.core import HomeAssistant, callback
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
@ -36,7 +36,6 @@ async def async_setup_entry(
|
|||||||
BMWLock(
|
BMWLock(
|
||||||
coordinator,
|
coordinator,
|
||||||
vehicle,
|
vehicle,
|
||||||
LockEntityDescription(key="lock", device_class="lock", name="Lock"),
|
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
async_add_entities(entities)
|
async_add_entities(entities)
|
||||||
@ -45,17 +44,17 @@ async def async_setup_entry(
|
|||||||
class BMWLock(BMWBaseEntity, LockEntity):
|
class BMWLock(BMWBaseEntity, LockEntity):
|
||||||
"""Representation of a MyBMW vehicle lock."""
|
"""Representation of a MyBMW vehicle lock."""
|
||||||
|
|
||||||
|
_attr_name = "Lock"
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
coordinator: BMWDataUpdateCoordinator,
|
coordinator: BMWDataUpdateCoordinator,
|
||||||
vehicle: MyBMWVehicle,
|
vehicle: MyBMWVehicle,
|
||||||
description: LockEntityDescription,
|
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Initialize the lock."""
|
"""Initialize the lock."""
|
||||||
super().__init__(coordinator, vehicle)
|
super().__init__(coordinator, vehicle)
|
||||||
|
|
||||||
self.entity_description = description
|
self._attr_unique_id = f"{vehicle.vin}-lock"
|
||||||
self._attr_unique_id = f"{vehicle.vin}-{description.key}"
|
|
||||||
self.door_lock_state_available = DOOR_LOCK_STATE in vehicle.available_attributes
|
self.door_lock_state_available = DOOR_LOCK_STATE in vehicle.available_attributes
|
||||||
|
|
||||||
async def async_lock(self, **kwargs: Any) -> None:
|
async def async_lock(self, **kwargs: Any) -> None:
|
||||||
@ -84,17 +83,17 @@ class BMWLock(BMWBaseEntity, LockEntity):
|
|||||||
def _handle_coordinator_update(self) -> None:
|
def _handle_coordinator_update(self) -> None:
|
||||||
"""Handle updated data from the coordinator."""
|
"""Handle updated data from the coordinator."""
|
||||||
_LOGGER.debug("Updating lock data of %s", self.vehicle.name)
|
_LOGGER.debug("Updating lock data of %s", self.vehicle.name)
|
||||||
|
# Set default attributes
|
||||||
|
self._attr_extra_state_attributes = self._attrs
|
||||||
|
|
||||||
# Only update the HA state machine if the vehicle reliably reports its lock state
|
# Only update the HA state machine if the vehicle reliably reports its lock state
|
||||||
if self.door_lock_state_available:
|
if self.door_lock_state_available:
|
||||||
self._attr_is_locked = self.vehicle.doors_and_windows.door_lock_state in {
|
self._attr_is_locked = self.vehicle.doors_and_windows.door_lock_state in {
|
||||||
LockState.LOCKED,
|
LockState.LOCKED,
|
||||||
LockState.SECURED,
|
LockState.SECURED,
|
||||||
}
|
}
|
||||||
self._attr_extra_state_attributes = dict(
|
self._attr_extra_state_attributes[
|
||||||
self._attrs,
|
"door_lock_state"
|
||||||
**{
|
] = self.vehicle.doors_and_windows.door_lock_state.value
|
||||||
"door_lock_state": self.vehicle.doors_and_windows.door_lock_state.value,
|
|
||||||
},
|
|
||||||
)
|
|
||||||
|
|
||||||
super()._handle_coordinator_update()
|
super()._handle_coordinator_update()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user