|
6 | 6 | from stringcase import camelcase |
7 | 7 | import voluptuous as vol |
8 | 8 |
|
9 | | -from homeassistant.components.device_tracker import PLATFORM_SCHEMA, SOURCE_TYPE_GPS |
| 9 | +from homeassistant.components.device_tracker import ( |
| 10 | + PLATFORM_SCHEMA as PARENT_PLATFORM_SCHEMA, |
| 11 | + SOURCE_TYPE_GPS, |
| 12 | +) |
10 | 13 | from homeassistant.components.device_tracker.config_entry import TrackerEntity |
| 14 | +from homeassistant.config_entries import ConfigEntry |
11 | 15 | from homeassistant.const import ( |
12 | 16 | CONF_EVENT, |
13 | 17 | CONF_HOST, |
|
70 | 74 | DEFAULT_SCAN_INTERVAL = timedelta(seconds=30) |
71 | 75 | SCAN_INTERVAL = DEFAULT_SCAN_INTERVAL |
72 | 76 |
|
73 | | -PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend( |
| 77 | +EVENTS = [ |
| 78 | + EVENT_DEVICE_MOVING, |
| 79 | + EVENT_COMMAND_RESULT, |
| 80 | + EVENT_DEVICE_FUEL_DROP, |
| 81 | + EVENT_GEOFENCE_ENTER, |
| 82 | + EVENT_DEVICE_OFFLINE, |
| 83 | + EVENT_DRIVER_CHANGED, |
| 84 | + EVENT_GEOFENCE_EXIT, |
| 85 | + EVENT_DEVICE_OVERSPEED, |
| 86 | + EVENT_DEVICE_ONLINE, |
| 87 | + EVENT_DEVICE_STOPPED, |
| 88 | + EVENT_MAINTENANCE, |
| 89 | + EVENT_ALARM, |
| 90 | + EVENT_TEXT_MESSAGE, |
| 91 | + EVENT_DEVICE_UNKNOWN, |
| 92 | + EVENT_IGNITION_OFF, |
| 93 | + EVENT_IGNITION_ON, |
| 94 | + EVENT_ALL_EVENTS, |
| 95 | +] |
| 96 | + |
| 97 | +PLATFORM_SCHEMA = PARENT_PLATFORM_SCHEMA.extend( |
74 | 98 | { |
75 | 99 | vol.Required(CONF_PASSWORD): cv.string, |
76 | 100 | vol.Required(CONF_USERNAME): cv.string, |
|
87 | 111 | ), |
88 | 112 | vol.Optional(CONF_EVENT, default=[]): vol.All( |
89 | 113 | cv.ensure_list, |
90 | | - [ |
91 | | - vol.Any( |
92 | | - EVENT_DEVICE_MOVING, |
93 | | - EVENT_COMMAND_RESULT, |
94 | | - EVENT_DEVICE_FUEL_DROP, |
95 | | - EVENT_GEOFENCE_ENTER, |
96 | | - EVENT_DEVICE_OFFLINE, |
97 | | - EVENT_DRIVER_CHANGED, |
98 | | - EVENT_GEOFENCE_EXIT, |
99 | | - EVENT_DEVICE_OVERSPEED, |
100 | | - EVENT_DEVICE_ONLINE, |
101 | | - EVENT_DEVICE_STOPPED, |
102 | | - EVENT_MAINTENANCE, |
103 | | - EVENT_ALARM, |
104 | | - EVENT_TEXT_MESSAGE, |
105 | | - EVENT_DEVICE_UNKNOWN, |
106 | | - EVENT_IGNITION_OFF, |
107 | | - EVENT_IGNITION_ON, |
108 | | - EVENT_ALL_EVENTS, |
109 | | - ) |
110 | | - ], |
| 114 | + [vol.In(EVENTS)], |
111 | 115 | ), |
112 | 116 | } |
113 | 117 | ) |
114 | 118 |
|
115 | 119 |
|
116 | | -async def async_setup_entry(hass: HomeAssistant, entry, async_add_entities): |
| 120 | +async def async_setup_entry( |
| 121 | + hass: HomeAssistant, entry: ConfigEntry, async_add_entities |
| 122 | +): |
117 | 123 | """Configure a dispatcher connection based on a config entry.""" |
118 | 124 |
|
119 | 125 | @callback |
@@ -197,6 +203,8 @@ def __init__( |
197 | 203 | ): |
198 | 204 | """Initialize.""" |
199 | 205 |
|
| 206 | + if EVENT_ALL_EVENTS in event_types: |
| 207 | + event_types = EVENTS |
200 | 208 | self._event_types = {camelcase(evt): evt for evt in event_types} |
201 | 209 | self._custom_attributes = custom_attributes |
202 | 210 | self._scan_interval = scan_interval |
@@ -318,7 +326,7 @@ async def import_events(self): |
318 | 326 | "device_moox_track_id": event["deviceId"], |
319 | 327 | "device_name": device_name, |
320 | 328 | "type": event["type"], |
321 | | - "serverTime": event["serverTime"], |
| 329 | + "serverTime": event.get("eventTime") or event.get("serverTime"), |
322 | 330 | "attributes": event["attributes"], |
323 | 331 | }, |
324 | 332 | ) |
@@ -394,8 +402,7 @@ async def async_added_to_hass(self): |
394 | 402 | if self._latitude is not None or self._longitude is not None: |
395 | 403 | return |
396 | 404 |
|
397 | | - state = await self.async_get_last_state() |
398 | | - if state is None: |
| 405 | + if (state := await self.async_get_last_state()) is None: |
399 | 406 | self._latitude = None |
400 | 407 | self._longitude = None |
401 | 408 | self._accuracy = None |
|
0 commit comments