def test_hddtemp_exec_failed(): t = HDDTemp( disk_path="/dev/sd?", min=TempCelsius(38.0), max=TempCelsius(45.0), panic=TempCelsius(50.0), threshold=None, hddtemp_bin="false", ) with pytest.raises(subprocess.CalledProcessError): t._call_hddtemp()
def test_hddtemp_exec_successful(temp_path): (temp_path / "sda").write_text("") (temp_path / "sdz").write_text("") t = HDDTemp( disk_path=str(temp_path / "sd") + "?", min=TempCelsius(38.0), max=TempCelsius(45.0), panic=TempCelsius(50.0), threshold=None, hddtemp_bin="printf '@%s'", ) expected_out = "@-n@-u@C@--@{0}/sda@{0}/sdz".format(temp_path) assert expected_out == t._call_hddtemp()
def test_hddtemp_bad(hddtemp_output_bad): with patch.object(HDDTemp, "_call_hddtemp") as mock_call_hddtemp: mock_call_hddtemp.return_value = hddtemp_output_bad t = HDDTemp( disk_path="/dev/sda", min=TempCelsius(38.0), max=TempCelsius(45.0), panic=TempCelsius(50.0), threshold=None, hddtemp_bin="testbin", ) with pytest.raises(RuntimeError): t.get()
def test_hddtemp_many(hddtemp_output_many): with patch.object(HDDTemp, "_call_hddtemp") as mock_call_hddtemp: mock_call_hddtemp.return_value = hddtemp_output_many t = HDDTemp( disk_path="/dev/sd?", min=TempCelsius(38.0), max=TempCelsius(45.0), panic=TempCelsius(50.0), threshold=None, hddtemp_bin="testbin", ) assert t.get() == TempStatus( temp=TempCelsius(39.0), min=TempCelsius(38.0), max=TempCelsius(45.0), panic=TempCelsius(50.0), threshold=None, is_panic=False, is_threshold=False, ) print(repr(t))
def _parse_temps( config: configparser.ConfigParser, hddtemp: str ) -> Tuple[Mapping[TempName, Temp], Mapping[TempName, Actions]]: temps = {} # type: Dict[TempName, Temp] temp_commands = {} # type: Dict[TempName, Actions] for section_name in config.sections(): section_name_parts = section_name.split(":", 1) if section_name_parts[0].strip().lower() != "temp": continue temp_name = TempName(section_name_parts[1].strip()) temp = config[section_name] keys = set(temp.keys()) actions_panic = AlertCommands( enter_cmd=first_not_none(temp.get("panic_enter_cmd")), leave_cmd=first_not_none(temp.get("panic_leave_cmd")), ) keys.discard("panic_enter_cmd") keys.discard("panic_leave_cmd") actions_threshold = AlertCommands( enter_cmd=first_not_none(temp.get("threshold_enter_cmd")), leave_cmd=first_not_none(temp.get("threshold_leave_cmd")), ) keys.discard("threshold_enter_cmd") keys.discard("threshold_leave_cmd") panic = TempCelsius(temp.getfloat("panic")) threshold = TempCelsius(temp.getfloat("threshold")) min = TempCelsius(temp.getfloat("min")) max = TempCelsius(temp.getfloat("max")) keys.discard("panic") keys.discard("threshold") keys.discard("min") keys.discard("max") type = temp["type"] keys.discard("type") if type == "file": t = FileTemp(temp["path"], min=min, max=max, panic=panic, threshold=threshold) # type: Temp keys.discard("path") elif type == "hdd": if min is None or max is None: raise RuntimeError( "hdd temp '%s' doesn't define the mandatory `min` and `max` temps" % temp_name) t = HDDTemp( temp["path"], min=min, max=max, panic=panic, threshold=threshold, hddtemp_bin=hddtemp, ) keys.discard("path") elif type == "exec": t = CommandTemp(temp["command"], min=min, max=max, panic=panic, threshold=threshold) keys.discard("command") else: raise RuntimeError("Unsupported temp type '%s' for temp '%s'" % (type, temp_name)) if keys: raise RuntimeError("Unknown options in the [%s] section: %s" % (section_name, keys)) if temp_name in temps: raise RuntimeError("Duplicate temp section declaration for '%s'" % temp_name) temps[temp_name] = t temp_commands[temp_name] = Actions(panic=actions_panic, threshold=actions_threshold) if not temps: raise RuntimeError( "No temps found in the config, at least 1 must be specified") return temps, temp_commands
def test_example_conf(example_conf: Path): daemon_cli_config = DaemonCLIConfig(pidfile=None, logfile=None, exporter_listen_host=None) parsed = parse_config(example_conf, daemon_cli_config) assert parsed == ParsedConfig( daemon=DaemonConfig( pidfile="/run/afancontrol.pid", logfile="/var/log/afancontrol.log", exporter_listen_host="127.0.0.1:8083", interval=5, ), report_cmd= ('printf "Subject: %s\nTo: %s\n\n%b" ' '"afancontrol daemon report: %REASON%" root "%MESSAGE%" | sendmail -t' ), triggers=TriggerConfig( global_commands=Actions( panic=AlertCommands(enter_cmd=None, leave_cmd=None), threshold=AlertCommands(enter_cmd=None, leave_cmd=None), ), temp_commands={ TempName("hdds"): Actions( panic=AlertCommands(enter_cmd=None, leave_cmd=None), threshold=AlertCommands(enter_cmd=None, leave_cmd=None), ), TempName("mobo"): Actions( panic=AlertCommands(enter_cmd=None, leave_cmd=None), threshold=AlertCommands(enter_cmd=None, leave_cmd=None), ), }, ), fans={ FanName("cpu"): PWMFanNorm( LinuxPWMFan( PWMDevice("/sys/class/hwmon/hwmon0/device/pwm1"), FanInputDevice( "/sys/class/hwmon/hwmon0/device/fan1_input"), ), pwm_line_start=PWMValue(100), pwm_line_end=PWMValue(240), never_stop=True, ), FanName("hdd"): PWMFanNorm( LinuxPWMFan( PWMDevice("/sys/class/hwmon/hwmon0/device/pwm2"), FanInputDevice( "/sys/class/hwmon/hwmon0/device/fan2_input"), ), pwm_line_start=PWMValue(100), pwm_line_end=PWMValue(240), never_stop=False, ), FanName("my_arduino_fan"): PWMFanNorm( ArduinoPWMFan( ArduinoConnection( ArduinoName("mymicro"), "/dev/ttyACM0", # linux # "/dev/cu.usbmodem14201", # macos baudrate=115200, status_ttl=5, ), pwm_pin=ArduinoPin(9), tacho_pin=ArduinoPin(3), ), pwm_line_start=PWMValue(100), pwm_line_end=PWMValue(240), never_stop=True, ), }, temps={ TempName("hdds"): HDDTemp( "/dev/sd?", min=TempCelsius(35.0), max=TempCelsius(48.0), panic=TempCelsius(55.0), threshold=None, hddtemp_bin="hddtemp", ), TempName("mobo"): FileTemp( "/sys/class/hwmon/hwmon0/device/temp1_input", min=TempCelsius(30.0), max=TempCelsius(40.0), panic=None, threshold=None, ), }, mappings={ MappingName("1"): FansTempsRelation( temps=[TempName("mobo"), TempName("hdds")], fans=[ FanSpeedModifier(fan=FanName("cpu"), modifier=1.0), FanSpeedModifier(fan=FanName("hdd"), modifier=0.6), FanSpeedModifier(fan=FanName("my_arduino_fan"), modifier=0.222), ], ), MappingName("2"): FansTempsRelation( temps=[TempName("hdds")], fans=[FanSpeedModifier(fan=FanName("hdd"), modifier=1.0)], ), }, )