sleevezipper / hass-workstation-service

Provide useful sensors and services from your workstation to Home Assistant.
Apache License 2.0
612 stars 54 forks source link

Feature request: Add support for Multiple Webcams #64

Open RDoull opened 3 years ago

RDoull commented 3 years ago

Please accept my appologies if this is the wrong place to make a request I wasn't sure where to put it so followed previous posts.

I have no idea if this is easy or hard to do, but if it is at all possible, could you add the ability to add multiple Webcams as sensors Please?

Ihave 4 Webcams set up, each requireing a different lighting set up and this will make it so much easier.

As I am typing this i just thought I can use a button.. but being able to change the lights depending on which cam is active would be somuch easier (for me at least).

Thank you for your time and for the program!

sleevezipper commented 3 years ago

Thanks for creating an issue!

This is not possible with our current implementation as Windows does not discriminate between different devices for its privacy logs. There mayb be other ways to get that information but that'll take some research.

josephmgaffney commented 3 years ago

Maybe looking for individual power states of USB devices? If it goes from D1/2/3 (low power states) to D0 (fully on state) then it would be active and in use

DennisGaida commented 2 years ago

I have looked into this and it seems pretty complicated since Windows doesn't offer nice APIs / any possibility to query this. I would very much love to be able to select which webcam is active as well - in my particular use case (or actually any modern setup) I have a virtual IR camera for Windows Hello authentication. This infrared camera is used for authentication and of course doesn't need any lights turned on since it's infrared and has its own infrared light source. Not important - but annoying that all lights turn on for authentication and I have no way to query what webcam is used and disable everything for this particular webcam.

In my particular use case I may get around using the webcam process sensor (https://github.com/sleevezipper/hass-workstation-service/blob/master/hass-workstation-service/Domain/Sensors/WebcamProcessSensor.cs), because auth is being done by another executable - I still would like to choose a webcam :)

The current implementation is to check the following registry key: https://github.com/sleevezipper/hass-workstation-service/blob/7fd20a4fc5718fcd5960e318ab81186b073af82d/hass-workstation-service/Domain/Sensors/WebcamActiveSensor.cs#L86 (as well as local machine). This registry key gets populated with the current executable using any webcam and the last time it was used - you do not know which webcam if you have multiple.

You can see this data nicely (Windows 10/11) when going to Settings > Privacy & security > camera. There you can see what app is currently using a webcam and the last time any app has accessed any webcam.

I have tried getting any webcam active state using WMI (e.g. using Get-PnPDevice & Get-PNPDeviceProperty like so get-pnpdevice | ? {$_.FriendlyName -like "*webcam*"} | Get-PnpDeviceProperty). Unfortunately WMI does not provide the power states of the camera (D0/D1/D2/D3) in fact the WMI information doesn't change one bit if the camera in question is on or off in other words: WMI doesn't contain the power state information ☹️

The only location I could find the power state information is in the device manager, as other have already figured out:

The second method seems a bit hacky and using SetupDiGetDeviceRegistryProperty() seems to be the correct way, but also involves calling some ancient WIN32 APIs. It should be possible also from HASS workstation service, some example code (not on power states, but how to work with the API) can be found here: https://theorypc.ca/2017/06/28/remove-ghost-devices-natively-with-powershell/

So no solution here, maybe just a way forward if someone wants to step into SetupDiGetDeviceRegistryProperty. It would be possible to list all webcams, and check which webcam is active looking at the power states.