From c93865cf97b0af1e24804ee75740025518b17837 Mon Sep 17 00:00:00 2001 From: Giovanni Harting <539@idlegandalf.com> Date: Tue, 10 Mar 2020 02:17:31 +0100 Subject: [PATCH] added reloading of hwmap after file not found errors --- pyfan.py | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/pyfan.py b/pyfan.py index 6857d1e..53e4e1c 100644 --- a/pyfan.py +++ b/pyfan.py @@ -12,7 +12,7 @@ SYSFS_HWMON_BASE = "/sys/class/hwmon/" class ThermalZone: - def __init__(self, config, hwmon_map) -> None: + def __init__(self, config, pyfan_parent) -> None: self.fans = config["fan"] self.temp_source = config["source"] self.pid = PID(config["pid"]["p"], config["pid"]["i"], config["pid"]["d"], setpoint=0) @@ -20,7 +20,8 @@ class ThermalZone: self.factor = 1 / config["factor"] self.name = config["name"] self.target = config["target"] - self.hwmap = hwmon_map + self.hwmap = pyfan.hwmap + self.pyfan = pyfan_parent self.alias_replace = re.compile('|'.join(self.hwmap.keys())) self.setup_pwm() @@ -89,7 +90,8 @@ class ThermalZone: self.set_pwm_mode(target_fan, value) except FileNotFoundError as err: logging.getLogger("pyfan").warning("[%s] pwm not found." - " Not ready yet or wrong path? (%s)" % (self.name, err.strerror)) + " Try reloading hwmon map..." % self.name) + self.hwmap = self.pyfan.hwmap def replace_alias(self, path): replaced = self.alias_replace.sub(lambda x: self.hwmap[x.group()], path) @@ -122,8 +124,7 @@ class PyFan: self.config = self.__load_config(config) logging.basicConfig(level=logging.getLevelName(self.config["loglevel"])) self.zones = [] - self.hwmon_map = {} - self.gen_hwmon_map() + self._hwmon_map = {} if "interval" in self.config: self.interval = self.config["interval"] @@ -131,7 +132,7 @@ class PyFan: self.interval = 1 for zone in self.config["thermalzones"]: - self.zones.append(ThermalZone(zone, self.hwmon_map)) + self.zones.append(ThermalZone(zone, self)) logging.getLogger("pyfan").info( "Finished creating %d thermal zones, checking all %d seconds" % (len(self.zones), self.interval)) @@ -147,19 +148,23 @@ class PyFan: for zone in self.zones: zone.eval() - @staticmethod - def __load_config(path): - with open(path) as cfg_file: - return yaml.safe_load(cfg_file) + @property + def hwmap(self): + self._hwmon_map.clear() - def gen_hwmon_map(self): names = glob.glob(SYSFS_HWMON_BASE + "hwmon*/name") for name in names: hwmon = name.split("/")[-2] with open(name) as file: hwname = file.read().strip() - self.hwmon_map[hwname] = hwmon + self._hwmon_map[hwname] = hwmon + return self._hwmon_map + + @staticmethod + def __load_config(path): + with open(path) as cfg_file: + return yaml.safe_load(cfg_file) if __name__ == "__main__":