import type Plug from '.' ; import type { SendOptions } from '../client' ; import { createRule , hasRuleListWithRuleIds , ScheduleRuleInputTime , } from '../shared/schedule' ; import type { HasRuleListWithRuleIds } from '../shared/schedule' ; import { extractResponse , HasErrCode , hasErrCode } from '../utils' ; export type AwayRule = { name ? : string ; enable ? : number ; frequency ? : number ; delay ? : number ; } ; export interface AwayRuleInput { /**
     * Date or number of minutes
     */ start : ScheduleRuleInputTime ; /**
     * Date or number of minutes (only time component of date is used)
     */ end : ScheduleRuleInputTime ; /**
     * [0,6] = weekend, [1,2,3,4,5] = weekdays
     */ daysOfWeek : number [ ] ; defaultValue frequency : number ; name ? : string ; defaultValue enable : boolean | 0 | 1 ; } export default class Away { constructor ( readonly device , readonly apiModuleName , readonly childId = undefined ) { } throws ResponseError async getRules ( sendOptions ? ) { return extractResponse < HasRuleListWithRuleIds & HasErrCode > ( await this . device . sendCommand ( { [ this . apiModuleName ] : { get_rules : { } } , } , this . childId , sendOptions ) , '' , ( c ) => hasRuleListWithRuleIds ( c ) && hasErrCode ( c ) ) ; } throws ResponseError async getRule ( id , sendOptions ? ) { const rules = await this . getRules ( sendOptions ) ; const rule = rules . rule_list . find ( ( r ) => r . id === id ) ; if ( rule === undefined ) throw new Error ( `Rule id not found: ${ id }` ) ; return { ... rule , err_code : rules . err_code } ; } returns throws ResponseError async addRule ( rule , sendOptions ? ) { const { start , end , daysOfWeek , frequency = 5 , name = '' , enable = true , } = rule ; const awayRule = { frequency , name , enable : enable ? 1 : 0 , ... createRule ( { start , end , daysOfWeek } ) , } ; return this . device . sendCommand ( { [ this . apiModuleName ] : { add_rule : awayRule } , } , this . childId , sendOptions ) ; } returns throws ResponseError async editRule ( rule , sendOptions ? ) { const { id , start , end , daysOfWeek , frequency = 5 , name = '' , enable = true , } = rule ; const awayRule = { id , frequency , name , enable : enable ? 1 : 0 , ... createRule ( { start , end , daysOfWeek } ) , } ; return this . device . sendCommand ( { [ this . apiModuleName ] : { edit_rule : awayRule } , } , this . childId , sendOptions ) ; } returns throws ResponseError async deleteAllRules ( sendOptions ? ) { return this . device . sendCommand ( { [ this . apiModuleName ] : { delete_all_rules : { } } , } , this . childId , sendOptions ) ; } returns throws ResponseError async deleteRule ( id , sendOptions ? ) { return this . device . sendCommand ( { [ this . apiModuleName ] : { delete_rule : { id } } , } , this . childId , sendOptions ) ; } returns throws ResponseError async setOverallEnable ( enable , sendOptions ? ) { return this . device . sendCommand ( { [ this . apiModuleName ] : { set_overall_enable : { enable : enable ? 1 : 0 } , } , } , this . childId , sendOptions ) ; } } module . exports = Away ;