Skip to content

Commit

Permalink
feat: use unknown state if hdparm fails (merges #46)
Browse files Browse the repository at this point in the history
  • Loading branch information
desbma committed Dec 17, 2023
1 parent 39fbafa commit f74a475
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 11 deletions.
17 changes: 10 additions & 7 deletions hddfancontrol/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,13 +267,16 @@ def getState(self) -> DriveState:
"sleeping": self.__class__.DriveState.SLEEPING,
}
cmd = ("hdparm", "-C", self.device_filepath)
with self.get_state_lock:
output = subprocess.check_output(
cmd, stdin=subprocess.DEVNULL, stderr=subprocess.DEVNULL, universal_newlines=True
)
self.get_state_count += 1
str_state = output.rsplit(" ", 1)[-1].strip()
state = states[str_state]
try:
with self.get_state_lock:
output = subprocess.check_output(
cmd, stdin=subprocess.DEVNULL, stderr=subprocess.DEVNULL, universal_newlines=True
)
self.get_state_count += 1
str_state = output.rsplit(" ", 1)[-1].strip()
state = states[str_state]
except subprocess.CalledProcessError:
state = self.__class__.DriveState.UNKNOWN
self.logger.debug(f"Drive state: {state.name}")
return state

Expand Down
6 changes: 2 additions & 4 deletions tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,8 +218,7 @@ def test_getState(self):
)
with unittest.mock.patch("hddfancontrol.subprocess.check_output") as subprocess_check_output_mock:
subprocess_check_output_mock.side_effect = subprocess.CalledProcessError(0, "")
with self.assertRaises(Exception):
self.drive.getState()
self.assertEqual(self.drive.getState(), hddfancontrol.Drive.DriveState.UNKNOWN)
subprocess_check_output_mock.assert_called_once_with(
("hdparm", "-C", "/dev/_sdz"),
stdin=subprocess.DEVNULL,
Expand Down Expand Up @@ -268,8 +267,7 @@ def test_isSleeping(self):
)
with unittest.mock.patch("hddfancontrol.subprocess.check_output") as subprocess_check_output_mock:
subprocess_check_output_mock.side_effect = subprocess.CalledProcessError(0, "")
with self.assertRaises(Exception):
self.drive.isSleeping()
self.assertFalse(self.drive.isSleeping())
subprocess_check_output_mock.assert_called_once_with(
("hdparm", "-C", "/dev/_sdz"),
stdin=subprocess.DEVNULL,
Expand Down

0 comments on commit f74a475

Please sign in to comment.