Skip to content

Commit

Permalink
fix: allow pwmX_enable file to be absent
Browse files Browse the repository at this point in the history
  • Loading branch information
desbma committed Apr 27, 2022
1 parent dbf581d commit 47d9de3
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions hddfancontrol/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -693,7 +693,7 @@ def __init__(self, id: int, pwm_filepath: str, start_value: int, stop_value: int
assert pwm_num_match is not None
pwm_num = int(pwm_num_match.group(1))
self.fan_input_filepath = os.path.join(os.path.dirname(self.pwm_filepath), f"fan{pwm_num}_input")
self.enable_filepath = f"{self.pwm_filepath}_enable"
self.enable_filepath: Optional[str] = f"{self.pwm_filepath}_enable"
self.start_value = start_value
self.stop_value = stop_value
self.startup = False
Expand Down Expand Up @@ -757,12 +757,16 @@ def setSpeed(self, target_prct: int) -> None:
def setPwmValue(self, value: int) -> None:
"""Set fan PWM value."""
assert 0 <= value <= 255
with open(self.enable_filepath, "r+t") as enable_file:
enabled_val = int(enable_file.read().strip())
if enabled_val != 1:
self.logger.warning(f"{self.enable_filepath} was {enabled_val}, setting it to 1")
enable_file.seek(0)
enable_file.write("1")
if self.enable_filepath is not None:
try:
with open(self.enable_filepath, "r+t") as enable_file:
enabled_val = int(enable_file.read().strip())
if enabled_val != 1:
self.logger.warning(f"{self.enable_filepath} was {enabled_val}, setting it to 1")
enable_file.seek(0)
enable_file.write("1")
except FileNotFoundError:
self.enable_filepath = None
self.logger.debug(f"Setting PWM value to {value}")
with open(self.pwm_filepath, "wt") as pwm_file:
pwm_file.write(str(value))
Expand Down

0 comments on commit 47d9de3

Please sign in to comment.