1
+ """TeslaFi Alarm Control Panel."""
2
+
1
3
from dataclasses import dataclass
4
+
2
5
from homeassistant .components .alarm_control_panel import (
3
6
AlarmControlPanelEntity ,
4
7
AlarmControlPanelEntityDescription ,
5
8
AlarmControlPanelEntityFeature ,
9
+ AlarmControlPanelState ,
6
10
)
7
11
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
- )
14
12
from homeassistant .core import HomeAssistant
15
13
from homeassistant .helpers .entity_platform import AddEntitiesCallback
16
14
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
22
17
from .coordinator import TeslaFiCoordinator
23
18
from .util import _convert_to_bool
24
19
@@ -28,13 +23,15 @@ class TeslaFiSentryEntityDescription(
28
23
AlarmControlPanelEntityDescription ,
29
24
TeslaFiBaseEntityDescription ,
30
25
):
31
- """Alarm panel to control Sentry Mode"""
26
+ """Alarm panel to control Sentry Mode. """
32
27
33
28
34
29
class TeslaFiSentryEntity (
35
30
TeslaFiEntity [TeslaFiSentryEntityDescription ],
36
31
AlarmControlPanelEntity ,
37
32
):
33
+ """TeslaFi Sentry Mode Alarm Control Panel."""
34
+
38
35
_attr_code_arm_required : bool = False
39
36
_attr_supported_features : AlarmControlPanelEntityFeature = (
40
37
AlarmControlPanelEntityFeature .ARM_AWAY
@@ -46,17 +43,20 @@ def __init__(
46
43
coordinator : TeslaFiCoordinator ,
47
44
entity_description : TeslaFiSentryEntityDescription ,
48
45
) -> None :
46
+ """Initialize the TeslaFi alarm control panel entity."""
49
47
super ().__init__ (coordinator , entity_description )
50
48
self ._attr_changed_by = None
51
49
self ._target_state = None
52
50
53
51
@property
54
52
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 :
56
55
return "mdi:shield-car"
57
56
return super ().icon
58
57
59
58
async def async_alarm_disarm (self , code : str | None = None ) -> None :
59
+ """Send the disarm command to the Tesla vehicle."""
60
60
LOGGER .debug ("Disarming" )
61
61
response = await self .coordinator .execute_command (
62
62
"set_sentry_mode" , sentryMode = False
@@ -67,8 +67,8 @@ async def async_alarm_disarm(self, code: str | None = None) -> None:
67
67
# > https://developer.tesla.com/docs/fleet-api#2023-10-09-rest-api-vehicle-commands-endpoint-deprecation-warning:
68
68
69
69
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
72
72
self ._attr_changed_by = "hass"
73
73
self .async_write_ha_state ()
74
74
@@ -79,6 +79,7 @@ async def async_alarm_disarm(self, code: str | None = None) -> None:
79
79
self .coordinator .schedule_refresh_in (DELAY_LOCKS )
80
80
81
81
async def async_alarm_arm_away (self , code : str | None = None ) -> None :
82
+ """Send the arm command to the Tesla vehicle."""
82
83
LOGGER .debug ("Arming" )
83
84
response = await self .coordinator .execute_command (
84
85
"set_sentry_mode" , sentryMode = True
@@ -89,8 +90,8 @@ async def async_alarm_arm_away(self, code: str | None = None) -> None:
89
90
# > https://developer.tesla.com/docs/fleet-api#2023-10-09-rest-api-vehicle-commands-endpoint-deprecation-warning:
90
91
91
92
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
94
95
self ._attr_changed_by = "hass"
95
96
self .async_write_ha_state ()
96
97
@@ -109,7 +110,7 @@ def _handle_coordinator_update(self) -> None:
109
110
if waiting :
110
111
if new_state == target :
111
112
# It succeeded
112
- self ._attr_state = new_state
113
+ self ._attr_alarm_state = new_state
113
114
self ._attr_changed_by = "hass"
114
115
self ._target_state = None
115
116
LOGGER .info ("Target state succeeded: %s" , target )
@@ -119,10 +120,10 @@ def _handle_coordinator_update(self) -> None:
119
120
return self .coordinator .schedule_refresh_in (DELAY_WAKEUP )
120
121
elif old_state is None or new_state is None :
121
122
self ._attr_changed_by = None
122
- self ._attr_state = new_state
123
+ self ._attr_alarm_state = new_state
123
124
elif old_state != new_state :
124
125
self ._attr_changed_by = "remote"
125
- self ._attr_state = new_state
126
+ self ._attr_alarm_state = new_state
126
127
127
128
return super ()._handle_coordinator_update ()
128
129
@@ -133,7 +134,9 @@ def _handle_coordinator_update(self) -> None:
133
134
name = "Sentry Mode" ,
134
135
entity_registry_enabled_default = False ,
135
136
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
137
140
),
138
141
),
139
142
]
@@ -144,7 +147,7 @@ async def async_setup_entry(
144
147
config_entry : ConfigEntry ,
145
148
async_add_entities : AddEntitiesCallback ,
146
149
) -> None :
147
- """Set up from config entry"""
150
+ """Set up from config entry. """
148
151
coordinator : TeslaFiCoordinator
149
152
coordinator = hass .data [DOMAIN ][config_entry .entry_id ]["coordinator" ]
150
153
entities : list [TeslaFiSentryEntity ] = []
0 commit comments