Exemplo n.º 1
0
def test_has_battery(monkeypatch):
    """Test has_battery()."""

    _mock = TeslaMock(monkeypatch)
    _controller = Controller(None)

    _data = _mock.data_request_vehicle()
    _range = Range(_data, _controller)

    assert not _range.has_battery()
Exemplo n.º 2
0
def test_get_value_on_init(monkeypatch):
    """Test get_value() after initialization."""

    _mock = TeslaMock(monkeypatch)
    _controller = Controller(None)

    _data = _mock.data_request_vehicle()
    _range = Range(_data, _controller)

    assert not _range is None
    assert _range.get_value() is None
Exemplo n.º 3
0
async def test_get_value_after_update(monkeypatch):
    """Test get_value() after an update."""

    _mock = TeslaMock(monkeypatch)
    _controller = Controller(None)

    _data = _mock.data_request_vehicle()
    _range = Range(_data, _controller)

    await _range.async_update()

    assert not _range is None
    assert not _range.get_value() is None
    assert _range.get_value() == 167.96
Exemplo n.º 4
0
async def test_get_value_rated_off(monkeypatch):
    """Test get_value() for range display not 'Rated'."""

    _mock = TeslaMock(monkeypatch)
    _controller = Controller(None)

    _data = _mock.data_request_vehicle()
    _range = Range(_data, _controller)
    _data["gui_settings"]["gui_range_display"] = "Other"
    _data["charge_state"]["battery_range"] = 123.45
    _data["charge_state"]["est_battery_range"] = 234.56
    _data["charge_state"]["ideal_battery_range"] = 345.67
    await _range.async_update()

    assert not _range is None
    assert not _range.get_value() is None
    assert _range.get_value() == 345.67
Exemplo n.º 5
0
async def test_get_value_in_mph(monkeypatch):
    """Test get_value() for units in mph'."""

    _mock = TeslaMock(monkeypatch)
    _controller = Controller(None)

    _data = _mock.data_request_vehicle()
    _range = Range(_data, _controller)
    _data["gui_settings"]["gui_distance_units"] = "mi/hr"
    _data["gui_settings"]["gui_range_display"] = "Rated"
    _data["charge_state"]["battery_range"] = 123.45
    _data["charge_state"]["est_battery_range"] = 234.56
    _data["charge_state"]["ideal_battery_range"] = 345.67
    await _range.async_update()

    assert not _range is None
    assert not _range.get_value() is None
    assert _range.get_value() == 123.45
Exemplo n.º 6
0
def test_device_class(monkeypatch):
    """Test device_class()."""

    _mock = TeslaMock(monkeypatch)
    _controller = Controller(None)

    _data = _mock.data_request_vehicle()
    _range = Range(_data, _controller)

    assert _range.device_class is None
Exemplo n.º 7
0
 def _add_components(self, car):
     self.__components.append(Climate(car, self))
     self.__components.append(Battery(car, self))
     self.__components.append(Range(car, self))
     self.__components.append(TempSensor(car, self))
     self.__components.append(Lock(car, self))
     self.__components.append(ChargerLock(car, self))
     self.__components.append(ChargerConnectionSensor(car, self))
     self.__components.append(ChargingSensor(car, self))
     self.__components.append(ChargerSwitch(car, self))
     self.__components.append(RangeSwitch(car, self))
     self.__components.append(ParkingSensor(car, self))
     self.__components.append(GPS(car, self))
     self.__components.append(Odometer(car, self))
     self.__components.append(OnlineSensor(car, self))
     self.__components.append(SentryModeSwitch(car, self))
     self.__components.append(TrunkLock(car, self))
     self.__components.append(FrunkLock(car, self))
Exemplo n.º 8
0
    async def connect(self,
                      test_login=False,
                      wake_if_asleep=False) -> Tuple[Text, Text]:
        """Connect controller to Tesla.

        Args
            test_login (bool, optional): Whether to test credentials only. Defaults to False.
            wake_if_asleep (bool, optional): Whether to wake up any sleeping cars to update state. Defaults to False.

        Returns
            Tuple[Text, Text]: Returns the refresh_token and access_token

        """

        cars = await self.get_vehicles()
        self._last_attempted_update_time = time.time()
        self.__controller_lock = asyncio.Lock()

        for car in cars:
            vin = car["vin"]
            self.__id_vin_map[car["id"]] = vin
            self.__vin_id_map[vin] = car["id"]
            self.__vin_vehicle_id_map[vin] = car["vehicle_id"]
            self.__vehicle_id_vin_map[car["vehicle_id"]] = vin
            self.__lock[vin] = asyncio.Lock()
            self.__wakeup_conds[vin] = asyncio.Lock()
            self._last_update_time[vin] = 0
            self._last_wake_up_time[vin] = 0
            self.__update[vin] = True
            self.__update_state[vin] = "normal"
            self.car_state[vin] = car
            self.car_online[vin] = car["state"] == "online"
            self.__last_parked_timestamp[
                vin] = self._last_attempted_update_time
            self.__climate[vin] = {}
            self.__charging[vin] = {}
            self.__state[vin] = {}
            self.__config[vin] = {}
            self.__driving[vin] = {}
            self.__gui[vin] = {}

            self.__components.append(Climate(car, self))
            self.__components.append(Battery(car, self))
            self.__components.append(Range(car, self))
            self.__components.append(TempSensor(car, self))
            self.__components.append(Lock(car, self))
            self.__components.append(ChargerLock(car, self))
            self.__components.append(ChargerConnectionSensor(car, self))
            self.__components.append(ChargingSensor(car, self))
            self.__components.append(ChargerSwitch(car, self))
            self.__components.append(RangeSwitch(car, self))
            self.__components.append(ParkingSensor(car, self))
            self.__components.append(GPS(car, self))
            self.__components.append(Odometer(car, self))
            self.__components.append(OnlineSensor(car, self))
            self.__components.append(SentryModeSwitch(car, self))

        if not test_login:
            tasks = [
                self.update(car["id"], wake_if_asleep=wake_if_asleep)
                for car in cars
            ]
            _LOGGER.debug("tasks %s %s", tasks, wake_if_asleep)
            try:
                await asyncio.gather(*tasks)
            except (TeslaException, RetryLimitError):
                pass
        return (self.__connection.refresh_token,
                self.__connection.access_token)
Exemplo n.º 9
0
    def __init__(self, email, password, update_interval):
        """Initialize controller.

        Parameters
        ----------
        email : string
            Email of Tesla account
        password : type
            Password of Tesla account
        update_interval : type
            Seconds between allowed updates to the API.  This is to prevent
            being blocked by Tesla

        Returns
        -------
        None

        """
        self.__connection = Connection(email, password)
        self.__vehicles = []
        self.update_interval = update_interval
        self.__update = {}
        self.__climate = {}
        self.__charging = {}
        self.__state = {}
        self.__driving = {}
        self.__gui = {}
        self._last_update_time = {}  # succesful attempts by car
        self._last_wake_up_time = {}  # succesful wake_ups by car
        self._last_attempted_update_time = 0  # all attempts by controller
        self.__lock = RLock()
        self.car_online = {}

        cars = self.get_vehicles()
        self._last_attempted_update_time = time.time()

        for car in cars:
            self._last_update_time[car['id']] = 0
            self._last_wake_up_time[car['id']] = 0
            self.__update[car['id']] = True
            self.car_online[car['id']] = (car['state'] == 'online')
            self.__climate[car['id']] = False
            self.__charging[car['id']] = False
            self.__state[car['id']] = False
            self.__driving[car['id']] = False
            self.__gui[car['id']] = False

            try:
                self.update(car['id'], wake_if_asleep=False)
            except (TeslaException, RetryLimitError):
                pass
            self.__vehicles.append(Climate(car, self))
            self.__vehicles.append(Battery(car, self))
            self.__vehicles.append(Range(car, self))
            self.__vehicles.append(TempSensor(car, self))
            self.__vehicles.append(Lock(car, self))
            self.__vehicles.append(ChargerLock(car, self))
            self.__vehicles.append(ChargerConnectionSensor(car, self))
            self.__vehicles.append(ChargerSwitch(car, self))
            self.__vehicles.append(RangeSwitch(car, self))
            self.__vehicles.append(ParkingSensor(car, self))
            self.__vehicles.append(GPS(car, self))
            self.__vehicles.append(Odometer(car, self))