-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding Fanspeed Entity for Linux (#80)
* init fanspeed and first mock * working for linux, still wip * add readme and todos * rename venv to be ignored * cleanup * remove debug prints * add Rotation ValueFormat as rpm * rename and create link to Deployments * change to hardlink, add units, move test to correct location, update images * implement config dialog * format and link * code more readable * docstrings and formatting, fix update * fix menu preset * more work on test * Delete .gitignore * Delete changes in IoTuring/Configurator/ConfiguratorIO.py * Delete softlink was a softlink for developement * remove dev temps * whoops forgot the code * cleanup * first mock for state as num of fans above threshold * configurable threshold, entity state = amount of fans above that threshold * docstrings * remove unit from entity state, but keep rpm for attributes * unify unix systems as psutil seems consistent, check for attributes on sensors * remove default from printed config string, redo sensorlabel fallback * redo configuration to be more readable and easier * update configuration, redo update method cleaner * restore consts.py * remove tests * add FALLBACK labels when blank, addd some logging, raise NotImplementedError on blank controllername * more granular check for psutil support/ found fans * remove config, extra classes and threshold, entityState->max(rpm),) * cleanup, add unit back since were only talking rpm now * Small semantics * Fanspeed is not multi instance * Update readme --------- Co-authored-by: infeeeee <gyetpet@mailbox.org>
- Loading branch information
Showing
5 changed files
with
123 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
import psutil | ||
from IoTuring.Entity.Entity import Entity | ||
from IoTuring.Entity.EntityData import EntitySensor | ||
from IoTuring.Entity.ValueFormat import ValueFormatterOptions | ||
from IoTuring.MyApp.SystemConsts import OperatingSystemDetection as OsD | ||
|
||
|
||
VALUEFORMATTEROPTIONS_FANSPEED_RPM = ValueFormatterOptions( | ||
value_type=ValueFormatterOptions.TYPE_ROTATION) | ||
|
||
|
||
FALLBACK_CONTROLLER_LABEL = "controller" | ||
FALLBACK_FAN_LABEL = "fan" | ||
|
||
|
||
class Fanspeed(Entity): | ||
"""Entity to read fanspeed""" | ||
NAME = "Fanspeed" | ||
|
||
def Initialize(self) -> None: | ||
"""Initialize the Class, setup Formatter, determin specificInitialize and specificUpdate depending on OS""" | ||
|
||
self.specificInitialize = None | ||
self.specificUpdate = None | ||
|
||
if OsD.IsLinux(): | ||
# psutil docs: no attribute -> system not supported | ||
if not hasattr(psutil, "sensors_fans"): | ||
raise Exception("System not supported by psutil") | ||
# psutil docs: empty dict -> no fancontrollers reporting | ||
if not bool(psutil.sensors_fans()): | ||
raise Exception("No fan found in system") | ||
self.specificInitialize = self.InitLinux | ||
self.specificUpdate = self.UpdateLinux | ||
|
||
else: | ||
raise NotImplementedError | ||
|
||
self.specificInitialize() | ||
|
||
def InitLinux(self) -> None: | ||
"""OS dependant Init for Linux""" | ||
sensors = psutil.sensors_fans() | ||
self.Log(self.LOG_DEBUG, f"fancontrollers found:{sensors}") | ||
|
||
for i, controller in enumerate(sensors): | ||
# use FALLBACK for blank controllernames | ||
controllerName = controller or FALLBACK_CONTROLLER_LABEL + str(i) | ||
|
||
# Add extra attributes only if there are multiple fans: | ||
hasMultipleFans = bool(len(sensors[controller]) > 1) | ||
|
||
# register an entity for each controller | ||
self.RegisterEntitySensor( | ||
EntitySensor( | ||
self, | ||
controllerName, | ||
supportsExtraAttributes=hasMultipleFans, | ||
valueFormatterOptions=VALUEFORMATTEROPTIONS_FANSPEED_RPM, | ||
) | ||
) | ||
|
||
def Update(self) -> None: | ||
"""placeholder for OS specificUpdate""" | ||
if self.specificUpdate: | ||
self.specificUpdate() | ||
else: | ||
raise NotImplementedError | ||
|
||
def UpdateLinux(self) -> None: | ||
"""Updatemethod for Linux""" | ||
for controller, fans in psutil.sensors_fans().items(): | ||
# get all fanspeed in a list and find max | ||
highest_fan = max([fan.current for fan in fans]) | ||
# find higest fanspeed and assign the entity state | ||
self.SetEntitySensorValue( | ||
key=controller, | ||
value=highest_fan) | ||
# Set extra attributes {fan name : fanspeed in rpm} | ||
self.Log(self.LOG_DEBUG, | ||
f"updating controller:{controller} with {fans}") | ||
|
||
# Add fans as extra attributes, if there are more than one: | ||
if len(fans) > 1: | ||
for i, fan in enumerate(fans): | ||
# appy FALLBACK if label is blank | ||
fanlabel = fan.label or FALLBACK_FAN_LABEL + str(i) | ||
|
||
# set extra attributes for each fan | ||
self.SetEntitySensorExtraAttribute( | ||
controller, | ||
fanlabel, | ||
fan.current, | ||
valueFormatterOptions=VALUEFORMATTEROPTIONS_FANSPEED_RPM, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters