import os import json import shutil class Config(): def __init__(self, configfile): self.__config_file = configfile self.__config = None try: with open(self.__config_file, "r") as handle: self.__config = json.load(handle) except FileNotFoundError: # create default config os.makedirs(os.path.dirname(self.__config_file), exist_ok=True) shutil.copyfile("config/config.json", self.__config_file) with open(self.__config_file, "r") as handle: self.__config = json.load(handle) def hostinfo(self): res = self.__config['hostname'], int(self.__config['port']) return res def get_heat_autostate(self, ident: int): for entry in self.__config['heat']: if int(entry['id']) == ident: return entry['autostate'] return None def set_heat_autostate(self, ident: str): for idx in range(len(self.__config['heat'])): if self.__config['heat'][idx]['id'] == ident: self.__config['heat'][idx]['autostate'] = True with open(self.__config_file, "w") as handle: json.dump(self.__config, handle) return def clear_heat_autostate(self, ident: str): for idx in range(len(self.__config['heat'])): if self.__config['heat'][idx]['id'] == ident: self.__config['heat'][idx]['autostate'] = False with open(self.__config_file, "w") as handle: json.dump(self.__config, handle) return def increase_on_temperature(self, ident: str): for idx in range(len(self.__config['heat'])): if self.__config['heat'][idx]['id'] == ident: on_temp = int(self.__config['heat'][idx]['on_temperature']) on_temp += 1 self.__config['heat'][idx]['on_temperature'] = str(on_temp) with open(self.__config_file, "w") as handle: json.dump(self.__config, handle) return def decrease_on_temperature(self, ident: str): for idx in range(len(self.__config['heat'])): if self.__config['heat'][idx]['id'] == ident: on_temp = int(self.__config['heat'][idx]['on_temperature']) on_temp -= 1 self.__config['heat'][idx]['on_temperature'] = str(on_temp) with open(self.__config_file, "w") as handle: json.dump(self.__config, handle) return def increase_off_temperature(self, ident: str): for idx in range(len(self.__config['heat'])): if self.__config['heat'][idx]['id'] == ident: off_temp = int(self.__config['heat'][idx]['off_temperature']) off_temp += 1 self.__config['heat'][idx]['off_temperature'] = str(off_temp) with open(self.__config_file, "w") as handle: json.dump(self.__config, handle) return def decrease_off_temperature(self, ident: str): for idx in range(len(self.__config['heat'])): if self.__config['heat'][idx]['id'] == ident: off_temp = int(self.__config['heat'][idx]['off_temperature']) off_temp -= 1 self.__config['heat'][idx]['off_temperature'] = str(off_temp) with open(self.__config_file, "w") as handle: json.dump(self.__config, handle) return def get_on_temperature(self, ident: int): for entry in self.__config['heat']: if int(entry['id']) == ident: return entry['on_temperature'] return None def get_off_temperature(self, ident: int): for entry in self.__config['heat']: if int(entry['id']) == ident: return entry['off_temperature'] return None def get_water_autostate(self, ident: int): for entry in self.__config['water']: if int(entry['id']) == ident: return entry['autostate'] return None def get_water_times(self, ident: int): for entry in self.__config['water']: if int(entry['id']) == ident: return entry['times'] return None def set_water_autostate(self, ident: str): for idx in range(len(self.__config['water'])): if self.__config['water'][idx]['id'] == ident: self.__config['water'][idx]['autostate'] = True with open(self.__config_file, "w") as handle: json.dump(self.__config, handle) return def clear_water_autostate(self, ident: str): for idx in range(len(self.__config['water'])): if self.__config['water'][idx]['id'] == ident: self.__config['water'][idx]['autostate'] = False with open(self.__config_file, "w") as handle: json.dump(self.__config, handle) return def set_water_times(self, ident: str, times): for idx in range(len(self.__config['water'])): if self.__config['water'][idx]['id'] == ident: self.__config['water'][idx]['times'] = times with open(self.__config_file, "w") as handle: json.dump(self.__config, handle) return