diff --git a/HomeAssistant/Get-HAPath.ps1 b/HomeAssistant/Get-HAPath.ps1 new file mode 100644 index 0000000..a1f9c93 --- /dev/null +++ b/HomeAssistant/Get-HAPath.ps1 @@ -0,0 +1,6 @@ +if( $HAConfigPath ) { return $HAConfigPath } +@( + $( + @($HAConfigPath,'/srv/ha/current/homeassistant','/usr/share/hassio/homeassistant') | Where-Object { Test-Path -Type Container $_ } | Select-Object -First 1 + ), '/srv/ha/current/homeassistant' +) | Where-Object { $_ } | Select-Object -First 1 -OutVariable HAConfigPath diff --git a/HomeAssistant/Get-HA_Addon.ps1 b/HomeAssistant/Get-HA_Addon.ps1 new file mode 100644 index 0000000..a9a43ef --- /dev/null +++ b/HomeAssistant/Get-HA_Addon.ps1 @@ -0,0 +1,56 @@ +[CmdletBinding(SupportsShouldProcess)]param( + [string[]]$AddOnSlug, + [string[]]$AddOnName, + [switch]$InspectContainer, + [switch]$NoPatternMatch, + [ValidateSet('Installed','NotInstalled','Running','Stopped')] + [string]$Status +) + +$local:addons = $(Invoke-HomeAssistantCli addons) +if( -not $addons -or $addons.result -ne 'ok' ) { throw "No Addons returned" } + +$addons = $addons.data.addons +switch( $Status ) { + 'Installed' { $addons = $addons | Where-Object Installed } + 'NotInstalled' { $addons = $addons | Where-Object -Not Installed } + 'Running' { + $addons = $addons | Where-Object Installed + $InspectContainer = $true + } +} + +if( $AddOnSlug ) { + $addons = $addons | Where-Object { + $local:s = $_.slug; + ($s -in $AddOnSlug) -or ( + -not $NoPatternMatch -and [bool]( + $AddOnSlug | Where-Object { $s -match $_ } + ) + ) + } +} +if( $InspectContainer ) { + foreach( $local:addon in $addons ) { + $local:c = $null + $local:s = 'invalid' + if( $addon.Installed ) { + $($c = docker inspect "addon_$($addon.slug)" | ConvertFrom-Json) *>&1 | out-null + if( $c ) { $s = $c.State.status } + } + $addon | + Add-Member -MemberType NoteProperty -Name Status -Value $s -PassThru | + Add-Member -MemberType NoteProperty -Name Container -Value $c + } +} + +switch( $Status ) { + 'Running' { + $addons = $addons | Where-Object Status -eq 'running' + } + 'Stopped' { + $addons = $addons | Where-Object Status -eq 'running' + } +} + +$addons diff --git a/HomeAssistant/Invoke-HomeAssistantCli.ps1 b/HomeAssistant/Invoke-HomeAssistantCli.ps1 new file mode 100644 index 0000000..4bcef6a --- /dev/null +++ b/HomeAssistant/Invoke-HomeAssistantCli.ps1 @@ -0,0 +1,20 @@ +[CmdletBinding(SupportsShouldProcess)]param( + [switch]$NoJsonParsing, + [switch]$Help, + [Parameter(Position = 0, ValueFromRemainingArguments = $true)] + [string[]]$Params +) + +$local:baseParams='--raw-json' +if( -not $Params ) { $NoJsonParsing = $true } +if( $NoJsonParsing ) { $baseParams=[string]::Empty } +if( $Help ) { + $NoJsonParsing = $true + $baseParams = '--help' +} +$local:results = & docker exec hassio_cli ha $params $baseParams +if( -not $NoJsonParsing ) { + $results | ConvertFrom-Json +} else { + $results +} diff --git a/HomeAssistant/Refresh-HADeviceRegistry.ps1 b/HomeAssistant/Refresh-HADeviceRegistry.ps1 index 2eafdbb..90340ee 100644 --- a/HomeAssistant/Refresh-HADeviceRegistry.ps1 +++ b/HomeAssistant/Refresh-HADeviceRegistry.ps1 @@ -1,47 +1,69 @@ -Push-Location /srv/ha/current/homeassistant/.storage +[CmdletBinding()]param([switch]$ApplyChanges) +Push-Location (Join-Path $(Get-HAPath) '.storage') $Count = [ordered]@{ Devices=[PSCustomObject]([ordered]@{ Before = -1 After = -1 Removed = -1 + BeforeData = $null + AfterData = $null }) Entities=[PSCustomObject]([ordered]@{ Before = -1 After = -1 Removed = -1 + BeforeData = $null + AfterData = $null }) } -$devices = $(Get-Content ./core.device_registry | ConvertFrom-Json -Depth 10) -$Count.Devices.Before = $devices.data.devices.count -$devices.data.devices = $devices.data.devices | -# Where-Object identifiers -notmatch 'zwave' - Where-Object {$_.identifiers[0] -eq 'zha'} + $entities = $(Get-Content ./core.entity_registry | ConvertFrom-Json -Depth 10) +$Count.Entities.BeforeData = $entities.data.entities $Count.Entities.Before = $entities.data.entities.count $entities.data.entities = $entities.data.entities | - Where-Object platform -match 'zha' -# Where-Object platform -notmatch zwave | -# Where-Object { -not ( -# $_.platform -match 'switch' -and $_.entity_id -match 'light\.' -and -not $_.config_entry_id -# ) } + Where-Object { + ( $_.platform -match 'zha' ) -or ($_.Original_name -in $HAPreserveEntities) + } +$Count.Entities.AfterData = $entities.data.entities + +$devices = $(Get-Content ./core.device_registry | ConvertFrom-Json -Depth 10) +$Count.Devices.BeforeData = $devices.data.devices +$Count.Devices.Before = $devices.data.devices.count +$devices.data.devices = $devices.data.devices | + Where-Object id -in $($entities.data.entities.device_id) +# Where-Object identifiers -notmatch 'zwave' +# Where-Object {$_.identifiers[0] -in 'zha','smartthings' } +$Count.Devices.AfterData = $devices.data.devices $Count.Devices.After = $devices.data.devices.count $Count.Entities.After = $entities.data.entities.count $Count.Devices.Removed = $Count.Devices.Before - $Count.Devices.After $Count.Entities.Removed = $Count.Entities.Before - $Count.Entities.After -$Count | Format-Table +$Count $devices | ConvertTo-Json -Depth 10 > /tmp/new.core.device_registry $entities | ConvertTo-Json -Depth 10 > /tmp/new.core.entity_registry -ha core stop -sudo mv ./core.entity_registry ./core.entity_registry.old -sudo mv ./core.device_registry ./core.device_registry.old -sudo cp /tmp/new.core.entity_registry ./core.entity_registry -sudo cp /tmp/new.core.device_registry ./core.device_registry +if( $ApplyChanges ) { + Write-Host -ForegroundColor Cyan $( + "Stopping Home-Assistant Core, if a timeout occurs, you might need"+ + "`nto the following command to start the core manually:"+ + "`n ha core start" + ) + ha core stop + $local:_ts = Get-Date -Format "yyyyMMdd-HHmmss" + sudo mv ./core.entity_registry ./core.entity_registry.$_ts.old + sudo mv ./core.device_registry ./core.device_registry.$_ts.old + sudo cp /tmp/new.core.entity_registry ./core.entity_registry + sudo cp /tmp/new.core.device_registry ./core.device_registry +} Pop-Location - -ha core start +if( $ApplyChanges ) { + Write-Host -ForegroundColor Cyan $( + "Starting Home-Assistant Core, if a timeout occurs, wait a bit, and check your gui" + ) + ha core start +} diff --git a/HomeAssistant/Start-HA_Addon.ps1 b/HomeAssistant/Start-HA_Addon.ps1 new file mode 100644 index 0000000..6c05c28 --- /dev/null +++ b/HomeAssistant/Start-HA_Addon.ps1 @@ -0,0 +1,8 @@ +[CmdletBinding(SupportsShouldProcess)]param( + [string[]]$AddOnSlug, + [switch]$NoPatternMatch +) + +Get-HA_Addon 'Installed' -AddOnSlug:$AddOnSlug -NoPatternMatch:$NoPatternMatch | ForEach-Object { + Invoke-HomeAssistantCli addons start $_.slug +} diff --git a/HomeAssistant/Stop-HA_Addon.ps1 b/HomeAssistant/Stop-HA_Addon.ps1 new file mode 100644 index 0000000..dc4b65d --- /dev/null +++ b/HomeAssistant/Stop-HA_Addon.ps1 @@ -0,0 +1,8 @@ +[CmdletBinding(SupportsShouldProcess)]param( + [string[]]$AddOnSlug, + [switch]$NoPatternMatch +) + +Get-HA_Addon 'Running' -AddOnSlug:$AddOnSlug -NoPatternMatch:$NoPatternMatch | ForEach-Object { + Invoke-HomeAssistantCli addons stop $_.slug +} diff --git a/HomeAssistant/_.package.json b/HomeAssistant/_.package.json index d1fc772..c36515f 100644 --- a/HomeAssistant/_.package.json +++ b/HomeAssistant/_.package.json @@ -2,16 +2,10 @@ "package": { "Condition": [ { - "custom": null, - "System": null, - "Hostname": null, - "Username": null, - "CmdletExists": null, "AppExeExists": [ "ha", "docker" - ], - "Logic": 0 + ] } ], "Name": "HomeAssistant" diff --git a/HomeAssistant/profile.d/setHAdir.local.ps1.template b/HomeAssistant/profile.d/setHAdir.local.ps1.template new file mode 100644 index 0000000..071ba31 --- /dev/null +++ b/HomeAssistant/profile.d/setHAdir.local.ps1.template @@ -0,0 +1,6 @@ +# $HAConfigPath='/usr/share/hassio/homeassistant' +# $HAPreserveEntities = @( +# "Centralite Thermostat Downstairs", +# "Centralite Thermostat Upstairs", +# "Adam's light" +# )