Skip to content

Commit

Permalink
refactor: break out non IO operations to refresh
Browse files Browse the repository at this point in the history
This is to support callback functions for the HA coordinator
  • Loading branch information
alandtse committed Jul 29, 2020
1 parent e8bfca7 commit 9793ee0
Show file tree
Hide file tree
Showing 9 changed files with 156 additions and 1 deletion.
16 changes: 16 additions & 0 deletions teslajsonpy/battery_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ def __init__(self, data: Dict, controller) -> None:
async def async_update(self, wake_if_asleep=False, force=False) -> None:
"""Update the battery state."""
await super().async_update(wake_if_asleep=wake_if_asleep)
self.refresh()

def refresh(self) -> None:
"""Refresh data.
This assumes the controller has already been updated
"""
super().refresh()
data = self._controller.get_charging_params(self._id)
if data:
self.__battery_level = data["battery_level"]
Expand Down Expand Up @@ -100,6 +108,14 @@ def __init__(self, data: Dict, controller) -> None:
async def async_update(self, wake_if_asleep=False, force=False) -> None:
"""Update the battery range state."""
await super().async_update(wake_if_asleep=wake_if_asleep)
self.refresh()

def refresh(self) -> None:
"""Refresh data.
This assumes the controller has already been updated
"""
super().refresh()
data = self._controller.get_charging_params(self._id)
if data:
self.__battery_range = data["battery_range"]
Expand Down
32 changes: 32 additions & 0 deletions teslajsonpy/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,14 @@ def __init__(self, data: Dict, controller):
async def async_update(self, wake_if_asleep=False, force=False) -> None:
"""Update the parking brake sensor."""
await super().async_update(wake_if_asleep=wake_if_asleep)
self.refresh()

def refresh(self) -> None:
"""Refresh data.
This assumes the controller has already been updated
"""
super().refresh()
data = self._controller.get_drive_params(self._id)
if data:
self.attrs["shift_state"] = (
Expand Down Expand Up @@ -142,6 +150,14 @@ def __init__(self, data, controller):
async def async_update(self, wake_if_asleep=False, force=False) -> None:
"""Update the charger connection sensor."""
await super().async_update(wake_if_asleep=wake_if_asleep)
self.refresh()

def refresh(self) -> None:
"""Refresh data.
This assumes the controller has already been updated
"""
super().refresh()
data = self._controller.get_charging_params(self._id)
if data:
self.attrs["charging_state"] = data["charging_state"]
Expand Down Expand Up @@ -182,6 +198,14 @@ def __init__(self, data: Dict, controller) -> None:
async def async_update(self, wake_if_asleep=False, force=False) -> None:
"""Update the battery state."""
await super().async_update(wake_if_asleep=wake_if_asleep)
self.refresh()

def refresh(self) -> None:
"""Refresh data.
This assumes the controller has already been updated
"""
super().refresh()
self.__online_state = self._controller.car_online[self._vin]
self.attrs["state"] = self._controller.car_state[self._vin].get("state")

Expand Down Expand Up @@ -211,6 +235,14 @@ def __init__(self, data: Dict, controller) -> None:
async def async_update(self, wake_if_asleep=False, force=False) -> None:
"""Update the battery state."""
await super().async_update(wake_if_asleep=wake_if_asleep)
self.refresh()

def refresh(self) -> None:
"""Refresh data.
This assumes the controller has already been updated
"""
super().refresh()
self.attrs = (
self.device_state_attributes.copy() if self.device_state_attributes else {}
)
Expand Down
24 changes: 24 additions & 0 deletions teslajsonpy/charger.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@ def __init__(self, data, controller):
async def async_update(self, wake_if_asleep=False, force=False) -> None:
"""Update the charging state of the Tesla Vehicle."""
await super().async_update(wake_if_asleep=wake_if_asleep)
self.refresh()

def refresh(self) -> None:
"""Refresh data.
This assumes the controller has already been updated
"""
super().refresh()
last_update = self._controller.get_last_update_time(self._id)
if last_update >= self.__manual_update_time:
data = self._controller.get_charging_params(self._id)
Expand Down Expand Up @@ -97,6 +105,14 @@ def __init__(self, data, controller):
async def async_update(self, wake_if_asleep=False, force=False) -> None:
"""Update the status of the range setting."""
await super().async_update(wake_if_asleep=wake_if_asleep)
self.refresh()

def refresh(self) -> None:
"""Refresh data.
This assumes the controller has already been updated
"""
super().refresh()
last_update = self._controller.get_last_update_time(self._id)
if last_update >= self.__manual_update_time:
data = self._controller.get_charging_params(self._id)
Expand Down Expand Up @@ -165,6 +181,14 @@ def __init__(self, data: Dict, controller) -> None:
async def async_update(self, wake_if_asleep=False, force=False) -> None:
"""Update the battery state."""
await super().async_update(wake_if_asleep=wake_if_asleep)
self.refresh()

def refresh(self) -> None:
"""Refresh data.
This assumes the controller has already been updated
"""
super().refresh()
data = self._controller.get_gui_params(self._id)
if data:
self.measurement = data["gui_distance_units"]
Expand Down
16 changes: 16 additions & 0 deletions teslajsonpy/climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,14 @@ def get_fan_status(self):
async def async_update(self, wake_if_asleep=False, force=False) -> None:
"""Update the HVAC state."""
await super().async_update(wake_if_asleep=wake_if_asleep, force=force)
self.refresh()

def refresh(self) -> None:
"""Refresh data.
This assumes the controller has already been updated
"""
super().refresh()
data = self._controller.get_climate_params(self._id)
if data:
last_update = self._controller.get_last_update_time(self._id)
Expand Down Expand Up @@ -222,6 +230,14 @@ def get_outside_temp(self):
async def async_update(self, wake_if_asleep=False, force=False) -> None:
"""Update the temperature."""
await super().async_update(wake_if_asleep=wake_if_asleep)
self.refresh()

def refresh(self) -> None:
"""Refresh data.
This assumes the controller has already been updated
"""
super().refresh()
data = self._controller.get_climate_params(self._id)
if data:
self.__inside_temp = (
Expand Down
16 changes: 16 additions & 0 deletions teslajsonpy/gps.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,14 @@ def get_location(self):
async def async_update(self, wake_if_asleep=False, force=False) -> None:
"""Update the current GPS location."""
await super().async_update(wake_if_asleep=wake_if_asleep)
self.refresh()

def refresh(self) -> None:
"""Refresh data.
This assumes the controller has already been updated
"""
super().refresh()
data = self._controller.get_drive_params(self._id)
if data:
if data["native_location_supported"]:
Expand Down Expand Up @@ -115,6 +123,14 @@ def __init__(self, data, controller):
async def async_update(self, wake_if_asleep=False, force=False) -> None:
"""Update the odometer and the unit of measurement based on GUI."""
await super().async_update(wake_if_asleep=wake_if_asleep)
self.refresh()

def refresh(self) -> None:
"""Refresh data.
This assumes the controller has already been updated
"""
super().refresh()
data = self._controller.get_state_params(self._id)
if data:
self.__odometer = data["odometer"]
Expand Down
16 changes: 16 additions & 0 deletions teslajsonpy/lock.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@ def __init__(self, data, controller):
async def async_update(self, wake_if_asleep=False, force=False) -> None:
"""Update the lock state."""
await super().async_update(wake_if_asleep=wake_if_asleep)
self.refresh()

def refresh(self) -> None:
"""Refresh data.
This assumes the controller has already been updated
"""
super().refresh()
last_update = self._controller.get_last_update_time(self._id)
if last_update >= self.__manual_update_time:
data = self._controller.get_state_params(self._id)
Expand Down Expand Up @@ -117,6 +125,14 @@ def __init__(self, data, controller):
async def async_update(self, wake_if_asleep=False, force=False) -> None:
"""Update state of the charger lock."""
await super().async_update(wake_if_asleep=wake_if_asleep)
self.refresh()

def refresh(self) -> None:
"""Refresh data.
This assumes the controller has already been updated
"""
super().refresh()
last_update = self._controller.get_last_update_time(self._id)
if last_update >= self.__manual_update_time:
data = self._controller.get_charging_params(self._id)
Expand Down
8 changes: 8 additions & 0 deletions teslajsonpy/sentry_mode.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@ def __init__(self, data, controller):
async def async_update(self, wake_if_asleep=False, force=False):
"""Update the sentry mode of the vehicle."""
await super().async_update(wake_if_asleep=wake_if_asleep)
self.refresh()

def refresh(self) -> None:
"""Refresh data.
This assumes the controller has already been updated
"""
super().refresh()
last_update = self._controller.get_last_update_time(self._id)
if last_update >= self.__manual_update_time:
data = self._controller.get_state_params(self._id)
Expand Down
16 changes: 16 additions & 0 deletions teslajsonpy/trunk.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ def __init__(self, data, controller):
async def async_update(self, wake_if_asleep=False, force=False) -> None:
"""Update the rear trunk state."""
await super().async_update(wake_if_asleep=wake_if_asleep, force=force)
self.refresh()

def refresh(self) -> None:
"""Refresh data.
This assumes the controller has already been updated
"""
super().refresh()
last_update = self._controller.get_last_update_time(self._id)
if last_update >= self.__manual_update_time:
data = self._controller.get_state_params(self._id)
Expand Down Expand Up @@ -97,6 +105,14 @@ def __init__(self, data, controller):
async def async_update(self, wake_if_asleep=False, force=False) -> None:
"""Update the front trunk (frunk) state."""
await super().async_update(wake_if_asleep=wake_if_asleep, force=force)
self.refresh()

def refresh(self) -> None:
"""Refresh data.
This assumes the controller has already been updated
"""
super().refresh()
last_update = self._controller.get_last_update_time(self._id)
if last_update >= self.__manual_update_time:
data = self._controller.get_state_params(self._id)
Expand Down
13 changes: 12 additions & 1 deletion teslajsonpy/vehicle.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,21 @@ def assumed_state(self) -> bool:
async def async_update(
self, wake_if_asleep: bool = False, force: bool = False
) -> None:
"""Update the vehicle data."""
"""Update the vehicle data.
This function will call a controller update.
"""
await self._controller.update(
self.id(), wake_if_asleep=wake_if_asleep, force=force
)
self.refresh()

def refresh(self) -> None:
"""Refresh the vehicle data.
This assumes the controller has already been updated. This should be
called by inherited classes so the overall vehicle information is updated.
"""
state = self._controller.get_state_params(self.id())
if state and "car_version" in state:
self._car_version = state["car_version"]
Expand Down

0 comments on commit 9793ee0

Please sign in to comment.