Powering a Windows PC via HomeAssistant

Jamie October 18, 2019
Source
I have a PC hooked up to the TV in our living room, which is used primarily for Kodi and video games. I'm using a Logitech Harmony activity to switch the TV on, power the AV receiver, change to the PC input, etc. The only problem is that there's no way to turn on or off the PC via Harmony, so I often sit down and forget to turn it on, or go to bed and find that it has been running all night. Inconvenient and a waste of energy! Thankfully, we can use HomeAssistant to bridge the gap, but there are a few pieces to the puzzle. Turning it on This is the really easy step, and will work for any properly configured PC on the network, not just Windows. HomeAssistant provides a Wake-on-LAN integration for just this purpose. Configuring the Windows PC First, we need to enable Wake-on-LAN support on the Windows PC. This can differ from computer to computer — you may need to change a setting in both the BIOS and in Windows; in my case, I only needed to enable it in the BIOS. See this guide for instructions. Creating a switch in HomeAssistant Now let's create a simple power switch. On Windows, pull up CMD and type ipconfig /all. You'll see soomething like this: If you have multiple adapters, be sure to recognise the one you use to connect. The relevant information — the MAC address — is listed as the "Physical Address". In my case, it's B4-2E-99-48-43-5B. Copy this somewhere. Now pop open your homeassisatnt configuration.yaml file and let's make some changes: Switch out your own MAC address. The first line simply enables Wake-on-LAN support, whilst the switch definition is all we need to turn on the computer. Now, with the PC turned off, head over to HomeAssistant, click the little "Services" button under "Developer tools", and call your switch. Your computer should turn on! Turning it off Now comes the potentially tricky part. We can't turn off a computer using Wake-on-LAN, so we need to do this some other way. If all you want to do is turn off the computer, you can use the handy RPC Shutdown integration. Once you've installed and configured as per the instructions, you should just need to change your switch: I haven't tested this, however, as what I really want to do is hibernate the PC. Hibernating it And this is where things get a bit more difficult. We can't use RPC Shutdown to hibernate a PC, so our only option is to install some software on Windows that allows us to hibernate the machine remotely. There are a number of options here, but I wanted to get by without installing any third-party software on the Windows machine if possible. A friend of mine suggested I use the built-in — albeit optional — Windows OpenSSH service, because you can hibernate via the command line locally. So I gave it a shot and found that it works great. Do note that this may not be available in all versions of Windows. Installing the OpenSSH server Microsoft has a good guide on this. In a nutshell, open PowerShell as an administrator and paste the following: That's it! Now try to SSH in using your Windows username and password from another machine: If that worked, let's move on. Creating a user and generating an SSH key Since we'll be giving our HomeAssistant box full access to SSH into the Windows machine, I feel it's wise only to give it access under a different Windows user, so go ahead and create a new local user in Windows. To save yourself a GUI nightmare, you can do this quickly in PowerShell: Next, let's generate an SSH key. Since I'm running HassOS which is docker-based, I need to ensure this remains persisted, so I've chosen to put the private key in the config directory. As such, I SSH into HassOS and generate it there: Follow the prompts, saving your key where you want. Finally, sign into Windows as your new user and create a new directory called .ssh under the home folder. Now paste in the public key you just generated and name the file authorizedkeys. Back in HomeAssistant, it's time to check if we can access Windows over ssh: All working? Great, that's the hard part! Configuring HomeAssistant to hibernate the PC Now that we have SSH access, we can create a new shell command in HomeAssistant: All those flags will help avoid a variety of issues. Now let's modify the switch to call this command: Done! Logitech Harmony Integration We now have a switch to turn on and off the PC. Assuming you have a Logitech Harmony, though, you'll want to combine it with your activities. This will require a harmony activity sensor along with two automations: the sensor identifies what Harmony's current activity is, and the automations act on that sensor. The following assumes that your harmony hub is the entity remote.harmonyhub, the WoL switch is named pcpower, and the activity name is PC. Now, when switching the PC activity, the PC will turn on or hibernate appropriately. One more step towards never having to get up from the sofa!

Discussion in the ATmosphere

Loading comments...