Skip to content

Commit

Permalink
fix: Add new transitionCount parameter on thermostatScheduleDayMultiDP (
Browse files Browse the repository at this point in the history
  • Loading branch information
Nopraz authored Dec 1, 2024
1 parent d803c63 commit 9bf46b4
Showing 1 changed file with 44 additions and 38 deletions.
82 changes: 44 additions & 38 deletions src/lib/tuya.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
},
Expand Down Expand Up @@ -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('/');
Expand Down

0 comments on commit 9bf46b4

Please sign in to comment.