Skip to content

Commit 49f3234

Browse files
committed
refactor to match backend changes
1 parent b7fdd63 commit 49f3234

3 files changed

Lines changed: 13 additions & 52 deletions

File tree

src/data/zwave_js-credentials.ts

Lines changed: 4 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -120,24 +120,14 @@ export interface SetZwaveUserParams {
120120
user_type?: string;
121121
credential_rule?: string;
122122
active?: boolean;
123+
credential_type?: ZwaveCredentialType;
124+
credential_slot?: number;
125+
credential_data?: string;
123126
}
124127

125128
export interface SetZwaveUserResult {
126129
user_id: number;
127-
}
128-
129-
export interface AddZwaveUserParams {
130-
user_name?: string | null;
131-
user_type?: string;
132-
credential_rule?: string;
133-
active?: boolean;
134-
credential_type: ZwaveCredentialType;
135-
credential_data: string;
136-
}
137-
138-
export interface AddZwaveUserResult {
139-
user_id: number;
140-
credential_slot: number;
130+
credential_slot?: number | null;
141131
}
142132

143133
export interface SetZwaveCredentialParams {
@@ -230,23 +220,6 @@ export const setZwaveUser = async (
230220
return unwrapEntityResponse(hass, result.response, entity_id);
231221
};
232222

233-
export const addZwaveUser = async (
234-
hass: HomeAssistant,
235-
entity_id: string,
236-
params: AddZwaveUserParams
237-
): Promise<AddZwaveUserResult> => {
238-
// notifyOnError=false — caller surfaces errors in-dialog instead.
239-
const result = await hass.callService<Record<string, AddZwaveUserResult>>(
240-
"zwave_js",
241-
"add_user",
242-
params,
243-
{ entity_id },
244-
false,
245-
true
246-
);
247-
return unwrapEntityResponse(hass, result.response, entity_id);
248-
};
249-
250223
export const deleteZwaveUser = (
251224
hass: HomeAssistant,
252225
entity_id: string,

src/panels/config/integrations/integration-panels/zwave_js/dialog-zwave_js-credential-user-edit.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import {
1818
DEFAULT_CREDENTIAL_MAX_LENGTH,
1919
DEFAULT_CREDENTIAL_MIN_LENGTH,
2020
ENTERABLE_ZWAVE_CREDENTIAL_TYPES,
21-
addZwaveUser,
2221
deleteZwaveCredential,
2322
enterableCredentialTypes,
2423
getCredentialError,
@@ -508,12 +507,9 @@ class DialogZwaveCredentialUserEdit extends LitElement {
508507
credentialType: ZwaveCredentialType
509508
): Promise<void> {
510509
const params = this._params!;
511-
// Create the user and write the credential in a single call. This is the
512-
// only way to add a user on User Code CC locks, where the user and code
513-
// share a slot. If the credential write fails, the service rolls the user
514-
// back so the lock is never left with a credential-less user.
515510
try {
516-
await addZwaveUser(this.hass, params.entity_id, {
511+
await setZwaveUser(this.hass, params.entity_id, {
512+
// Omit user_id to create a new user with the given credential.
517513
user_name: this._supportsUserNames ? this._userName.trim() : undefined,
518514
user_type: this._userType,
519515
active: true,

test/data/zwave_js-credentials.test.ts

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import {
33
getZwaveCredentialCapabilities,
44
getZwaveUsers,
55
setZwaveUser,
6-
addZwaveUser,
76
deleteZwaveUser,
87
deleteZwaveAllUsers,
98
setZwaveCredential,
@@ -170,20 +169,13 @@ describe("zwave_js-credentials", () => {
170169
);
171170
expect(result.user_id).toBe(1);
172171
});
173-
});
174172

175-
describe("addZwaveUser", () => {
176-
const addUserResponse = (inner: unknown) =>
177-
({
178-
callService: vi
179-
.fn()
180-
.mockResolvedValue({ response: { [ENTITY_ID]: inner } }),
181-
}) as unknown as HomeAssistant;
182-
183-
it("calls add_user with user and credential, returning the slot", async () => {
184-
const hass = addUserResponse({ user_id: 1, credential_slot: 1 });
173+
it("creates a new user with a credential and returns the slot", async () => {
174+
// Omitting user_id makes set_user create a new user and write the
175+
// credential in one call, returning the allocated credential_slot.
176+
const hass = setUserResponse({ user_id: 1, credential_slot: 1 });
185177

186-
const result = await addZwaveUser(hass, ENTITY_ID, {
178+
const result = await setZwaveUser(hass, ENTITY_ID, {
187179
user_name: "Alice",
188180
user_type: "general",
189181
active: true,
@@ -193,7 +185,7 @@ describe("zwave_js-credentials", () => {
193185

194186
expect(hass.callService).toHaveBeenCalledWith(
195187
"zwave_js",
196-
"add_user",
188+
"set_user",
197189
{
198190
user_name: "Alice",
199191
user_type: "general",
@@ -214,7 +206,7 @@ describe("zwave_js-credentials", () => {
214206
} as unknown as HomeAssistant;
215207

216208
await expect(
217-
addZwaveUser(hass, ENTITY_ID, {
209+
setZwaveUser(hass, ENTITY_ID, {
218210
credential_type: "pin_code",
219211
credential_data: "1234",
220212
})

0 commit comments

Comments
 (0)