Skip to content

Commit

Permalink
Add MQTT for GRDF
Browse files Browse the repository at this point in the history
based/tested only on Alpine docker setup
  • Loading branch information
vingerha committed Feb 29, 2024
1 parent 17b9239 commit 6511d70
Showing 1 changed file with 63 additions and 3 deletions.
66 changes: 63 additions & 3 deletions apps/meters_to_ha/meters_to_ha.py
Original file line number Diff line number Diff line change
Expand Up @@ -1899,8 +1899,7 @@ def get_gazpar_file(self):
)

# Next line would log without hiding data
# self.mylog(r"Get Data URL {}".format(data_url), end="~~")

# self.mylog(r"Get Data URL {}".format(data_url), end="~~")
# Go to the URL.
dl_starttime = time.time()
self.__browser.get(data_url)
Expand Down Expand Up @@ -2905,7 +2904,68 @@ def update_veolia_device(self, csv_file):
)

def update_grdf_device(self, json_file):
pass
# pylint:disable=import-outside-toplevel
data = ""
import paho.mqtt.client as mqtt
from paho.mqtt import publish

#data = self.veolia_to_dict(json_file)
with open(json_file, encoding="utf_8") as f:
data = json.load(f)

if len(data) > 0:
pce = list(data.keys())[0]

Check failure on line 2917 in apps/meters_to_ha/meters_to_ha.py

View workflow job for this annotation

GitHub Actions / pre-commit

meters_to_ha.py: "str" has no attribute "keys" [attr-defined]
data = data[pce]["releves"][-1]

Check failure on line 2918 in apps/meters_to_ha/meters_to_ha.py

View workflow job for this annotation

GitHub Actions / pre-commit

meters_to_ha.py: Invalid index type "str" for "str"; expected type "SupportsIndex | slice" [index]
self.mylog(f"MQTT GRDF data: {data}")
else:
self.mylog(f"MQTT GRDF data: file empty")

Check failure on line 2921 in apps/meters_to_ha/meters_to_ha.py

View workflow job for this annotation

GitHub Actions / pre-commit

meters_to_ha.py: F541 f-string is missing placeholders

Check failure on line 2921 in apps/meters_to_ha/meters_to_ha.py

View workflow job for this annotation

GitHub Actions / pre-commit

meters_to_ha.py: W1309: Using an f-string that does not have any interpolated variables (f-string-without-interpolation)
pass

if data is not None:
state_topic = f"grdf/{pce}/last_data"
mqtt_server = self.configuration[PARAM_MQTT_SERVER]
mqtt_port = int(self.configuration[PARAM_MQTT_PORT])
mqtt_login = self.configuration[PARAM_MQTT_LOGIN]
mqtt_password = self.configuration[PARAM_MQTT_PASSWORD]
auth = {"username": mqtt_login, "password": mqtt_password}
# tls_dict= {'ca_certs':"<ca_certs>", 'certfile':"<certfile>",
# 'keyfile':"<keyfile>", 'tls_version':"<tls_version>",
# 'ciphers':"<ciphers">}
tls_dict = None
# will = {'topic': "<topic>", 'payload':"<payload">,
# 'qos':<qos>, 'retain':<retain>}

self.mylog(
f"MQTT GRDF Publish {mqtt_server}:{mqtt_port} {auth} {data!r}"
)
data_out: dict[str, Any] = {}
data_out = {

Check failure on line 2942 in apps/meters_to_ha/meters_to_ha.py

View workflow job for this annotation

GitHub Actions / pre-commit

meters_to_ha.py: "str" has no attribute "get" [attr-defined]
"datetime_period_start": data.get("dateDebutReleve",None),

Check failure on line 2943 in apps/meters_to_ha/meters_to_ha.py

View workflow job for this annotation

GitHub Actions / pre-commit

meters_to_ha.py: "str" has no attribute "get" [attr-defined]
"datetime_period_end": data.get("dateFinReleve", None),

Check failure on line 2944 in apps/meters_to_ha/meters_to_ha.py

View workflow job for this annotation

GitHub Actions / pre-commit

meters_to_ha.py: "str" has no attribute "get" [attr-defined]
"date_period_start": data.get("journeeGaziere", None),

Check failure on line 2945 in apps/meters_to_ha/meters_to_ha.py

View workflow job for this annotation

GitHub Actions / pre-commit

meters_to_ha.py: "str" has no attribute "get" [attr-defined]
"index_period_start_m3": data.get("indexDebut", None),

Check failure on line 2946 in apps/meters_to_ha/meters_to_ha.py

View workflow job for this annotation

GitHub Actions / pre-commit

meters_to_ha.py: "str" has no attribute "get" [attr-defined]
"index_period_end_m3": data.get("indexFin", None),
"volume_period_m3": data.get("volumeBrutConsomme", None),
"energy_period_kWh": data.get("energieConsomme", None),
"conversion_coefficient": data.get("coeffConversion", None),
"status": data.get("status", None)
}

publish.single(
state_topic,
payload=json.dumps(data_out),
qos=0,
retain=True, # Retain this data as a state
hostname=mqtt_server,
port=mqtt_port,
# will=will,
auth=auth,
keepalive=60,
client_id="",
tls=tls_dict,
protocol=mqtt.MQTTv311,
transport="tcp",
)

def cleanup(self, keep_output=False):
pass
Expand Down

0 comments on commit 6511d70

Please sign in to comment.