Controlling the Power in my Homelab

I’ve been using Home Assistant for a while now, and it’s time to explore automation with my Homelab equipment. Home Assistant offers integrations for a wide range of power outlets and allows for the control of PoE ports as well.

Usually i power things with ZigBee Power Plugs or over PoE if possible. Raspberry PI comes in handy with the PoE hat or with an PoE to USB adapter.

We integrate the Power Switches with their integration, for ZigBee this is just pairing the power switch into the Zigbee Network and give them some name.

For Cisco Switches we have to do a bit more work. Currently i use Cisco WS-C2960C-12PC-L for PoE delivery.

We should start with checking that the port has PoE enabled.

#show power inline
Interface Admin  Oper       Power   Device              Class Max
                            (Watts)
--------- ------ ---------- ------- ------------------- ----- ----
Fa0/7     auto   on         8.2     SIP-MP54            3     15.4

Next we can get the interface index on the switch, this will need to translate to the POWER-ETHERNET-MIB index, those differ at least on this Platform as we will see later.

#show snmp mib ifmib ifindex  Fa0/7
Interface = FastEthernet0/7, Ifindex = 10007

From the POWER-ETHERNET-MIB we will use:

  • pethPsePortEntry 1.3.6.1.2.1.105.1.1.1
  • pethPsePortAdminEnable 1.3.6.1.2.1.105.1.1.1.3

Next enable SNMP on the switch, we will use SNMPv2.

First limit who will have access to the Community with an ACL.

#conf t
(config)# ip access-list standard ha-acl
(config-std-nacl)#permit host 

And limit the access to the SNMP Tree, to prevent accidental taking power away from ports that should stay enabled.

#conf t
(config)#snmp-server view ha-view ifIndex.10007 included
(config)#snmp-server view ha-view ifDescr.10007 included
(config)#snmp-server view ha-view ifEntry.*.*.* included
(config)#snmp-server view ha-view pethPsePortAdminEnable.*.7 included
(config)#snmp-server view ha-view pethPsePortType.*.7 included

Out comunity then will configured with the acl and view assigened.

#conf t
(config)#snmp-server community  view ha-view RW ha-acl

With this model the port fa0/7 has the index 10007 in the ifMib but in the poe mib this is just 7.

Now, we can verify if everything functions as intended. To do this, we can use tools like snmpwalk and snmpset.

$ snmpwalk -Ofn -v2c -c   
.1.3.6.1.2.1.2.2.1.1.10007 = INTEGER: 10007
.1.3.6.1.2.1.2.2.1.2.10007 = STRING: "FastEthernet0/7"
.1.3.6.1.2.1.105.1.1.1.3.1.7 = INTEGER: 1
.1.3.6.1.2.1.105.1.1.1.9.1.7 = STRING: "SIP-MP54"
.1.3.6.1.2.1.105.1.1.1.9.1.7 = No more variables left in this MIB View (It is past the end of the MIB tree)

To turn off the port, you can set pethPsePortAdminEnable to 2.

$ snmpset -v2c -c   .1.3.6.1.2.1.105.1.1.1.3.1.7 i 2
iso.3.6.1.2.1.105.1.1.1.3.1.7 = INTEGER: 2

To enable power on the port, you can set pethPsePortAdminEnable to 1.

$ snmpset -v2c -c   .1.3.6.1.2.1.105.1.1.1.3.1.7 i 1
iso.3.6.1.2.1.105.1.1.1.3.1.7 = INTEGER: 1

Certainly, we can review the switch logs to confirm the success of the action. For sure your device has powered off and is currently in the process of booting up.

248710: Nov  3 09:30:14.325 UTC: %LINEPROTO-5-UPDOWN: Line protocol on Interface FastEthernet0/7, changed state to down
248711: Nov  3 09:30:15.332 UTC: %LINK-3-UPDOWN: Interface FastEthernet0/7, changed state to down
248712: Nov  3 09:30:21.506 UTC: %ILPOWER-7-DETECT: Interface Fa0/7: Power Device detected: IEEE PD
248713: Nov  3 09:30:22.060 UTC: %ILPOWER-5-POWER_GRANTED: Interface Fa0/7: Power granted
248715: Nov  3 09:30:27.420 UTC: %LINK-3-UPDOWN: Interface FastEthernet0/7, changed state to up
248716: Nov  3 09:30:28.427 UTC: %LINEPROTO-5-UPDOWN: Line protocol on Interface FastEthernet0/7, changed state to up
248722: Nov  3 09:30:58.123 UTC: %LINEPROTO-5-UPDOWN: Line protocol on Interface FastEthernet0/7, changed state to down
248723: Nov  3 09:31:00.136 UTC: %LINEPROTO-5-UPDOWN: Line protocol on Interface FastEthernet0/7, changed state

Now, we can integrate this into Home Assistant (HA) to control the state of this port and automate the process.

switch:
  - platform: snmp
    name: cisco_snmp_poe_switch_p7
    host: 
    community:  
    baseoid: 1.3.6.1.2.1.105.1.1.1.3.1.7
    payload_on: 1
    payload_off: 2

With this integration into Home Assistant, we have various possibilities. We can use scripting or automation tools to interact with the API and control the power state of devices.

# power.py --device coffee --check
2023-11-03 15:00:16,094 coffee is off

just as a convenience.

Links

Photo by Thomas Kelley on Unsplash