"""Module for apcupsd.Requires ``apcaccess`` in ``$PATH``."""importloggingimportsalt.utils.pathfromsalt.utilsimportdecoratorslog=logging.getLogger(__name__)# Define the module's virtual name__virtualname__="apcups"@decorators.memoizedef_check_apcaccess():""" Looks to see if apcaccess is present on the system """returnsalt.utils.path.which("apcaccess")def__virtual__():if_check_apcaccess():return__virtualname__return(False,f"{__virtualname__} module can only be loaded on when apcupsd is installed",)
[docs]defstatus():""" Return apcaccess output CLI Example: .. code-block:: bash salt '*' apcups.status """ret={}apcaccess=_check_apcaccess()res=__salt__["cmd.run_all"](apcaccess)retcode=res["retcode"]ifretcode!=0:ret["Error"]="Something with wrong executing apcaccess, is apcupsd running?"returnretforlineinres["stdout"].splitlines():line=line.split(":")ret[line[0].strip()]=line[1].strip()returnret
[docs]defstatus_load():""" Return load CLI Example: .. code-block:: bash salt '*' apcups.status_load """data=status()if"LOADPCT"indata:load=data["LOADPCT"].split()ifload[1].lower()=="percent":returnfloat(load[0])return{"Error":"Load not available."}
[docs]defstatus_charge():""" Return battery charge CLI Example: .. code-block:: bash salt '*' apcups.status_charge """data=status()if"BCHARGE"indata:charge=data["BCHARGE"].split()ifcharge[1].lower()=="percent":returnfloat(charge[0])return{"Error":"Load not available."}
[docs]defstatus_battery():""" Return true if running on battery power CLI Example: .. code-block:: bash salt '*' apcups.status_battery """data=status()if"TONBATT"indata:returnnotdata["TONBATT"]=="0 Seconds"return{"Error":"Battery status not available."}