sleevezipper / hass-workstation-service

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

Different string matching rules for NamedWindowSensor #41

Open aFrankLion opened 3 years ago

aFrankLion commented 3 years ago

It would be helpful to have the option to specify exact string matching for the NamedWindowSensor. A checkbox in the user interface to toggle between the current string matching logic and a narrower match should suffice.

My motivation for this change is as follows. I've been using NamedWindowSensors to activate scenes in HASS based on what programs are open on my computer. This works great for lighting schemes for particular games or applications. I'm also able to automatically turn fans on when running workout software. However, these scenes are being inadvertently triggered if I use an internet browser to read a page which has a related term in the title, e.g., Steam, VR, Zwift.

In terms of implementation, I'm not familiar enough with .NET to make a proper fix, but I hacked something together which seems to work. Within NamedWindowSensor.cs, changing the GetState() function as below achieves the basic functionality of enforcing exact string matches:

        public override string GetState()
        {
            var windowNames = GetOpenWindows().Values;
            //return windowNames.Any(v => v.Contains(this.WindowName, StringComparison.OrdinalIgnoreCase)) ? "True" : "False";
            return windowNames.Any(v => v.Equals(this.WindowName)) ? "True" : "False";
        }

With this change, a NamedWindowSensor can distinguish windows named "Zwift" and "Zwift keyboard shortcuts - Google Search" and your ceiling fan won't turn on high unexpectedly.

sleevezipper commented 3 years ago

Hi aFrankLion,

Thanks for creating an issue! I have thought about this as well and your use case perfectly describes the need for it. I'll let you know when I get to it.