|
| 1 | +"""Tests for the listen_ActiveRoute* composite listeners. |
| 2 | +
|
| 3 | +After navigation ends Minutes to Arrival goes null, but the car keeps |
| 4 | +reporting the last trip's destination, arrival energy and traffic delay. The |
| 5 | +composites pair each route field with Minutes to Arrival. Scenarios mirror the |
| 6 | +Home Assistant Teslemetry tests the helper was ported from. |
| 7 | +""" |
| 8 | +from __future__ import annotations |
| 9 | + |
| 10 | +import asyncio |
| 11 | +from typing import Any |
| 12 | + |
| 13 | +from teslemetry_stream.const import Signal, TeslaLocation |
| 14 | +from teslemetry_stream.stream import TeslemetryStream |
| 15 | +from teslemetry_stream.vehicle import TeslemetryStreamVehicle |
| 16 | + |
| 17 | +VIN = "TESTVIN0000000001" |
| 18 | +MINUTES = Signal.MINUTES_TO_ARRIVAL |
| 19 | +DELAY = Signal.ROUTE_TRAFFIC_MINUTES_DELAY |
| 20 | +ENERGY = Signal.EXPECTED_ENERGY_PERCENT_AT_TRIP_ARRIVAL |
| 21 | +DESTINATION = Signal.DESTINATION_LOCATION |
| 22 | +LOCATION = {"latitude": -27.824252, "longitude": 153.328079} |
| 23 | + |
| 24 | + |
| 25 | +def check(label: str, ok: bool, detail: str = "") -> bool: |
| 26 | + print(f"{label:<72} {'PASS' if ok else 'FAIL'}{' ' + detail if detail else ''}") |
| 27 | + return ok |
| 28 | + |
| 29 | + |
| 30 | +def make_vehicle() -> tuple[TeslemetryStream, TeslemetryStreamVehicle]: |
| 31 | + stream = TeslemetryStream(None, "token", manual=True) # type: ignore[arg-type] |
| 32 | + vehicle = TeslemetryStreamVehicle(stream, VIN) |
| 33 | + vehicle.fields = {s.value: {} for s in (MINUTES, DELAY, ENERGY, DESTINATION)} |
| 34 | + vehicle._populated = True |
| 35 | + return stream, vehicle |
| 36 | + |
| 37 | + |
| 38 | +def run(listener: str, messages: list[dict[Signal, Any]]) -> list[Any]: |
| 39 | + """Return every value the callback received, in order.""" |
| 40 | + stream, vehicle = make_vehicle() |
| 41 | + values: list[Any] = [] |
| 42 | + unsub = getattr(vehicle, listener)(values.append) |
| 43 | + for message in messages: |
| 44 | + stream.ingest({k.value: v for k, v in message.items()}, vin=VIN) |
| 45 | + unsub() |
| 46 | + return values |
| 47 | + |
| 48 | + |
| 49 | +SEQUENCES: list[tuple[str, str, list[dict[Signal, Any]], list[Any]]] = [ |
| 50 | + ( |
| 51 | + "route_ends_delay", |
| 52 | + "listen_ActiveRouteTrafficMinutesDelay", |
| 53 | + [ |
| 54 | + {MINUTES: 12.5, DELAY: 3, ENERGY: 62}, |
| 55 | + {MINUTES: None, DELAY: 0, ENERGY: 62}, |
| 56 | + ], |
| 57 | + [3, None], |
| 58 | + ), |
| 59 | + ( |
| 60 | + "route_ends_energy", |
| 61 | + "listen_ActiveRouteExpectedEnergyPercentAtTripArrival", |
| 62 | + [ |
| 63 | + {MINUTES: 12.5, DELAY: 3, ENERGY: 62}, |
| 64 | + {MINUTES: None, DELAY: 0, ENERGY: 62}, |
| 65 | + ], |
| 66 | + [62, None], |
| 67 | + ), |
| 68 | + ( |
| 69 | + "route_restarts", |
| 70 | + "listen_ActiveRouteTrafficMinutesDelay", |
| 71 | + [ |
| 72 | + {MINUTES: 12.5, DELAY: 3}, |
| 73 | + {MINUTES: None, DELAY: 0}, |
| 74 | + {MINUTES: 30.0, DELAY: 5}, |
| 75 | + ], |
| 76 | + [3, None, 5], |
| 77 | + ), |
| 78 | + ( |
| 79 | + "route_restarts_with_unchanged_value", |
| 80 | + "listen_ActiveRouteTrafficMinutesDelay", |
| 81 | + [{MINUTES: 12.5, DELAY: 3}, {MINUTES: None}, {MINUTES: 20.0}], |
| 82 | + [3, None, 3], |
| 83 | + ), |
| 84 | + ( |
| 85 | + "waits_for_minutes_to_arrival", |
| 86 | + "listen_ActiveRouteTrafficMinutesDelay", |
| 87 | + [{DELAY: 3}, {MINUTES: 12.5}], |
| 88 | + [3], |
| 89 | + ), |
| 90 | + ( |
| 91 | + "energy_never_reported", |
| 92 | + "listen_ActiveRouteExpectedEnergyPercentAtTripArrival", |
| 93 | + [{MINUTES: 12.5, DELAY: 3}, {MINUTES: None, DELAY: 0}, {MINUTES: 30.0}], |
| 94 | + [None], |
| 95 | + ), |
| 96 | + ( |
| 97 | + "destination_route_ends", |
| 98 | + "listen_ActiveRouteDestinationLocation", |
| 99 | + [ |
| 100 | + {DESTINATION: LOCATION, MINUTES: 12.5}, |
| 101 | + {DESTINATION: LOCATION, MINUTES: None}, |
| 102 | + ], |
| 103 | + [TeslaLocation(**LOCATION), None], |
| 104 | + ), |
| 105 | + ( |
| 106 | + "unrelated_event_ignored", |
| 107 | + "listen_ActiveRouteTrafficMinutesDelay", |
| 108 | + [{MINUTES: 12.5, DELAY: 3}, {Signal.VEHICLE_SPEED: 40}], |
| 109 | + [3], |
| 110 | + ), |
| 111 | +] |
| 112 | + |
| 113 | + |
| 114 | +async def main() -> None: |
| 115 | + results: list[bool] = [] |
| 116 | + for name, listener, messages, expected in SEQUENCES: |
| 117 | + got = run(listener, messages) |
| 118 | + results.append(check(f"sequence {name}", got == expected, f"got {got!r}")) |
| 119 | + |
| 120 | + stream, vehicle = make_vehicle() |
| 121 | + before = len(stream._listeners) |
| 122 | + unsub = vehicle.listen_ActiveRouteDestinationLocation(lambda v: None) |
| 123 | + added = len(stream._listeners) - before |
| 124 | + unsub() |
| 125 | + results.append( |
| 126 | + check( |
| 127 | + "one unsubscribe removes all three listeners", |
| 128 | + added == 3 and len(stream._listeners) == before, |
| 129 | + f"added {added}, left {len(stream._listeners) - before}", |
| 130 | + ) |
| 131 | + ) |
| 132 | + await asyncio.sleep(0) |
| 133 | + |
| 134 | + print("-" * 72) |
| 135 | + print("ALL PASS" if all(results) else "FAILURES PRESENT") |
| 136 | + if not all(results): |
| 137 | + raise SystemExit(1) |
| 138 | + |
| 139 | + |
| 140 | +if __name__ == "__main__": |
| 141 | + asyncio.run(main()) |
0 commit comments