Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions homeassistant-addon/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ If this add-on saves you time, you can [buy me a coffee](https://buymeacoffee.co

Releases before 1.22.0 are in the [changelog archive](https://github.com/dougrathbone/cgateweb/blob/master/homeassistant-addon/CHANGELOG-archive.md).

## [1.40.3](https://github.com/dougrathbone/cgateweb/releases/tag/v1.40.3) - 2026-11-14

### Fixed

- **A project saved by an older C-Gate now loads after upgrading managed C-Gate to 3.8.0.** The add-on migrates the database when C-Gate refuses it, and keeps a copy beside the original. ([#174](https://github.com/dougrathbone/cgateweb/issues/174))

## [1.40.2](https://github.com/dougrathbone/cgateweb/releases/tag/v1.40.2) - 2026-11-14

### Fixed
Expand Down
2 changes: 2 additions & 0 deletions homeassistant-addon/DOCS.md
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,8 @@ If it persists on a current version, the project genuinely has no synchronised u

**If the network stays Closed after you click Open Network:** current C-Bus Toolkit needs C-Gate 3.8.0. The add-on's built-in download is 3.3.2, which answers `OriginateInProject` with 402 and Toolkit leaves the network Closed even though the interface is running and Home Assistant can control it. Download C-Gate 3.8.0 from Clipsal, place the zip in `/share/cgate/`, and restart. If C-Gate install source is left unset, a zip in that folder is what gets installed. Set it to upload if you previously chose download. Turn on Force C-Gate Reinstall for one restart if C-Gate is already installed, then turn it off again (#122).

**If C-Gate 3.8.0 is running but the project never loads:** a project last saved by an older C-Gate has to be migrated before 3.8.0 will open it, and until then every command fails with "network not found". From 1.40.3 the add-on runs that migration when C-Gate refuses the database, and keeps a copy of it next to the original (`HOME.db.before-schema-migrate` for a project named HOME). Restart the add-on once after upgrading (#174).

> **Remember what mapping this port means.** C-Gate has no authentication beyond the address list, and `program` sits above `admin` in its access levels, so it also permits shutting C-Gate down. Map to a single specific address, never a subnet, and never expose it to the internet.

## Finding Your C-Bus Network ID
Expand Down
2 changes: 1 addition & 1 deletion homeassistant-addon/config.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: "C-Gate Web Bridge"
version: "1.40.2"
version: "1.40.3"
slug: cgateweb
description: "Bridge between Clipsal C-Bus systems and MQTT/Home Assistant"
url: "https://github.com/dougrathbone/cgateweb"
Expand Down
6 changes: 6 additions & 0 deletions homeassistant-addon/rootfs/etc/services.d/cgateweb/run
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ if [[ "${CGATE_MODE}" == "managed" ]]; then
done
bashio::log.info "C-Gate is ready"

# Issue #174: C-Gate 3.8.0 will not load a project database written by
# 3.3.x until TRANSFORM MIGRATE_SQL runs. Do that before the bridge
# starts, so it does not spend the boot sending commands at a project
# that was never loaded. The script exits 0 unless it is interrupted.
cgateweb-migrate-project || true

# ALPHA (issue #28): when the opt-in cgate_serial_device is configured,
# dump what C-Gate sees (PORT LIST / IFLIST) into the log. Backgrounded:
# the nc query can block up to its -w timeout and diagnostics must not
Expand Down
150 changes: 150 additions & 0 deletions homeassistant-addon/rootfs/usr/bin/cgateweb-migrate-project
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
#!/usr/bin/with-contenv bashio
# ==============================================================================
# Migrate a managed C-Gate project database when this C-Gate cannot load it.
#
# C-Gate 3.8.0 rejects a project last written by 3.3.x with:
# 408 ... SchemaVersion is 13 and must be 14 - perform a TRANSFORM MIGRATE_SQL
# and then every bridge command fails because the project never starts (#174).
# The command that fixes it is `transform migrate_sql <PROJECT>`, followed by
# project load, use and start. This script does that, and only that.
#
# It must never break add-on startup. A query failure, a project that is
# already started, or a load error that is not the schema mismatch all exit 0.
# ==============================================================================
set -euo pipefail

CGATE_MODE=$(bashio::config 'cgate_mode' 'remote')
if [[ "${CGATE_MODE}" != "managed" ]]; then
exit 0
fi

PROJECT=$(bashio::config 'cgate_project' 'HOME')
if [[ "${PROJECT}" == "null" || -z "${PROJECT}" ]]; then
PROJECT="HOME"
fi
# Same rule as cgate-install.sh. The name is sent as a C-Gate command argument
# and used as a directory name, so anything else is refused rather than escaped.
if [[ ! "${PROJECT}" =~ ^[A-Za-z0-9_]{1,32}$ ]]; then
bashio::log.warning "cgate_project is not a usable project name; skipping schema migration"
exit 0
fi

if [[ -n "${CGATEWEB_CGATE_PORT:-}" ]]; then
CGATE_PORT="${CGATEWEB_CGATE_PORT}"
else
CGATE_PORT=$(bashio::config 'cgate_port' '20023')
fi
CGATE_HOST="${CGATEWEB_CGATE_HOST:-127.0.0.1}"
CGATE_DIR="${CGATE_DIR:-/data/cgate}"
POLLS="${CGATEWEB_MIGRATE_POLLS:-4}"
POLL_INTERVAL="${CGATEWEB_MIGRATE_POLL_INTERVAL:-2}"

# One C-Gate command, response body only (the 201 greeting is discarded).
# Tests set CGATEWEB_CGATE_QUERY to a program that reads the command on stdin.
# Production uses bash /dev/tcp, which the integration probe already relies on.
_cgate_query() {
local cmd="$1"
if [[ -n "${CGATEWEB_CGATE_QUERY:-}" ]]; then
printf '%s\n' "${cmd}" | "${CGATEWEB_CGATE_QUERY}" 2>&1 || true
return 0
fi
# shellcheck disable=SC2016
timeout 8 bash -c '
host="$1"; port="$2"; cmd="$3"
exec 3<>"/dev/tcp/${host}/${port}" || exit 1
IFS= read -r -t 3 -u 3 _greeting || true
printf "%s\r\n" "${cmd}" >&3
while IFS= read -r -t 5 -u 3 line; do
printf "%s\n" "${line}"
[[ "${line}" =~ ^[0-9]{3}[[:space:]] ]] && break
done
' bash "${CGATE_HOST}" "${CGATE_PORT}" "${cmd}" 2>&1 || true
}

# project list lines look like "project=HOME state=started" (integration test
# asserts that form). Anything else for this project (loading, loaded) is left
# alone: project load would change a project that is already coming up.
_project_state() {
local out="$1"
if [[ "${out}" == *"project=${PROJECT} state=started"* ]]; then
printf 'started'
elif [[ "${out}" == *"project=${PROJECT} state=stopped"* || "${out}" == *"project=${PROJECT} state=failed"* ]]; then
printf 'stopped'
elif [[ "${out}" == *"project=${PROJECT} state="* ]]; then
printf 'other'
else
printf 'missing'
fi
}

_is_schema_mismatch() {
[[ "$1" == *SchemaVersion* && "$1" == *MIGRATE_SQL* ]]
}

_response_ok() {
[[ "$1" =~ (^|[^0-9])2[0-9]{2}[[:space:]] ]]
}

list_out=""
state="missing"
poll=0
while [[ "${poll}" -lt "${POLLS}" ]]; do
poll=$((poll + 1))
list_out=$(_cgate_query "project list")
list_out="${list_out//$'\r'/}"
state=$(_project_state "${list_out}")
[[ "${state}" == "started" ]] && exit 0
[[ "${state}" == "stopped" ]] && break
[[ "${poll}" -lt "${POLLS}" ]] && sleep "${POLL_INTERVAL}"
done

if [[ -z "${list_out//[[:space:]]/}" ]]; then
bashio::log.warning "Could not query C-Gate project list; skipping schema migration"
exit 0
fi

# A project that is loading or already loaded is not this bug. Only a stopped
# project, or one missing after the wait, is asked to load — that is when
# 3.8.0 returns the schema error instead of starting the project.
if [[ "${state}" != "stopped" && "${state}" != "missing" ]]; then
exit 0
fi

load_out=$(_cgate_query "project load ${PROJECT}")
load_out="${load_out//$'\r'/}"
if ! _is_schema_mismatch "${load_out}"; then
exit 0
fi

db="${CGATE_DIR}/Projects/${PROJECT}/${PROJECT}.db"
backup="${db}.before-schema-migrate"
if [[ ! -f "${db}" ]]; then
bashio::log.warning "C-Gate cannot load project ${PROJECT} (database schema is too old) but ${db} is not there, so it was not migrated"
exit 0
fi
if [[ ! -f "${backup}" ]]; then
if ! cp -p "${db}" "${backup}"; then
bashio::log.warning "Could not copy ${db} before migrating it; leaving the project database unchanged"
exit 0
fi
bashio::log.info "Saved a copy of project ${PROJECT} as ${backup}"
fi

bashio::log.warning "C-Gate refused to load project ${PROJECT} because its database schema is older than this C-Gate. Migrating it."
migrate_out=$(_cgate_query "transform migrate_sql ${PROJECT}")
migrate_out="${migrate_out//$'\r'/}"
if ! _response_ok "${migrate_out}"; then
bashio::log.warning "Schema migration for project ${PROJECT} failed. The copy from before the attempt is ${backup}"
exit 0
fi

_cgate_query "project load ${PROJECT}" >/dev/null
_cgate_query "project use ${PROJECT}" >/dev/null
start_out=$(_cgate_query "project start ${PROJECT}")
start_out="${start_out//$'\r'/}"
if _response_ok "${start_out}"; then
bashio::log.info "Project ${PROJECT} was migrated and started."
else
bashio::log.warning "Project ${PROJECT} was migrated but did not start. The copy from before the migration is ${backup}"
fi
exit 0
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cgateweb",
"version": "1.40.2",
"version": "1.40.3",
"description": "Node.js bridge connecting Clipsal C-Bus automation systems to MQTT for Home Assistant integration",
"keywords": [
"cbus",
Expand Down
Loading
Loading