The SteamVR input has recently changed completely, it now uses Actions and here is a step by step tutorial from valve on how to set it up.
Here's one of the (best) ways to poll it in code:
void Update(){
//NOTE: I could have used one-off events. But I used these constant Update() polls instead because if the VR Dashboard is on while the headset is taken off or put on,
//then the OnDown and OnUp events get eaten by the dashboard and will never reach this code in unity. (github issue here: https://github.com/ValveSoftware/openvr/issues/628)
// m_inputSourceDevice needs to be Any - there's no entry for "Headset", but the headset's sensors are reported under the Any "hand".
SteamVR_Input_Sources m_inputSourceDevice = SteamVR_Input_Sources.Any;
if(!m_isHeadsetOn && SteamVR_Input._default.inActions.Proximity_Sensor.GetState(m_inputSourceDevice)){
Debug.Log("Headset On.");
HeadsetOn();
}
else if(m_isHeadsetOn && !SteamVR_Input._default.inActions.Proximity_Sensor.GetState(m_inputSourceDevice)){
Debug.Log("Headset Off (cancellable)");
StartCoroutine(HeadsetOff());
}
}
Note that as of right now, there is a bug that prevents you from using SteamVR to assign the Proximity Sensor to an action if you have a HTC Vive or Vive Pro, even though ironically it works as intended if you're using it with an Oculus headset. There is an easy fix on this steamvr github issues page.
Also note that even though the builtin unity OpenVR usually implements what SteamVR comes up with, for now it does not let you access the proximity sensor. The only thing you can do with OpenVR is to check the EDeviceActivityLevel
as MichaelHouse explained in his answer. However that is useless because the only level that involves the prox sensor is the k_EDeviceActivityLevel_UserInteraction
but it's a union of both the prox and the gyro, so according to OpenVR the headset is always interacting with the user forever as long as a gentle breeze or vibration tilts the headset a little bit once every 10 seconds.
[EDIT]
Some things got fixed in the meantime. For instance now there is an entry for the Hmd, instead of Any; and OpenVR does have an XRDevice.userPresence value (though it might not explicitly be the proximity sensor).