|
15 | 15 | # ------------------------------------------------------------------------------ |
16 | 16 | from __future__ import annotations |
17 | 17 |
|
18 | | -from typing import TypeVar |
| 18 | +from typing import Any, TypeVar |
19 | 19 |
|
20 | 20 | from pydantic import Field |
21 | 21 |
|
@@ -337,28 +337,45 @@ def from_vnish(cls, web_settings: dict): |
337 | 337 |
|
338 | 338 | @classmethod |
339 | 339 | def from_boser(cls, grpc_miner_conf: dict): |
340 | | - try: |
341 | | - temperature_conf = grpc_miner_conf["temperature"] |
342 | | - except LookupError: |
| 340 | + temperature_conf = grpc_miner_conf.get("temperature") |
| 341 | + if not isinstance(temperature_conf, dict): |
343 | 342 | return cls.default() |
344 | 343 |
|
345 | | - keys = temperature_conf.keys() |
346 | | - if "auto" in keys: |
347 | | - if "minimumRequiredFans" in keys: |
348 | | - return cls.normal(minimum_fans=temperature_conf["minimumRequiredFans"]) |
| 344 | + def _maybe_int(value: Any) -> int | None: |
| 345 | + if isinstance(value, (int, float, str)) and value != "": |
| 346 | + try: |
| 347 | + return int(value) |
| 348 | + except (TypeError, ValueError): |
| 349 | + return None |
| 350 | + return None |
| 351 | + |
| 352 | + min_fans = _maybe_int(temperature_conf.get("minimumRequiredFans")) |
| 353 | + |
| 354 | + def _build_conf(conf_section: object) -> dict: |
| 355 | + conf: dict = {} |
| 356 | + if isinstance(conf_section, dict): |
| 357 | + speed = _maybe_int(conf_section.get("fanSpeedRatio")) |
| 358 | + if speed is not None: |
| 359 | + conf["speed"] = speed |
| 360 | + if min_fans is not None: |
| 361 | + conf["minimum_fans"] = min_fans |
| 362 | + return conf |
| 363 | + |
| 364 | + if "auto" in temperature_conf: |
| 365 | + if min_fans is not None: |
| 366 | + return cls.normal(minimum_fans=min_fans) |
349 | 367 | return cls.normal() |
350 | | - if "manual" in keys: |
351 | | - conf = {} |
352 | | - if "fanSpeedRatio" in temperature_conf["manual"].keys(): |
353 | | - conf["speed"] = int(temperature_conf["manual"]["fanSpeedRatio"]) |
354 | | - if "minimumRequiredFans" in keys: |
355 | | - conf["minimum_fans"] = int(temperature_conf["minimumRequiredFans"]) |
356 | | - return cls.manual(**conf) |
357 | | - if "disabled" in keys: |
358 | | - conf = {} |
359 | | - if "fanSpeedRatio" in temperature_conf["disabled"].keys(): |
360 | | - conf["speed"] = int(temperature_conf["disabled"]["fanSpeedRatio"]) |
| 368 | + |
| 369 | + if "manual" in temperature_conf: |
| 370 | + conf = _build_conf(temperature_conf.get("manual")) |
361 | 371 | return cls.manual(**conf) |
| 372 | + |
| 373 | + if "disabled" in temperature_conf: |
| 374 | + conf = _build_conf(temperature_conf.get("disabled")) |
| 375 | + if conf.get("speed") is not None: |
| 376 | + return cls.manual(**conf) |
| 377 | + return cls.immersion() |
| 378 | + |
362 | 379 | return cls.default() |
363 | 380 |
|
364 | 381 | @classmethod |
|
0 commit comments