Skip to content

Commit f3abdf0

Browse files
authored
Home Assistant 2024.11 (#52)
1 parent b457126 commit f3abdf0

File tree

3 files changed

+38
-41
lines changed

3 files changed

+38
-41
lines changed

custom_components/teslafi/alarm_control_panel.py

+25-22
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,19 @@
1+
"""TeslaFi Alarm Control Panel."""
2+
13
from dataclasses import dataclass
4+
25
from homeassistant.components.alarm_control_panel import (
36
AlarmControlPanelEntity,
47
AlarmControlPanelEntityDescription,
58
AlarmControlPanelEntityFeature,
9+
AlarmControlPanelState,
610
)
711
from homeassistant.config_entries import ConfigEntry
8-
from homeassistant.const import (
9-
STATE_ALARM_ARMING,
10-
STATE_ALARM_ARMED_AWAY,
11-
STATE_ALARM_DISARMING,
12-
STATE_ALARM_DISARMED,
13-
)
1412
from homeassistant.core import HomeAssistant
1513
from homeassistant.helpers.entity_platform import AddEntitiesCallback
1614

17-
from .base import (
18-
TeslaFiBaseEntityDescription,
19-
TeslaFiEntity,
20-
)
21-
from .const import DELAY_WAKEUP, DELAY_LOCKS, DOMAIN, LOGGER
15+
from .base import TeslaFiBaseEntityDescription, TeslaFiEntity
16+
from .const import DELAY_LOCKS, DELAY_WAKEUP, DOMAIN, LOGGER
2217
from .coordinator import TeslaFiCoordinator
2318
from .util import _convert_to_bool
2419

@@ -28,13 +23,15 @@ class TeslaFiSentryEntityDescription(
2823
AlarmControlPanelEntityDescription,
2924
TeslaFiBaseEntityDescription,
3025
):
31-
"""Alarm panel to control Sentry Mode"""
26+
"""Alarm panel to control Sentry Mode."""
3227

3328

3429
class TeslaFiSentryEntity(
3530
TeslaFiEntity[TeslaFiSentryEntityDescription],
3631
AlarmControlPanelEntity,
3732
):
33+
"""TeslaFi Sentry Mode Alarm Control Panel."""
34+
3835
_attr_code_arm_required: bool = False
3936
_attr_supported_features: AlarmControlPanelEntityFeature = (
4037
AlarmControlPanelEntityFeature.ARM_AWAY
@@ -46,17 +43,20 @@ def __init__(
4643
coordinator: TeslaFiCoordinator,
4744
entity_description: TeslaFiSentryEntityDescription,
4845
) -> None:
46+
"""Initialize the TeslaFi alarm control panel entity."""
4947
super().__init__(coordinator, entity_description)
5048
self._attr_changed_by = None
5149
self._target_state = None
5250

5351
@property
5452
def icon(self) -> str | None:
55-
if self.state == STATE_ALARM_ARMED_AWAY:
53+
"""Return the icon for the entity."""
54+
if self.state == AlarmControlPanelState.ARMED_AWAY:
5655
return "mdi:shield-car"
5756
return super().icon
5857

5958
async def async_alarm_disarm(self, code: str | None = None) -> None:
59+
"""Send the disarm command to the Tesla vehicle."""
6060
LOGGER.debug("Disarming")
6161
response = await self.coordinator.execute_command(
6262
"set_sentry_mode", sentryMode=False
@@ -67,8 +67,8 @@ async def async_alarm_disarm(self, code: str | None = None) -> None:
6767
# > https://developer.tesla.com/docs/fleet-api#2023-10-09-rest-api-vehicle-commands-endpoint-deprecation-warning:
6868

6969
if response:
70-
self._target_state = STATE_ALARM_DISARMED
71-
self._attr_state = STATE_ALARM_DISARMING
70+
self._target_state = AlarmControlPanelState.DISARMED
71+
self._attr_alarm_state = AlarmControlPanelState.DISARMING
7272
self._attr_changed_by = "hass"
7373
self.async_write_ha_state()
7474

@@ -79,6 +79,7 @@ async def async_alarm_disarm(self, code: str | None = None) -> None:
7979
self.coordinator.schedule_refresh_in(DELAY_LOCKS)
8080

8181
async def async_alarm_arm_away(self, code: str | None = None) -> None:
82+
"""Send the arm command to the Tesla vehicle."""
8283
LOGGER.debug("Arming")
8384
response = await self.coordinator.execute_command(
8485
"set_sentry_mode", sentryMode=True
@@ -89,8 +90,8 @@ async def async_alarm_arm_away(self, code: str | None = None) -> None:
8990
# > https://developer.tesla.com/docs/fleet-api#2023-10-09-rest-api-vehicle-commands-endpoint-deprecation-warning:
9091

9192
if response:
92-
self._target_state = STATE_ALARM_ARMED_AWAY
93-
self._attr_state = STATE_ALARM_ARMING
93+
self._target_state = AlarmControlPanelState.ARMED_AWAY
94+
self._attr_alarm_state = AlarmControlPanelState.ARMING
9495
self._attr_changed_by = "hass"
9596
self.async_write_ha_state()
9697

@@ -109,7 +110,7 @@ def _handle_coordinator_update(self) -> None:
109110
if waiting:
110111
if new_state == target:
111112
# It succeeded
112-
self._attr_state = new_state
113+
self._attr_alarm_state = new_state
113114
self._attr_changed_by = "hass"
114115
self._target_state = None
115116
LOGGER.info("Target state succeeded: %s", target)
@@ -119,10 +120,10 @@ def _handle_coordinator_update(self) -> None:
119120
return self.coordinator.schedule_refresh_in(DELAY_WAKEUP)
120121
elif old_state is None or new_state is None:
121122
self._attr_changed_by = None
122-
self._attr_state = new_state
123+
self._attr_alarm_state = new_state
123124
elif old_state != new_state:
124125
self._attr_changed_by = "remote"
125-
self._attr_state = new_state
126+
self._attr_alarm_state = new_state
126127

127128
return super()._handle_coordinator_update()
128129

@@ -133,7 +134,9 @@ def _handle_coordinator_update(self) -> None:
133134
name="Sentry Mode",
134135
entity_registry_enabled_default=False,
135136
convert=lambda v: (
136-
STATE_ALARM_ARMED_AWAY if _convert_to_bool(v) else STATE_ALARM_DISARMED
137+
AlarmControlPanelState.ARMED_AWAY
138+
if _convert_to_bool(v)
139+
else AlarmControlPanelState.DISARMED
137140
),
138141
),
139142
]
@@ -144,7 +147,7 @@ async def async_setup_entry(
144147
config_entry: ConfigEntry,
145148
async_add_entities: AddEntitiesCallback,
146149
) -> None:
147-
"""Set up from config entry"""
150+
"""Set up from config entry."""
148151
coordinator: TeslaFiCoordinator
149152
coordinator = hass.data[DOMAIN][config_entry.entry_id]["coordinator"]
150153
entities: list[TeslaFiSentryEntity] = []

custom_components/teslafi/lock.py

+12-18
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,15 @@
1-
"""TeslaFi Locks"""
1+
"""TeslaFi Locks."""
22

33
from typing import Any
4-
from homeassistant.components.lock import LockEntity
4+
5+
from homeassistant.components.lock import LockEntity, LockState
56
from homeassistant.config_entries import ConfigEntry
6-
from homeassistant.const import STATE_LOCKING, STATE_UNLOCKING
77
from homeassistant.core import HomeAssistant
88
from homeassistant.helpers.entity_platform import AddEntitiesCallback
99

10-
from .const import (
11-
DELAY_LOCKS,
12-
DELAY_WAKEUP,
13-
DOMAIN,
14-
LOGGER,
15-
)
16-
from .coordinator import TeslaFiCoordinator
1710
from .base import TeslaFiEntity, TeslaFiLockEntityDescription
18-
11+
from .const import DELAY_LOCKS, DELAY_WAKEUP, DOMAIN, LOGGER
12+
from .coordinator import TeslaFiCoordinator
1913

2014
LOCKS = [
2115
TeslaFiLockEntityDescription(
@@ -31,7 +25,7 @@ async def async_setup_entry(
3125
config_entry: ConfigEntry,
3226
async_add_entities: AddEntitiesCallback,
3327
) -> None:
34-
"""Set up from config entry"""
28+
"""Set up from config entry."""
3529
coordinator: TeslaFiCoordinator
3630
coordinator = hass.data[DOMAIN][config_entry.entry_id]["coordinator"]
3731
entities: list[TeslaFiLock] = []
@@ -40,20 +34,20 @@ async def async_setup_entry(
4034

4135

4236
class TeslaFiLock(TeslaFiEntity[TeslaFiLockEntityDescription], LockEntity):
43-
"""TeslaFi Door Locks"""
37+
"""TeslaFi Door Locks."""
4438

4539
_pending_action: str | None = None
4640

4741
async def async_lock(self, **kwargs: Any) -> None:
48-
"""Ask TeslaFi to lock the vehicle"""
42+
"""Ask TeslaFi to lock the vehicle."""
4943
self._attr_is_unlocking = False
5044

5145
response = await self.coordinator.execute_command("door_lock")
5246
assert response
5347

5448
if response:
5549
LOGGER.debug("Lock response %s", response)
56-
self._pending_action = STATE_LOCKING
50+
self._pending_action = LockState.LOCKING
5751
self._attr_is_locking = True
5852
self.async_write_ha_state()
5953
if self.coordinator.data.is_sleeping:
@@ -63,15 +57,15 @@ async def async_lock(self, **kwargs: Any) -> None:
6357
self.coordinator.schedule_refresh_in(DELAY_LOCKS)
6458

6559
async def async_unlock(self, **kwargs: Any) -> None:
66-
"""Ask TeslaFi to unlock the vehicle"""
60+
"""Ask TeslaFi to unlock the vehicle."""
6761
self._attr_is_locking = False
6862

6963
response = await self.coordinator.execute_command("door_unlock")
7064
assert response
7165

7266
if response:
7367
LOGGER.debug("Unlock response %s", response)
74-
self._pending_action = STATE_UNLOCKING
68+
self._pending_action = LockState.UNLOCKING
7569
self._attr_is_unlocking = True
7670
self.async_write_ha_state()
7771
if self.coordinator.data.is_sleeping:
@@ -84,7 +78,7 @@ def _handle_coordinator_update(self) -> None:
8478
prev = self.state
8579
newest = self._get_value()
8680
waiting = self._pending_action is not None
87-
target = prev == STATE_LOCKING if waiting else None
81+
target = prev == LockState.LOCKING if waiting else None
8882
LOGGER.debug(
8983
"lock %s: prev=%s, new=%s, pending=%s, target=%s",
9084
self.entity_id,

hacs.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
"name": "TeslaFi",
33
"render_readme": true,
44
"country": "US",
5-
"homeassistant": "2024.4.0"
5+
"homeassistant": "2024.11.0"
66
}

0 commit comments

Comments
 (0)