Skip to content

Commit

Permalink
rework config
Browse files Browse the repository at this point in the history
  • Loading branch information
m4dm4rtig4n committed Nov 18, 2021
1 parent d9fd685 commit 965f26c
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 83 deletions.
2 changes: 1 addition & 1 deletion app/VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.7.4
0.7.5-dev
1 change: 0 additions & 1 deletion app/function.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import main

from importlib import import_module

main = import_module("main")


Expand Down
23 changes: 12 additions & 11 deletions app/home_assistant.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
import json
from dateutil.relativedelta import *

from pprint import pprint
from importlib import import_module

main = import_module("main")
f = import_module("function")


def haAutodiscovery(client, type="Sensor", pdl=None, name=None, value=None, attributes=None, unit_of_meas=None,
def haAutodiscovery(client, config, type="Sensor", pdl=None, name=None, value=None, attributes=None, unit_of_meas=None,
device_class=None,
state_class=None):
name = name.replace("-", "_")
config = {

ha_config = {
"name": f"enedisgateway_{pdl}_{name}",
"uniq_id": f"enedisgateway.{pdl}_{name}",
"stat_t": f"{main.config['home_assistant']['discovery_prefix']}/{type}/enedisgateway/{pdl}_{name}/state",
"json_attr_t": f"{main.config['home_assistant']['discovery_prefix']}/{type}/enedisgateway/{pdl}_{name}/attributes",
"stat_t": f"{config['home_assistant']['discovery_prefix']}/{type}/enedisgateway/{pdl}_{name}/state",
"json_attr_t": f"{config['home_assistant']['discovery_prefix']}/{type}/enedisgateway/{pdl}_{name}/attributes",
"device": {
"identifiers": [f"linky_{pdl}"],
"name": f"Linky {pdl}",
Expand All @@ -24,14 +25,14 @@ def haAutodiscovery(client, type="Sensor", pdl=None, name=None, value=None, attr
}
}
if unit_of_meas is not None:
config['unit_of_meas'] = str(unit_of_meas)
ha_config['unit_of_meas'] = str(unit_of_meas)
if device_class is not None:
config['device_class'] = str(device_class)
ha_config['device_class'] = str(device_class)
if state_class is not None:
config['state_class'] = str(state_class)
ha_config['state_class'] = str(state_class)

f.publish(client, f"{type}/enedisgateway/{pdl}_{name}/config", json.dumps(config), main.config['home_assistant']['discovery_prefix'])
f.publish(client, f"{type}/enedisgateway/{pdl}_{name}/config", json.dumps(ha_config), config['home_assistant']['discovery_prefix'])
if attributes is not None:
f.publish(client, f"{type}/enedisgateway/{pdl}_{name}/attributes", json.dumps(attributes),
main.config['home_assistant']['discovery_prefix'])
f.publish(client, f"{type}/enedisgateway/{pdl}_{name}/state", str(value), main.config['home_assistant']['discovery_prefix'])
config['home_assistant']['discovery_prefix'])
f.publish(client, f"{type}/enedisgateway/{pdl}_{name}/state", str(value), config['home_assistant']['discovery_prefix'])
142 changes: 72 additions & 70 deletions app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@
}
}

global config

config_file = '/data/config.yaml'
if os.path.exists(config_file):
with open(r'/data/config.yaml') as file:
Expand All @@ -95,6 +97,70 @@
with open(r'/data/config.yaml') as file:
config = yaml.load(file, Loader=yaml.FullLoader)

lost_params = []
# CHECK GLOBAL CONFIGURATION
for id, data in default.items():
isDict = False
if isinstance(default[id], dict):
isDict = True
name = id
mandatory = False
if id in mandatory_parameters:
mandatory = True
name = id
if mandatory == True and not name in config:
lost_params.append(name.upper())
elif not isDict:
if not name in config:
config[name] = data

# CHECK MQTT CONFIGURATION
list = "mqtt"
for id, data in default[list].items():
mandatory = False
if id in mandatory_parameters["mqtt"]:
mandatory = True
if mandatory == True and not id in config[list]:
lost_params.append(f"{list}.{id.upper()}")
else:
if not id in config[list]:
config[list][id] = data

# CHECK HOME ASSISTANT CONFIGURATION
list = "home_assistant"
for id, data in default[list].items():
mandatory = False
if id in mandatory_parameters:
mandatory = True
if mandatory == True and not id in config[list]:
lost_params.append(f"{list}.{id.upper()}")
else:
if not id in config[list]:
config[list][id] = data

# CHECK ENEDIS GATEWAY CONFIGURATION
if not "enedis_gateway" in config:
lost_params.append("enedis_gateway")
else:
if not isinstance(config["enedis_gateway"], dict):
lost_params.append("enedis_gateway.PDL")
else:
for pdl, pdl_data in config["enedis_gateway"].items():
if len(str(pdl)) != 14:
lost_params.append(f"PDL must be 14 characters ({pdl} => {len(str(pdl))})")
if not isinstance(config["enedis_gateway"][pdl], dict):
lost_params.append(f"enedis_gateway.{pdl}.TOKEN")
else:
for id, data in default['enedis_gateway']['pdl'].items():
mandatory = False
if id in mandatory_parameters:
mandatory = True
if mandatory == True and not id in config["enedis_gateway"][pdl]:
lost_params.append(f"enedis_gateway.{pdl}.{id.upper()}")
else:
if not id in config["enedis_gateway"][pdl]:
config["enedis_gateway"][pdl][id] = data

def init_database(cur):
## CONFIG
cur.execute('''CREATE TABLE config (
Expand Down Expand Up @@ -200,7 +266,7 @@ def run(pdl, pdl_config):
offpeak_hours = data
if "home_assistant" in config and "discovery" in config['home_assistant'] and config['home_assistant'][
'discovery'] == True:
ha.haAutodiscovery(client=client, type="sensor", pdl=pdl, name=key, value=data)
ha.haAutodiscovery(config=config, client=client, type="sensor", pdl=pdl, name=key, value=data)

if pdl_config['addresses'] == True:
f.logLine()
Expand Down Expand Up @@ -243,7 +309,7 @@ def run(pdl, pdl_config):
else:
state_class = None
if "value" in sensor_data:
ha.haAutodiscovery(client=client, type="sensor", pdl=pdl, name=name,
ha.haAutodiscovery(config=config, client=client, type="sensor", pdl=pdl, name=name,
value=sensor_data['value'],
attributes=attributes, unit_of_meas=unit_of_meas,
device_class=device_class, state_class=state_class)
Expand Down Expand Up @@ -279,7 +345,7 @@ def run(pdl, pdl_config):
else:
state_class = None
if "value" in sensor_data:
ha.haAutodiscovery(client=client, type="sensor", pdl=pdl, name=name,
ha.haAutodiscovery(config=config, client=client, type="sensor", pdl=pdl, name=name,
value=sensor_data['value'],
attributes=attributes, unit_of_meas=unit_of_meas,
device_class=device_class, state_class=state_class)
Expand Down Expand Up @@ -315,7 +381,7 @@ def run(pdl, pdl_config):
state_class = sensor_data['state_class']
else:
state_class = None
ha.haAutodiscovery(client=client, type="sensor", pdl=pdl, name=name, value=sensor_data['value'],
ha.haAutodiscovery(config=config, client=client, type="sensor", pdl=pdl, name=name, value=sensor_data['value'],
attributes=attributes, unit_of_meas=unit_of_meas,
device_class=device_class, state_class=state_class)
f.log(" => HA Sensor updated")
Expand Down Expand Up @@ -350,7 +416,7 @@ def run(pdl, pdl_config):
else:
state_class = None
if "value" in sensor_data:
ha.haAutodiscovery(client=client, type="sensor", pdl=pdl, name=name,
ha.haAutodiscovery(config=config, client=client, type="sensor", pdl=pdl, name=name,
value=sensor_data['value'],
attributes=attributes, unit_of_meas=unit_of_meas,
device_class=device_class, state_class=state_class)
Expand Down Expand Up @@ -386,7 +452,7 @@ def run(pdl, pdl_config):
else:
state_class = None
if "value" in sensor_data:
ha.haAutodiscovery(client=client, type="sensor", pdl=pdl, name=name,
ha.haAutodiscovery(config=config, client=client, type="sensor", pdl=pdl, name=name,
value=sensor_data['value'],
attributes=attributes, unit_of_meas=unit_of_meas,
device_class=device_class, state_class=state_class)
Expand All @@ -410,70 +476,6 @@ def run(pdl, pdl_config):

if __name__ == '__main__':

lost_params = []
# CHECK GLOBAL CONFIGURATION
for id, data in default.items():
isDict = False
if isinstance(default[id], dict):
isDict = True
name = id
mandatory = False
if id in mandatory_parameters:
mandatory = True
name = id
if mandatory == True and not name in config:
lost_params.append(name.upper())
elif not isDict:
if not name in config:
config[name] = data

# CHECK MQTT CONFIGURATION
list = "mqtt"
for id, data in default[list].items():
mandatory = False
if id in mandatory_parameters["mqtt"]:
mandatory = True
if mandatory == True and not id in config[list]:
lost_params.append(f"{list}.{id.upper()}")
else:
if not id in config[list]:
config[list][id] = data

# CHECK HOME ASSISTANT CONFIGURATION
list = "home_assistant"
for id, data in default[list].items():
mandatory = False
if id in mandatory_parameters:
mandatory = True
if mandatory == True and not id in config[list]:
lost_params.append(f"{list}.{id.upper()}")
else:
if not id in config[list]:
config[list][id] = data

# CHECK ENEDIS GATEWAY CONFIGURATION
if not "enedis_gateway" in config:
lost_params.append("enedis_gateway")
else:
if not isinstance(config["enedis_gateway"], dict):
lost_params.append("enedis_gateway.PDL")
else:
for pdl, pdl_data in config["enedis_gateway"].items():
if len(str(pdl)) != 14:
lost_params.append(f"PDL must be 14 characters ({pdl} => {len(str(pdl))})")
if not isinstance(config["enedis_gateway"][pdl], dict):
lost_params.append(f"enedis_gateway.{pdl}.TOKEN")
else:
for id, data in default['enedis_gateway']['pdl'].items():
mandatory = False
if id in mandatory_parameters:
mandatory = True
if mandatory == True and not id in config["enedis_gateway"][pdl]:
lost_params.append(f"enedis_gateway.{pdl}.{id.upper()}")
else:
if not id in config["enedis_gateway"][pdl]:
config["enedis_gateway"][pdl][id] = data

if lost_params != []:
f.log(f'Some mandatory parameters are missing :', "ERROR")
for param in lost_params:
Expand Down

0 comments on commit 965f26c

Please sign in to comment.