Lighting API

Last Updated on : 2023-02-10 06:55:30download

Installation

yarn add @tuya/tuya-panel-api
// Supported version: 1.11.0
import { lampApi } from '@tuya/tuya-panel-api';

Scene API

const { getSceneVersionInfo, getSceneData,  updateVersion } = lampApi;

Scene methods

Method name Purpose Parameter
getSceneVersionInfo Returns the current version and all versions of the scene library. (sceneId : number )
getSceneData Returns scene resources. (libVersion: string)
updateVersion Edits an image of a smart scene. (libVersion: string)

Example

const { getSceneVersionInfo, getSceneData,  updateVersion } = lampApi;

getSceneVersionInfo().then((response: any) => {
    console.log(response);
})

getSceneData(version).then((response: ISceneData[]) => {
    console.log(response);
})

updateVersion(version).then((flag: boolean) => {
    console.log(flag);
})

Device property API

const { getAllCloudConfig, saveCloudConfig,  deleteCloudConfig, syncCloudConfig, fetchLocalConfig, fetchCloudConfig } = lampApi;

Device property methods

Method name Purpose Parameter
getAllCloudConfig Returns all device properties. None
saveCloudConfig Saves device custom properties. (code: string, data: any)
deleteCloudConfig Deletes device custom properties. (code: string)
syncCloudConfig Syncs device properties. None
fetchLocalConfig Returns device properties cached locally. None
fetchCloudConfig Returns device properties saved in the cloud. Device properties cached locally are used in priority over those in the cloud. None

Example

const { getAllCloudConfig, saveCloudConfig,  deleteCloudConfig, syncCloudConfig, fetchLocalConfig, fetchCloudConfig } = lampApi;

getAllCloudConfig().then((data: Record<string, string>) => {
  console.log(data);
});

saveCloudConfig(code: string, data: any).then((data:Promise<any>) => {
  console.log(data);
});

deleteCloudConfig(code: string).then((data:Promise<any>) => {
  console.log(data);
});

syncCloudConfig().then((data:Promise<any>) => {
  console.log(data);
});

fetchLocalConfig().then((data:Promise<any>) => {
  console.log(data);
});

fetchCloudConfig().then((data:Promise<any>) => {
  console.log(data);
});

Biorhythm API

const { isSupportRhythm, getCurrentLocation, getCityInfoByLocation, saveRhythmInfo, getRhythmTimeByLocation, getCloudCustomRhythm, getCloudCityInfo, triggerRhythmStatus } = lampApi;

Biorhythm methods

Method name Description Parameter Remarks
isSupportRhythm Specifies whether to support biorhythm. None Return value: Promise<boolean>.
getCurrentLocation Returns the longitude and latitude of the current location. The location permission must be turned on for the app. None Return value: Promise<{ longitude: number; latitude: number }>.
saveRhythmInfo Saves the biorhythm information. This feature can be enabled or disabled. (propertyList: []) For more information about the request parameters, see Example.
triggerRhythmStatus Updates the biorhythm information. ( cronExp: string; ) The cron expression. If it is not set, the biorhythm information is updated once per month.
getRhythmTimeByLocation Returns the sunrise and sunset time at the specified location. The time is converted to the time zone where the device is located before the result is returned. (lon: string, lat: string)
  • lon: the longitude of the specified location.
  • lat: the latitude of the specified location.
getCityInfoByLocation Returns the city information at the specified location. (lon: string, lat: string)
  • lon: the longitude of the specified location.
  • lat: the latitude of the specified location.
getCloudCustomRhythm Returns the custom biorhythm data that is saved by saveRhythmInfo. None For more information, see Example
getCloudCityInfo Returns the city information that is saved in the cloud by saveRhythmInfo. None For more information, see Example.

Example

const { isSupportRhythm, getCurrentLocation, getCityInfoByLocation, saveRhythmInfo, getRhythmTimeByLocation, getCloudCustomRhythm, getCloudCityInfo, triggerRhythmStatus } = lampApi;

// Specifies whether to support biorhythm.
isSupportRhythm().then((isSupport: boolean) => {
    console.log(isSupport);
})

// Returns the longitude and latitude of the current location. The location permission must be turned on for the app.
getCurrentLocation().then( { longitude: number; latitude: number } ) => {
    console.log(longitude, latitude);
})

// Returns the city information at the specified location.
const lon = ''; // Returns the longitude.
const lat = ''; // Returns the latitude.
getCityInfoByLocation(lon, lat).then(cityInfo: {
  country: string; // The country.
  province: string; // The province.
  city: string; // The city.
}) => {
    console.log(cityInfo);
})

// The values that match `code` in `propertyList` include:
// leona_rhythm_info: string; // The geographic and city information.
// leona_rhythm_dp_sunrise: string; // The sunrise time that is displayed in the format of HH:MM.
// leona_rhythm_dp_sunset: string; // The sunset time that is displayed in the format of HH:MM.
// leona_rhythm_dp_noon: string; // The noon time that is displayed in the format of HH:MM.
// leona_rhythm_dp_dawn: string; // The dawn time that is displayed in the format of HH:MM.
// leona_rhythm_dp_dusk: string; // The dusk time that is displayed in the format of HH:MM.

// For `code=leona_rhythm_info`, `value` includes two fields:
// - power: specifies whether to enable the biorhythm feature. Valid values: `0`: yes. `1`: no.
// - mode: the mode. Valid values: `0`: default. `1`: natural. `2`: custom.
const params = {
    propertyList: [{
        "value":{
            "power":0,
            "latitude": 39.90960456049752
            "longitude":116.3972282409668,
            "name":"Beijing, China",
            "mode":0
        },
        "code":"leona_rhythm_info"
    }, {
        "value":{
            "weeks":127,
            "power":1,
            "number":5,
            "rhythm":{
                "minute":29,
                "hour":18,
                "brightness":0,
                "power":1,
                "temperature":100,
                "saturation":0,
                "lightness":0,
                "hue":0
            },
            "mode":0,
            "version":0
        },
        "code":"leona_rhythm_dp_dawn"
    },
    // Other similar configurations.
    ]
}
// Updates and saves the biorhythm information.
saveRhythmInfo(propertyList).then((success: boolean) => {
    console.log(success);
})

// Creates a biorhythm schedule to trigger the biorhythm feature at a specified time.
const cronExp = ''; // The cron expression. Search the internet for more details about how to write a cron expression. This parameter is optional. If it is not set, the biorhythm information is updated once per month.
triggerRhythmStatus(cronExp).then(success: boolean) => {
    console.log(success);
})

// Returns the sunrise and sunset time at the specified location. The time is converted to the time zone where the device is located before the result is returned.
const lon = ''; // Returns the longitude.
const lat = ''; // Returns the latitude.
getRhythmTimeByLocation(lon, lat).then((rhythmTimeInfo: {
  sunrise: string; // The sunrise time that is displayed in the format of HH:MM.
  sunset: string; // The sunset time that is displayed in the format of HH:MM.
  transitAtNoon: string; // The noon time that is displayed in the format of HH:MM.
  dawn: string; // The dawn time that is displayed in the format of HH:MM.
  dusk: string; // The dusk time that is displayed in the format of HH:MM.
  date: string; // The date.
}[]) => {
    console.log(rhythmTimeInfo);
})

// Returns the custom biorhythm data that is saved by `saveRhythmInfo`.
type CustomRhythm = {
  value: {
    weeks: number;
    power: EPower;
    number: number;
    rhythm: {
      minute: number;
      hour: number;
      brightness: number;
      power: number;
      temperature: number;
      saturation: number;
      lightness: number;
      hue: number;
    };
    mode: EMode;
    version: number;
  };
  code:
 | 'leona_rhythm_dp_dawn'
 | 'leona_rhythm_dp_dusk'
 | 'leona_rhythm_dp_noon'
 | 'leona_rhythm_dp_sunrise'
 | 'leona_rhythm_dp_sunset';
};
getCloudCustomRhythm().then(customRhythmList: CustomRhythm[]) => {
    console.log(customRhythmList);
})

// Returns the city information that is saved in the cloud by `saveRhythmInfo`.
type CityLocationInfo = {
  name: string;
  latitude: string;
  longitude: string;
};
getCloudCityInfo().then(cityInfo: CityLocationInfo) => {
    console.log(cityInfo);
})