├── Home-Assistant ├── Home-Assistant.psd1 └── Home-Assistant.psm1 ├── LICENSE └── README.md /Home-Assistant/Home-Assistant.psd1: -------------------------------------------------------------------------------- 1 | # 2 | # Module manifest for module 'Home-Assistant' 3 | # 4 | # Generated by: Flemming Sørvollen Skaret 5 | # 6 | # Generated on: 11.06.2019 7 | # 8 | 9 | @{ 10 | 11 | # Script module or binary module file associated with this manifest. 12 | RootModule = 'Home-Assistant.psm1' 13 | 14 | # ModuleVersion ignore this, but this is required 15 | ModuleVersion = '0.0.0' 16 | 17 | # ID used to uniquely identify this module 18 | GUID = '9bfaf5c0-e44e-4715-b7f3-1cbc68713f6e' 19 | 20 | # Author of this module 21 | Author = 'Flemming Sørvollen Skaret' 22 | 23 | # Copyright statement for this module 24 | Copyright = '(c) 2019 Flemming Sørvollen Skaret.' 25 | 26 | # Description of the functionality provided by this module 27 | Description = 'A PowerShell Module to Control the Home-Assistant home automation software' 28 | 29 | # HelpInfo URI of this module 30 | HelpInfoURI = 'https://github.com/flemmingss/Home-Assistant-PowerShell-Module' 31 | 32 | } 33 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Flemming Sørvollen Skaret 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Home-Assistant PowerShell Module 2 | A PowerShell Module to Control the Home-Assistant home automation software 3 | 4 | 5 | **Description** 6 | This is a project I started with in late 2018 and it hasn't been developed since. But I will publish it anyway and hopefully implement more features in the future 7 | 8 | **Instructions** 9 | 10 | Load module to import all functions: 11 | ```powershell 12 | Import-Module .\Home-Assistant 13 | ``` 14 | New-HomeAssistantSession neds to be used before any other function will work. 15 | 16 | **Functions** 17 | 18 | ```powershell 19 | Get-HomeAssistantConfig 20 | Get-HomeAssistantEntity 21 | Get-HomeAssistantEntity -entity_id 22 | Get-HomeAssistantServices 23 | Invoke-HomeAssistantService -service -entity_id 24 | Invoke-HomeAssistantService -service -json '' 25 | New-HomeAssistantSession -ip -port -token 26 | ``` 27 | **Examples** 28 | 29 | ```powershell 30 | 31 | New-HomeAssistantSession -ip 10.0.24.4 -port 8123 -token eyYfo49g036gdKg5LSki4w04tkifulaglgkMn3idnf2w57x5eyfog036gdKg5LSkgw04tkifulaglgkMidnf2w57axyYfo49g036gdKLSkgi4w04tkifaglgkMn3idnf2w57axyYfo49g036gdKg5LSkgi4w04tkifulaglgkMn3idnf2w57ax5 32 | Get-HomeAssistantEntity -entity_id light.limitlessled_g1 33 | Invoke-HomeAssistantService -service light.turn_on -entity_id light.limitlessled_g1 34 | Invoke-HomeAssistantService -service light.turn_on -json '{"entity_id":"light.limitlessled_g1","color_name":"blue"}' 35 | ``` 36 | 37 | **Examples - Advanced/Script/JSON** 38 | 39 | Use with custom component "hass-variables" https://github.com/rogro82/hass-variables 40 | 41 | ```powershell 42 | 43 | $cpu_usage_pct = "25" 44 | $json_to_send = '{"variable":"computer_cpu_usage","value":' + '"' + "$cpu_usage_pct" + '"' + '}' 45 | Invoke-HomeAssistantService -service variable.set_variable -json $json_to_send 46 | 47 | ``` 48 | 49 | Use with custom component "home-assistant-variables" https://github.com/snarky-snark/home-assistant-variables 50 | 51 | ```powershell 52 | 53 | $cpu_usage_pct = "25" 54 | $json_to_send = '{"entity_id":"var.computer_cpu_usage","value":' + '"' + "$cpu_usage_pct" + '"' + '}' 55 | Invoke-HomeAssistantService -service var.set -json $json_to_send 56 | 57 | ``` 58 | 59 | **Changelog** 60 | * 17.03.2019 61 | * Release 62 | * 11.06.2019 63 | * Updated to better follow PowerShell's best practice for modules. Created folder /Home-Assistant/ and Merged all .ps1 functions to one module file "Home-Assistant.psm1" and Added Module Manifest file "Home-Assistant.psd1" 64 | * 05.11.2019 65 | * Added ModuleVersion to module manifest because it is required 66 | -------------------------------------------------------------------------------- /Home-Assistant/Home-Assistant.psm1: -------------------------------------------------------------------------------- 1 | <# 2 | .SYNOPSIS 3 | Name: New-HomeAssistantSession.ps1 4 | This is a function in the a PowerShell Module to Control the Home-Assistant home automation software. 5 | .DESCRIPTION 6 | This function is used to connect to the Home-Assistent API 7 | .NOTES 8 | Original release Date: 17.03.2019 9 | Author: Flemming Sørvollen Skaret (https://github.com/flemmingss/) 10 | .LINK 11 | https://github.com/flemmingss/ 12 | .EXAMPLE 13 | New-HomeAssistantSession -ip -port -token 14 | #> 15 | 16 | Function New-HomeAssistantSession 17 | { 18 | Param( 19 | [Parameter(Mandatory=$true)][string]$ip, 20 | [Parameter(Mandatory=$true)][string]$port, 21 | [Parameter(Mandatory=$true)][string]$token 22 | ) 23 | 24 | $global:ha_api_headers = @{Authorization = "Bearer "+$token} 25 | $global:ha_api_url = "http://"+"$ip"+":"+"$port"+"/api/" 26 | 27 | try 28 | { 29 | Write-Host "Testing connection... " -ForegroundColor Yellow -NoNewline 30 | $api_connection = (Invoke-WebRequest -uri $ha_api_url -Method GET -Headers $ha_api_headers) 31 | $global:ha_api_configured = $true 32 | Write-Host "Connection to Home-Assistant API succeeded! (" ($api_connection).StatusCode ($api_connection).StatusDescription ")" -ForegroundColor Green 33 | } 34 | catch 35 | { 36 | if ((Test-NetConnection -ComputerName $ip -WarningAction SilentlyContinue).PingSucceeded) 37 | { 38 | Write-Host "Connection to Home-Assistant API failed!" -ForegroundColor Red 39 | } 40 | else 41 | { 42 | Write-Host "Connection failed - ICMP request timed out!" -ForegroundColor Red 43 | } 44 | 45 | $global:ha_api_url = $null 46 | $global:ha_api_headers = $null 47 | $global:ha_api_configured = $false 48 | } #catch end 49 | 50 | } #End of function New-HomeAssistantSession 51 | 52 | <# 53 | .SYNOPSIS 54 | Name: Invoke-HomeAssistantService.ps1 55 | This is a function in the a PowerShell Module to Control the Home-Assistant home automation software. 56 | .DESCRIPTION 57 | This function is used to invoke a service in the Home-Assistent 58 | .NOTES 59 | Original release Date: 17.03.2019 60 | Author: Flemming Sørvollen Skaret (https://github.com/flemmingss/) 61 | .LINK 62 | https://github.com/flemmingss/ 63 | .EXAMPLE 64 | Invoke-HomeAssistantService -service -entity_id 65 | Invoke-HomeAssistantService -service -json '' 66 | #> 67 | 68 | Function Invoke-HomeAssistantService 69 | { 70 | [CmdletBinding()] 71 | Param( 72 | [Parameter(Mandatory=$true,Position="0",ParameterSetName="ByEntity")][string] 73 | [Parameter(Mandatory=$true,Position="0",ParameterSetName="ByJSON")][string] 74 | $service, 75 | 76 | [Parameter(Mandatory=$true,Position="1",ParameterSetName="ByEntity")][string] 77 | $entity_id, 78 | 79 | [Parameter(Mandatory=$true,Position="1",ParameterSetName="ByJSON")][string] 80 | $json 81 | ) 82 | 83 | #-SERVICE 84 | $service = $service.replace(".","/") 85 | 86 | #-ENTITY_ID 87 | 88 | if ($entity_id) 89 | { 90 | $body = ConvertTo-Json @{"entity_id" = "$entity_id"} 91 | } 92 | 93 | #-JSON 94 | elseif ($json) 95 | { 96 | $body = $json 97 | } 98 | 99 | Invoke-RestMethod -Method Post -Uri ("$ha_api_url"+"services/"+"$service") -Body ($body) -Header $ha_api_headers 100 | Write-Host $body 101 | } #End of function Invoke-HomeAssistantService 102 | 103 | <# 104 | .SYNOPSIS 105 | Name: Get-HomeAssistantConfig.ps1 106 | This is a function in the a PowerShell Module to Control the Home-Assistant home automation software. 107 | .DESCRIPTION 108 | This function is used to get information abaout the Home-Assistant configuration 109 | .NOTES 110 | Original release Date: 17.03.2019 111 | Author: Flemming Sørvollen Skaret (https://github.com/flemmingss/) 112 | .LINK 113 | https://github.com/flemmingss/ 114 | .EXAMPLE 115 | Get-HomeAssistantConfig 116 | #> 117 | 118 | function Get-HomeAssistantConfig 119 | { 120 | Invoke-RestMethod -Method get -Uri ("$ha_api_url"+"config") -Header $ha_api_headers 121 | } #End of function Get-HomeAssistantConfig 122 | 123 | <# 124 | .SYNOPSIS 125 | Name: Get-HomeAssistantEntity.ps1 126 | This is a function in the a PowerShell Module to Control the Home-Assistant home automation software. 127 | .DESCRIPTION 128 | This function is used to get information abaout a entity in Home-Assistant 129 | .NOTES 130 | Original release Date: 17.03.2019 131 | Author: Flemming Sørvollen Skaret (https://github.com/flemmingss/) 132 | .LINK 133 | https://github.com/flemmingss/ 134 | .EXAMPLE 135 | Get-HomeAssistantEntity 136 | Get-HomeAssistantEntity -entity_id 137 | #> 138 | 139 | function Get-HomeAssistantEntity 140 | { 141 | [CmdletBinding()] 142 | Param( 143 | [Parameter(Mandatory=$false,Position="0") 144 | ][string]$entity_id 145 | ) 146 | 147 | if ($entity_id) 148 | { 149 | Invoke-RestMethod -Method get -Uri ("$ha_api_url"+"states/"+$entity_id) -Header $ha_api_headers 150 | } 151 | else 152 | { 153 | Invoke-RestMethod -Method get -Uri ("$ha_api_url"+"states") -Header $ha_api_headers 154 | } 155 | 156 | } #End of function Get-HomeAssistantEntity 157 | 158 | <# 159 | .SYNOPSIS 160 | Name: Get-HomeAssistantServices.ps1 161 | This is a function in the a PowerShell Module to Control the Home-Assistant home automation software. 162 | .DESCRIPTION 163 | This function is used to get all the available services in Home-Assistant 164 | .NOTES 165 | Original release Date: 17.03.2019 166 | Author: Flemming Sørvollen Skaret (https://github.com/flemmingss/) 167 | .LINK 168 | https://github.com/flemmingss/ 169 | .EXAMPLE 170 | Get-HomeAssistantServices 171 | #> 172 | 173 | Function Get-HomeAssistantServices 174 | { 175 | Invoke-RestMethod -Method get -Uri ("$ha_api_url"+"services") -Header $ha_api_headers 176 | } #End of function Get-HomeAssistantServices --------------------------------------------------------------------------------