diff --git a/src/lib/tuya.ts b/src/lib/tuya.ts index 98637e722148c..c86a5f3c0003b 100644 --- a/src/lib/tuya.ts +++ b/src/lib/tuya.ts @@ -997,48 +997,54 @@ export const valueConverter = { }, }, thermostatScheduleDayMultiDP: { - from: (v: string) => { - const schedule = []; - for (let index = 1; index < 17; index = index + 4) { - schedule.push( - String(parseInt(v[index + 0])).padStart(2, '0') + - ':' + - String(parseInt(v[index + 1])).padStart(2, '0') + - '/' + - // @ts-expect-error ignore - (parseFloat((v[index + 2] << 8) + v[index + 3]) / 10.0).toFixed(1), - ); - } - return schedule.join(' '); - }, - to: (v: string) => { - const payload = [0]; - const transitions = v.split(' '); - if (transitions.length != 4) { - throw new Error('Invalid schedule: there should be 4 transitions'); - } - for (const transition of transitions) { - const timeTemp = transition.split('/'); - if (timeTemp.length != 2) { - throw new Error('Invalid schedule: wrong transition format: ' + transition); + from: (v: string) => valueConverter.thermostatScheduleDayMultiDPWithTransitionCount().from(v), + to: (v: string) => valueConverter.thermostatScheduleDayMultiDPWithTransitionCount().to(v), + }, + thermostatScheduleDayMultiDPWithTransitionCount: (transitionCount: number = 4) => { + return { + from: (v: string) => { + const schedule = []; + for (let index = 1; index < transitionCount * 4 - 1; index = index + 4) { + schedule.push( + String(parseInt(v[index + 0])).padStart(2, '0') + + ':' + + String(parseInt(v[index + 1])).padStart(2, '0') + + '/' + + // @ts-expect-error ignore + (parseFloat((v[index + 2] << 8) + v[index + 3]) / 10.0).toFixed(1), + ); } - const hourMin = timeTemp[0].split(':'); - const hour = parseInt(hourMin[0]); - const min = parseInt(hourMin[1]); - const temperature = Math.floor(parseFloat(timeTemp[1]) * 10); - if (hour < 0 || hour > 24 || min < 0 || min > 60 || temperature < 50 || temperature > 300) { - throw new Error('Invalid hour, minute or temperature of: ' + transition); + return schedule.join(' '); + }, + to: (v: string) => { + const payload = [0]; + const transitions = v.split(' '); + if (transitions.length != transitionCount) { + throw new Error(`Invalid schedule: there should be ${transitionCount} transitions`); } - payload.push(hour, min, (temperature & 0xff00) >> 8, temperature & 0xff); - } - return payload; - }, + for (const transition of transitions) { + const timeTemp = transition.split('/'); + if (timeTemp.length != 2) { + throw new Error('Invalid schedule: wrong transition format: ' + transition); + } + const hourMin = timeTemp[0].split(':'); + const hour = parseInt(hourMin[0]); + const min = parseInt(hourMin[1]); + const temperature = Math.floor(parseFloat(timeTemp[1]) * 10); + if (hour < 0 || hour > 24 || min < 0 || min > 60 || temperature < 50 || temperature > 300) { + throw new Error('Invalid hour, minute or temperature of: ' + transition); + } + payload.push(hour, min, (temperature & 0xff00) >> 8, temperature & 0xff); + } + return payload; + }, + }; }, - thermostatScheduleDayMultiDPWithDayNumber: (dayNum: number) => { + thermostatScheduleDayMultiDPWithDayNumber: (dayNum: number, transitionCount: number = 4) => { return { - from: (v: string) => valueConverter.thermostatScheduleDayMultiDP.from(v), + from: (v: string) => valueConverter.thermostatScheduleDayMultiDPWithTransitionCount(transitionCount).from(v), to: (v: string) => { - const data = valueConverter.thermostatScheduleDayMultiDP.to(v); + const data = valueConverter.thermostatScheduleDayMultiDPWithTransitionCount(transitionCount).to(v); data[0] = dayNum; return data; }, @@ -1066,7 +1072,7 @@ export const valueConverter = { const payload = []; const transitions = v.split(' '); if (transitions.length != 6) { - throw new Error('Invalid schedule: there should be 4 transitions'); + throw new Error('Invalid schedule: there should be 6 transitions'); } for (const transition of transitions) { const timeTemp = transition.split('/');