Skip to content

bug(OpenAPI-types): incorrect deviceStatus type #316

@TizzySaurus

Description

@TizzySaurus

Analysis

The return type of SwitchBotOpenAPI.getDeviceStatus is currently set to

Promise<{
    response: deviceStatus;
    statusCode: deviceStatusRequest['statusCode'];
}>

where

export interface deviceStatus extends device {
    deviceId: string;
    deviceType: string;
    hubDeviceId: string;
    version: number;
}

There are two problems with this deviceStatus type:

1: It incorrectly extends device.
This leads to TS saying that everything under device is also under deviceStatus, which is incorrect as p.e. deviceStatus doesn't have enableCloudService.

2: The type is the 'base' type common to all devices, rather than a union of the more exact types.
This results in TS thinking that p.e. temperature can never be returned, when in fact it can be just only for certain devices (e.g. the SwitchBot Meter).

Expected Behavior

The deviceStatus type should instead be a union of the more precise device-specific status types, i.e.

type baseDeviceStatus = {
    deviceId: string;
    deviceType: string;
    hubDeviceId: string;
    version: number;
}

export type botStatus = baseDeviceStatus & {
    power: string;
    battery: number;
    mode: 'pressMode' | 'switchMode' | 'customizeMode';
}
...

export type deviceStatus = botStatus | curtainStatus | meterStatus  | ...

This will then make TS realise that p.e. temperature could be returned.

Steps To Reproduce

Use the getDeviceStatus method of the SwitchBotOpenAPI class and note the incorrect typing.

Logs

N/A

Configuration

N/A

Environment

  • OS: Linux (NixOS 25.11)
  • Software: v3.6.2 of node-switchbot
  • Node: v22.221.1
  • npm: 10.9.4 (using pnpm 10.26.1)

Additional Context

Example actual response under a status key, showing the getDeviceStatus response for a SwitchBot Meter device:
Image

Note it doesn't have the enableCloudService key that's specified in the device type and it also contains more than just what's in the current deviceStatus type.

NB: I've opened #317 as a fix.

Metadata

Metadata

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions