Example #1
0
    def __init__(
        self,
        websession: Optional[httpx.AsyncClient] = None,
        email: Text = None,
        password: Text = None,
        access_token: Text = None,
        refresh_token: Text = None,
        expiration: int = 0,
        update_interval: int = 300,
        enable_websocket: bool = False,
    ) -> None:
        """Initialize controller.

        Args:
            websession (aiohttp.ClientSession): Websession for aiohttp.
            email (Text, optional): Email account. Defaults to None.
            password (Text, optional): Password. Defaults to None.
            access_token (Text, optional): Access token. Defaults to None.
            refresh_token (Text, optional): Refresh token. Defaults to None.
            expiration (int, optional): Timestamp when access_token expires. Defaults to 0
            update_interval (int, optional): Seconds between allowed updates to the API.  This is to prevent
            being blocked by Tesla. Defaults to 300.
            enable_websocket (bool, optional): Whether to connect with websockets. Defaults to False.

        """
        self.__connection = Connection(
            websession=websession
            if websession and isinstance(websession, httpx.AsyncClient) else
            httpx.AsyncClient(timeout=60),
            email=email,
            password=password,
            access_token=access_token,
            refresh_token=refresh_token,
            expiration=expiration,
        )
        self.__components = []
        self._update_interval: int = update_interval
        self.__update = {}
        self.__climate = {}
        self.__charging = {}
        self.__state = {}
        self.__config = {}
        self.__driving = {}
        self.__gui = {}
        self._last_update_time = {}  # succesful update 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 = {}
        self.__update_lock = None  # controls access to update function
        self.__wakeup_conds = {}
        self.car_online = {}
        self.car_state = {}
        self.__id_vin_map = {}
        self.__vin_id_map = {}
        self.__vin_vehicle_id_map = {}
        self.__vehicle_id_vin_map = {}
        self.__websocket_listeners = []
        self.__last_parked_timestamp = {}
        self.__update_state = {}
        self.enable_websocket = enable_websocket
Example #2
0
 def __init__(self, email, password, update_interval):
     self.__connection = Connection(email, password)
     self.__vehicles = []
     self.update_interval = update_interval
     self.__climate = {}
     self.__charging = {}
     self.__state = {}
     self.__driving = {}
     self.__gui = {}
     self.__last_update_time = {}
     self.__lock = RLock()
     cars = self.__connection.get('vehicles')['response']
     for car in cars:
         self.__last_update_time[car['id']] = 0
         self.update(car['id'])
         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(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))
Example #3
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))