Updated HA package

Refresh-HADeviceRegistry added prompts as well as generalization using
the profile.d set variables.
Added profile.d template file for smoother setup
Added HA Addon commands
This commit is contained in:
Gal Szkolnik 2021-05-23 16:13:28 -04:00
parent b410ec839d
commit 2aa29de253
8 changed files with 146 additions and 26 deletions

View File

@ -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

View File

@ -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

View File

@ -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
}

View File

@ -1,47 +1,69 @@
Push-Location /srv/ha/current/homeassistant/.storage [CmdletBinding()]param([switch]$ApplyChanges)
Push-Location (Join-Path $(Get-HAPath) '.storage')
$Count = [ordered]@{ $Count = [ordered]@{
Devices=[PSCustomObject]([ordered]@{ Devices=[PSCustomObject]([ordered]@{
Before = -1 Before = -1
After = -1 After = -1
Removed = -1 Removed = -1
BeforeData = $null
AfterData = $null
}) })
Entities=[PSCustomObject]([ordered]@{ Entities=[PSCustomObject]([ordered]@{
Before = -1 Before = -1
After = -1 After = -1
Removed = -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) $entities = $(Get-Content ./core.entity_registry | ConvertFrom-Json -Depth 10)
$Count.Entities.BeforeData = $entities.data.entities
$Count.Entities.Before = $entities.data.entities.count $Count.Entities.Before = $entities.data.entities.count
$entities.data.entities = $entities.data.entities | $entities.data.entities = $entities.data.entities |
Where-Object platform -match 'zha' Where-Object {
# Where-Object platform -notmatch zwave | ( $_.platform -match 'zha' ) -or ($_.Original_name -in $HAPreserveEntities)
# Where-Object { -not ( }
# $_.platform -match 'switch' -and $_.entity_id -match 'light\.' -and -not $_.config_entry_id $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.Devices.After = $devices.data.devices.count
$Count.Entities.After = $entities.data.entities.count $Count.Entities.After = $entities.data.entities.count
$Count.Devices.Removed = $Count.Devices.Before - $Count.Devices.After $Count.Devices.Removed = $Count.Devices.Before - $Count.Devices.After
$Count.Entities.Removed = $Count.Entities.Before - $Count.Entities.After $Count.Entities.Removed = $Count.Entities.Before - $Count.Entities.After
$Count | Format-Table $Count
$devices | ConvertTo-Json -Depth 10 > /tmp/new.core.device_registry $devices | ConvertTo-Json -Depth 10 > /tmp/new.core.device_registry
$entities | ConvertTo-Json -Depth 10 > /tmp/new.core.entity_registry $entities | ConvertTo-Json -Depth 10 > /tmp/new.core.entity_registry
ha core stop if( $ApplyChanges ) {
sudo mv ./core.entity_registry ./core.entity_registry.old Write-Host -ForegroundColor Cyan $(
sudo mv ./core.device_registry ./core.device_registry.old "Stopping Home-Assistant Core, if a timeout occurs, you might need"+
sudo cp /tmp/new.core.entity_registry ./core.entity_registry "`nto the following command to start the core manually:"+
sudo cp /tmp/new.core.device_registry ./core.device_registry "`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 Pop-Location
if( $ApplyChanges ) {
ha core start Write-Host -ForegroundColor Cyan $(
"Starting Home-Assistant Core, if a timeout occurs, wait a bit, and check your gui"
)
ha core start
}

View File

@ -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
}

View File

@ -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
}

View File

@ -2,16 +2,10 @@
"package": { "package": {
"Condition": [ "Condition": [
{ {
"custom": null,
"System": null,
"Hostname": null,
"Username": null,
"CmdletExists": null,
"AppExeExists": [ "AppExeExists": [
"ha", "ha",
"docker" "docker"
], ]
"Logic": 0
} }
], ],
"Name": "HomeAssistant" "Name": "HomeAssistant"

View File

@ -0,0 +1,6 @@
# $HAConfigPath='/usr/share/hassio/homeassistant'
# $HAPreserveEntities = @(
# "Centralite Thermostat Downstairs",
# "Centralite Thermostat Upstairs",
# "Adam's light"
# )