Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MQTT prefix #492

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 18 additions & 9 deletions lib/TWCManager/Vehicle/TeslaMateVehicle.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class TeslaMateVehicle:
__config = None
__configConfig = None
__configTeslaMate = None
__master = None
master = None
__mqtt_host = None
__mqtt_user = None
__mqtt_pass = None
Expand All @@ -29,7 +29,7 @@ class TeslaMateVehicle:
vehicles = {}

def __init__(self, master):
self.__master = master
self.master = master

self.__config = master.config
try:
Expand All @@ -44,7 +44,7 @@ def __init__(self, master):

# Unload if this module is disabled or misconfigured
if not self.status:
self.__master.releaseModule("lib.TWCManager.Vehicle", "TeslaMate")
self.master.releaseModule("lib.TWCManager.Vehicle", "TeslaMate")
return None

# Configure database parameters
Expand All @@ -57,7 +57,7 @@ def __init__(self, master):
self.__mqtt_host = self.__configTeslaMate.get("mqtt_host", None)
self.__mqtt_user = self.__configTeslaMate.get("mqtt_user", None)
self.__mqtt_pass = self.__configTeslaMate.get("mqtt_pass", None)
self.__mqtt_prefix = self.__configTeslaMate.get("mqtt_prefix", None)
self.__mqtt_prefix = self.__configTeslaMate.get("mqtt_prefix", "")

self.syncTelemetry = self.__configTeslaMate.get("syncTelemetry", False)
self.syncTokens = self.__configTeslaMate.get("syncTokens", False)
Expand Down Expand Up @@ -146,7 +146,7 @@ def doSyncTokens(self, firstrun=False):
result = cur.fetchone()

# Set Bearer and Refresh Tokens
carapi = self.__master.getModuleByName("TeslaAPI")
carapi = self.master.getModuleByName("TeslaAPI")
# We don't want to refresh the token - let the source handle that.
carapi.setCarApiTokenExpireTime(99999 * 99999 * 99999)
carapi.setCarApiBearerToken(result[0])
Expand Down Expand Up @@ -176,15 +176,24 @@ def doSyncTokens(self, firstrun=False):

def mqttConnect(self, client, userdata, flags, rc, properties=None):
logger.log(logging.INFO5, "MQTT Connected.")
logger.log(logging.INFO5, "Subscribe to " + self.__mqtt_prefix + "/cars/#")
res = client.subscribe(self.__mqtt_prefix + "/cars/#", qos=0)
subscription = "teslamate/"
if self.__mqtt_prefix:
subscription += self.__mqtt_prefix + "/"
subscription += "cars/#"
logger.log(logging.INFO5, "Subscribe to " + subscription)
res = client.subscribe(subscription, qos=0)
logger.log(logging.INFO5, "Res: " + str(res))

def mqttMessage(self, client, userdata, message):
topic = str(message.topic).split("/")
if len(topic) > 4:
prefix = topic[1:-3].join("/")
if prefix != self.__mqtt_prefix:
return
topic = [topic[0]] + topic[-3:]
payload = str(message.payload.decode("utf-8"))

if topic[0] == self.__mqtt_prefix and topic[1] == "cars":
if topic[0] == "teslamate" and topic[1] == "cars":
if topic[3] == "battery_level":
if self.vehicles.get(topic[2], None):
self.vehicles[topic[2]].batteryLevel = int(payload)
Expand Down Expand Up @@ -237,7 +246,7 @@ def updateVehicles(self, vehicle_id, vehicle_name):
# We already have this vehicle mapped
pass
else:
for apiVehicle in self.__master.getModuleByName(
for apiVehicle in self.master.getModuleByName(
"TeslaAPI"
).getCarApiVehicles():
if apiVehicle.name == vehicle_name:
Expand Down
Loading