@@ -977,6 +977,73 @@ def _get_params(self):
977977exports ["TasmotaPowerPort" ] = TasmotaPowerPortExport
978978
979979
980+ @attr .s (eq = False )
981+ class IPMIPowerPortExport (ResourceExport ):
982+ """ResourceExport for IPMIPowerPort devices"""
983+
984+ def __attrs_post_init__ (self ):
985+ super ().__attrs_post_init__ ()
986+ self .data ["cls" ] = self .cls
987+ from ..resource .power import IPMIPowerPort
988+ self .local = IPMIPowerPort (target = None , name = None , ** self .local_params )
989+
990+ self .lastpoll = 0
991+ self .job = None
992+ self .local .avail = False
993+ self .status = None
994+
995+ def poll (self ):
996+ now = time .monotonic ()
997+ if self .job :
998+ self .lastpoll = now
999+ if self .job .poll () is None :
1000+ return super ().poll ()
1001+ if self .job .returncode != 0 :
1002+ self .status = self ._ipmi_parse ()
1003+ self .local .avail = False
1004+ self .job = None
1005+ return super ().poll ()
1006+
1007+ self .status = self ._ipmi_parse ().upper ()
1008+ self .local .avail = True
1009+ self .job = None
1010+
1011+ if now - self .lastpoll < self .local .polling :
1012+ return super ().poll ()
1013+ self .lastpoll = now
1014+
1015+ if self .local .polling > 0 :
1016+ self .job = self ._ipmi_power ('--stat' )
1017+
1018+ return super ().poll ()
1019+
1020+ def _ipmi_power (self , cmd ):
1021+ runstr = f"ipmi-power -h { self .local .host } -u { self .local .username } "
1022+ runstr += f"--session-timeout={ self .local .timeout * 1000 } "
1023+ runstr += f"-p { self .local .password } { cmd } { self .local .args } "
1024+ return subprocess .Popen (runstr .strip ().split (' ' ), stdout = subprocess .PIPE ,
1025+ stderr = subprocess .PIPE )
1026+
1027+ def _ipmi_parse (self ):
1028+ out = self .job .stdout .read () + self .job .stderr .read ()
1029+ try :
1030+ return out .decode ('utf-8' ).split (':' )[1 ][1 :- 1 ]
1031+ except :
1032+ return ""
1033+
1034+ def _get_params (self ):
1035+ """Helper function to return parameters"""
1036+ return {
1037+ ** self .local_params ,
1038+ "extra" : {
1039+ "status" : self .status ,
1040+ }
1041+ }
1042+
1043+
1044+ exports ["IPMIPowerPort" ] = IPMIPowerPortExport
1045+
1046+
9801047class ExporterSession (ApplicationSession ):
9811048 def onConnect (self ):
9821049 """Set up internal datastructures on successful connection:
0 commit comments