Skip to content

Commit

Permalink
feat: add online binary sensor
Browse files Browse the repository at this point in the history
  • Loading branch information
alandtse committed Jan 16, 2020
1 parent 32a37b2 commit 08d6961
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
36 changes: 36 additions & 0 deletions teslajsonpy/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
For more details about this api, please refer to the documentation at
https://github.com/zabuldon/teslajsonpy
"""
from typing import Dict, Text

from teslajsonpy.vehicle import VehicleDevice


Expand Down Expand Up @@ -113,3 +115,37 @@ def get_value(self):
def has_battery():
"""Return whether the device has a battery."""
return False


class Online(VehicleDevice):
"""Home-Assistant Online class for a Tesla VehicleDevice."""

def __init__(self, data: Dict, controller) -> None:
"""Initialize the Online sensor.
Args:
data (Dict): The base state for a Tesla vehicle.
https://tesla-api.timdorr.com/vehicle/state/data
controller (Controller): The controller that controls updates to the Tesla API.
"""
super().__init__(data, controller)
self.__online_state: bool = None
self.type: Text = "online sensor"
self.hass_type = "binary_sensor"
self.name: Text = self._name()
self.uniq_name: Text = self._uniq_name()

async def async_update(self) -> None:
"""Update the battery state."""
await super().async_update()
self.__online_state = self._controller.car_online[self._vin]

@staticmethod
def has_battery() -> bool:
"""Return whether the device has a battery."""
return False

def get_value(self) -> bool:
"""Return the battery level."""
return self.__online_state
3 changes: 2 additions & 1 deletion teslajsonpy/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from typing import Optional, Text, Tuple

from teslajsonpy.battery_sensor import Battery, Range
from teslajsonpy.binary_sensor import ChargerConnectionSensor, ParkingSensor
from teslajsonpy.binary_sensor import ChargerConnectionSensor, Online, ParkingSensor
from teslajsonpy.charger import ChargerSwitch, ChargingSensor, RangeSwitch
from teslajsonpy.climate import Climate, TempSensor
from teslajsonpy.connection import Connection
Expand Down Expand Up @@ -108,6 +108,7 @@ async def connect(self, test_login=False) -> Tuple[Text, Text]:
self.__components.append(ParkingSensor(car, self))
self.__components.append(GPS(car, self))
self.__components.append(Odometer(car, self))
self.__components.append(Online(car, self))

tasks = [self.update(car["id"], wake_if_asleep=True) for car in cars]
try:
Expand Down

0 comments on commit 08d6961

Please sign in to comment.