1
1
import logging
2
2
3
- from homeassistant .components .sensor import DOMAIN as SENSOR_DOMAIN
4
- from homeassistant .config_entries import ConfigEntry
5
- from homeassistant .core import HomeAssistant , callback
6
-
7
3
from custom_components .hasl3 .haslworker import HaslWorker
8
4
from custom_components .hasl3 .rrapi import rrapi_sl
9
5
from custom_components .hasl3 .slapi import slapi_pu1 , slapi_rp3
10
6
7
+ from homeassistant .components .sensor import DOMAIN as SENSOR_DOMAIN
8
+ from homeassistant .config_entries import ConfigEntry
9
+ from homeassistant .core import Event , HomeAssistant , ServiceCall
10
+
11
11
from .const import (
12
+ CONF_INTEGRATION_ID ,
12
13
CONF_INTEGRATION_TYPE ,
13
14
DOMAIN ,
14
15
SCHEMA_VERSION ,
15
16
SENSOR_ROUTE ,
16
17
SENSOR_VEHICLE_LOCATION ,
17
- CONF_INTEGRATION_ID ,
18
18
)
19
19
20
20
logger = logging .getLogger (f"custom_components.{ DOMAIN } .core" )
21
21
serviceLogger = logging .getLogger (f"custom_components.{ DOMAIN } .services" )
22
22
23
+ EventOrService = Event | ServiceCall
23
24
24
25
async def async_setup (hass : HomeAssistant , config : ConfigEntry ):
25
26
"""Set up HASL integration"""
@@ -38,8 +39,7 @@ async def async_setup(hass: HomeAssistant, config: ConfigEntry):
38
39
return False
39
40
40
41
# SERVICE FUNCTIONS
41
- @callback
42
- async def sl_find_location (service ):
42
+ async def sl_find_location (service : EventOrService ):
43
43
serviceLogger .debug ("[sl_find_location] Entered" )
44
44
search_string = service .data .get ("search_string" )
45
45
api_key = service .data .get ("api_key" )
@@ -60,7 +60,7 @@ async def sl_find_location(service):
60
60
"result" : requestResult ,
61
61
},
62
62
)
63
- return True
63
+
64
64
except Exception as e :
65
65
serviceLogger .debug ("[sl_find_location] Lookup failed" )
66
66
hass .bus .fire (
@@ -71,10 +71,8 @@ async def sl_find_location(service):
71
71
"result" : f"Exception occured during execution: { str (e )} " ,
72
72
},
73
73
)
74
- return True
75
74
76
- @callback
77
- async def rr_find_location (service ):
75
+ async def rr_find_location (service : EventOrService ):
78
76
serviceLogger .debug ("[rr_find_location] Entered" )
79
77
search_string = service .data .get ("search_string" )
80
78
api_key = service .data .get ("api_key" )
@@ -95,7 +93,7 @@ async def rr_find_location(service):
95
93
"result" : requestResult ,
96
94
},
97
95
)
98
- return True
96
+
99
97
except Exception as e :
100
98
serviceLogger .debug ("[rr_find_location] Lookup failed" )
101
99
hass .bus .fire (
@@ -106,10 +104,9 @@ async def rr_find_location(service):
106
104
"result" : f"Exception occured during execution: { str (e )} " ,
107
105
},
108
106
)
109
- return True
110
107
111
- @ callback
112
- async def sl_find_trip_id (service ):
108
+
109
+ async def sl_find_trip_id (service : EventOrService ):
113
110
serviceLogger .debug ("[sl_find_trip_id] Entered" )
114
111
origin = service .data .get ("org" )
115
112
destination = service .data .get ("dest" )
@@ -129,7 +126,7 @@ async def sl_find_trip_id(service):
129
126
"result" : requestResult ,
130
127
},
131
128
)
132
- return True
129
+
133
130
except Exception as e :
134
131
serviceLogger .debug ("[sl_find_trip_id] Lookup failed" )
135
132
hass .bus .fire (
@@ -140,10 +137,9 @@ async def sl_find_trip_id(service):
140
137
"result" : f"Exception occured during execution: { str (e )} " ,
141
138
},
142
139
)
143
- return True
144
140
145
- @ callback
146
- async def sl_find_trip_pos (service ):
141
+
142
+ async def sl_find_trip_pos (service : EventOrService ):
147
143
serviceLogger .debug ("[sl_find_trip_pos] Entered" )
148
144
olat = service .data .get ("orig_lat" )
149
145
olon = service .data .get ("orig_long" )
@@ -167,7 +163,7 @@ async def sl_find_trip_pos(service):
167
163
"result" : requestResult ,
168
164
},
169
165
)
170
- return True
166
+
171
167
except Exception as e :
172
168
serviceLogger .debug ("[sl_find_trip_pos] Lookup failed" )
173
169
hass .bus .fire (
@@ -178,30 +174,29 @@ async def sl_find_trip_pos(service):
178
174
"result" : f"Exception occured during execution: { str (e )} " ,
179
175
},
180
176
)
181
- return True
182
177
183
- @ callback
184
- async def eventListener (service ):
178
+
179
+ async def eventListener (service : Event ):
185
180
serviceLogger .debug ("[eventListener] Entered" )
186
181
187
182
command = service .data .get ("cmd" )
188
183
189
184
if command == "sl_find_location" :
190
- sl_find_location (service )
185
+ hass . async_add_job ( sl_find_location (service ) )
191
186
serviceLogger .debug ("[eventListener] Dispatched to sl_find_location" )
192
- return True
187
+
193
188
if command == "rr_find_location" :
194
- rr_find_location (service )
189
+ hass . async_add_job ( rr_find_location (service ) )
195
190
serviceLogger .debug ("[eventListener] Dispatched to rr_find_location" )
196
- return True
191
+
197
192
if command == "sl_find_trip_pos" :
198
- sl_find_trip_pos (service )
193
+ hass . async_add_job ( sl_find_trip_pos (service ) )
199
194
serviceLogger .debug ("[eventListener] Dispatched to sl_find_trip_pos" )
200
- return True
195
+
201
196
if command == "sl_find_trip_id" :
202
- sl_find_trip_id (service )
197
+ hass . async_add_job ( sl_find_trip_id (service ) )
203
198
serviceLogger .debug ("[eventListener] Dispatched to sl_find_trip_id" )
204
- return True
199
+
205
200
206
201
logger .debug ("[setup] Registering services" )
207
202
try :
0 commit comments