LAB02-Research / HASS.Agent

Windows-based client for Home Assistant. Provides notifications, quick actions, commands, sensors and more.
https://hassagent.lab02-research.org
MIT License
1.54k stars 67 forks source link

Feature: Ability to use powershell.exe (legacy) or pwsh.exe (new) for powershell commands or sensors #353

Open MiguelTVMS opened 12 months ago

MiguelTVMS commented 12 months ago

Is your feature request related to a problem? Please describe. Windows comes with the legacy PowerShell version pre-installed; every time you start it, a message telling you to update is shown. The new PowerShell executable is pwsh.exe.

The real problem here is that I struggled to understand HASS for a long time.Agent uses Powershell.exe, and the modules I have installed should be installed in Powershell 2. Some modules, unfortunately, are only compatible with Powershell 6 and up, and I have to do a workaround by running a command or custom command calling pwsh.exe. One of the issues is that there is now a custom command implementation for the sensor. Only PowerShell 2 are supported.

Describe the solution you'd like Choose which PowerShell executable to use in PowerShell commands and sensors.

Describe alternatives you've considered To run commands using pwsh.exe, I'm running something like this pwsh -Script "Switch-Desktop 0". Unfortunately, any argument I try to add to the command is added to pwsh, not the script (as expected). Because of that, I had to set 3 commands: SwitchDesktop0, SwitchDesktop1 and SwitchDesktop2. I could only use a PowerShell command called Switch-Desktop and pass the desktop index as an argument.

I was able to make it work after installing the Virtual Desktop module to all users using powershell.exe

Additional context I'm trying to connect my AqaraCube to the PC and control multiple aspects of it, like volume, desktop changes and more. Aqara Cube Page: https://www.aqara.com/en/product/cube/ Virtual Desktop Management Powershell Module: https://github.com/MScholtes/PSVirtualDesktop

MiguelTVMS commented 12 months ago

To install the new PowerShell and to have it accessible to all users and not from the current user in Windows, I recommend running the following command: winget install microsoft.powershell --scope machine and not installing it from the Microsoft store UI.

Both use the Microsoft Store as the source to get PowerPoint, but by default, the UI install with --scope user.

amadeo-alex commented 12 months ago

The sensor part is tricky but for the commands you can use the CusomExecutor - enables you to use any parser/executor you want. For example python:

  1. Configure the executor in main options: dw87kC

  2. Configure the CustomExecutor command (in this case python3 with a script): obraz

Script:

import sys
import ctypes

scriptArgs = ' '.join(sys.argv)
ctypes.windll.user32.MessageBoxW(0, scriptArgs, "Your title", 1)
  1. Call it from HA/other MQTT client (or simply press/toggle the entity if not arguments are required):

    service: mqtt.publish
    data:
    topic: homeassistant/button/AMADEO-PC/customexecutor_python/action
    payload: HA rulez!
  2. Result: obraz

It might be a good idea to at some point add a CustomExecutorSensor or something along those lines if some will want a feedback tho.

On the side note, what version of Windows are you using? If memory serves me right Win10 came with ps5 and then got updated to ps6 somewhere along the way.

MiguelTVMS commented 12 months ago

I already did what you are telling me to do. I forgot to write it on the request. But since now PowerShell can have two or more different executables to run, let the user choose what he wants, and, as a default, use the latest one.

One way to see what versions of Powershell are installed is by using Winget.