Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new transitionCount parameter on thermostatScheduleDayMultiDP #8414

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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