2021-05-23 20:13:28 +00:00
|
|
|
[CmdletBinding()]param([switch]$ApplyChanges)
|
|
|
|
Push-Location (Join-Path $(Get-HAPath) '.storage')
|
2021-05-23 16:47:20 +00:00
|
|
|
|
|
|
|
$Count = [ordered]@{
|
|
|
|
Devices=[PSCustomObject]([ordered]@{
|
|
|
|
Before = -1
|
|
|
|
After = -1
|
|
|
|
Removed = -1
|
2021-05-23 20:13:28 +00:00
|
|
|
BeforeData = $null
|
|
|
|
AfterData = $null
|
2021-05-23 16:47:20 +00:00
|
|
|
})
|
|
|
|
Entities=[PSCustomObject]([ordered]@{
|
|
|
|
Before = -1
|
|
|
|
After = -1
|
|
|
|
Removed = -1
|
2021-05-23 20:13:28 +00:00
|
|
|
BeforeData = $null
|
|
|
|
AfterData = $null
|
2021-05-23 16:47:20 +00:00
|
|
|
})
|
|
|
|
}
|
2021-05-23 20:13:28 +00:00
|
|
|
|
|
|
|
$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' ) -or ($_.Original_name -in $HAPreserveEntities)
|
|
|
|
}
|
|
|
|
$Count.Entities.AfterData = $entities.data.entities
|
|
|
|
|
2021-05-23 16:47:20 +00:00
|
|
|
$devices = $(Get-Content ./core.device_registry | ConvertFrom-Json -Depth 10)
|
2021-05-23 20:13:28 +00:00
|
|
|
$Count.Devices.BeforeData = $devices.data.devices
|
2021-05-23 16:47:20 +00:00
|
|
|
$Count.Devices.Before = $devices.data.devices.count
|
|
|
|
$devices.data.devices = $devices.data.devices |
|
2021-05-23 20:13:28 +00:00
|
|
|
Where-Object id -in $($entities.data.entities.device_id)
|
2021-05-23 16:47:20 +00:00
|
|
|
# Where-Object identifiers -notmatch 'zwave'
|
2021-05-23 20:13:28 +00:00
|
|
|
# Where-Object {$_.identifiers[0] -in 'zha','smartthings' }
|
|
|
|
$Count.Devices.AfterData = $devices.data.devices
|
2021-05-23 16:47:20 +00:00
|
|
|
|
|
|
|
$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
|
|
|
|
|
2021-05-23 20:13:28 +00:00
|
|
|
$Count
|
2021-05-23 16:47:20 +00:00
|
|
|
|
|
|
|
$devices | ConvertTo-Json -Depth 10 > /tmp/new.core.device_registry
|
|
|
|
$entities | ConvertTo-Json -Depth 10 > /tmp/new.core.entity_registry
|
|
|
|
|
2021-05-23 20:13:28 +00:00
|
|
|
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
|
|
|
|
}
|
2021-05-23 16:47:20 +00:00
|
|
|
Pop-Location
|
|
|
|
|
2021-05-23 20:13:28 +00:00
|
|
|
if( $ApplyChanges ) {
|
|
|
|
Write-Host -ForegroundColor Cyan $(
|
|
|
|
"Starting Home-Assistant Core, if a timeout occurs, wait a bit, and check your gui"
|
|
|
|
)
|
|
|
|
ha core start
|
|
|
|
}
|