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
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Changelog

## [Unreleased]

### Fixed
- Smart Home: `GET /rules/gateway-count` no longer miscounts GateControl's own secondary rules (`#reset`/`#cancel` chains) as external — GC-owned rules are now counted by the `GC:` name prefix over the live gateway response instead of the DB `deconz_rule_id` column

---

## [1.113.0] — 2026-07-01

### Features
Expand Down
8 changes: 6 additions & 2 deletions src/services/smarthome/smarthomeRules.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,12 @@ async function setEnabled(id, on) {
}

async function gatewayRuleCount(gatewayId) {
const total = Object.keys(await clientFactory(gatewayId).getRules() || {}).length;
const gc = getDb().prepare('SELECT COUNT(*) c FROM smarthome_rules WHERE gateway_id = ? AND deconz_rule_id IS NOT NULL').get(gatewayId).c;
const rules = await clientFactory(gatewayId).getRules() || {};
const total = Object.keys(rules).length;
// GC-owned rules carry the "GC:" name prefix (primary + #reset/#cancel secondary rules).
// Counting by prefix over the live response — not the DB deconz_rule_id column, which only holds
// the primary rule id — so cancel/reset chains are attributed to gc, not miscounted as external.
const gc = Object.values(rules).filter((r) => r && typeof r.name === 'string' && r.name.startsWith('GC:')).length;
return { total_rules: total, gc_rules: gc, external_rules: Math.max(0, total - gc) };
}

Expand Down
17 changes: 17 additions & 0 deletions tests/smarthome_rules_service.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,20 @@ test('DECONZ_RULE_LIMIT_REACHED mapped from 503/507', async () => {
const def = { triggers: [{ kind: 'motion', resourceId: motion, event: 'detected' }], actions: [{ kind: 'group', resourceId: group, set: { on: true } }] };
await assert.rejects(() => rules.create(gw.id, 'L', def), (e) => e.code === 'DECONZ_RULE_LIMIT_REACHED');
});

test('gatewayRuleCount attributes GC-named rules (incl #reset/#cancel) to gc, not external', async () => {
rules._setClientFactoryForTest(() => ({
getRules: async () => ({
'1': { name: 'GC:5:Flur' }, // primary GC rule
'2': { name: 'GC:5:Flur#reset' }, // secondary GC rule (reset chain)
'3': { name: 'GC:8:Bad#cancel' }, // secondary GC rule (cancel chain)
'4': { name: 'pir-fsm-reset' }, // external (Phoscon-created)
'5': { name: 'my hue rule' }, // external
}),
}));
const { gw } = mkGatewayAndMotionGroup();
const c = await rules.gatewayRuleCount(gw.id);
assert.equal(c.total_rules, 5);
assert.equal(c.gc_rules, 3); // GC: prefix, incl #reset/#cancel — not the DB deconz_rule_id column
assert.equal(c.external_rules, 2); // only the two genuinely external rules
});
Loading