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 Tuya Plugin #3385

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
4 changes: 4 additions & 0 deletions src/app/config/config.default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ export const defaultConfig: Config = {
topic: 'topic',
relayNumber: null,
},
tuya: {
enabled: false,
label: 'label',
},
},
octodash: {
customActions: [
Expand Down
5 changes: 5 additions & 0 deletions src/app/config/config.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ interface Plugins {
tpLinkSmartPlug: TPLinkSmartPlugPlugin;
tasmota: TasmotaPlugin;
tasmotaMqtt: TasmotaMqttPlugin;
tuya: TuyaPlugin;
}

interface Plugin {
Expand Down Expand Up @@ -92,6 +93,10 @@ interface TasmotaMqttPlugin extends Plugin {
relayNumber: number;
}

interface TuyaPlugin extends Plugin {
label: string;
}

interface OctoDash {
customActions: CustomAction[];
fileSorting: FileSorting;
Expand Down
8 changes: 8 additions & 0 deletions src/app/config/config.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,14 @@ export class ConfigService {
return this.config.plugins.ophom.enabled;
}

public useTuya(): boolean {
return this.config.plugins.tuya.enabled;
}

public getTuyaLabel(): string {
return this.config.plugins.tuya.label;
}

public useTpLinkSmartPlug(): boolean {
return this.config.plugins.tpLinkSmartPlug.enabled;
}
Expand Down
8 changes: 8 additions & 0 deletions src/app/config/setup/plugins/plugins.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,14 @@
<ng-container i18n="@@plugin-tplink-smartplug"> TPLink SmartPlug </ng-container>
</span>

<span>
<app-toggle-switch
[value]="tuyaPlugin"
(valueChange)="tuyaPluginChange.emit(!tuyaPlugin)"
></app-toggle-switch>
<ng-container i18n="@@plugin-tuya"> Tuya SmartPlug </ng-container>
</span>

<span>
<app-toggle-switch [value]="tasmotaPlugin" (valueChange)="tasmotaPluginChange.emit(!tasmotaPlugin)">
</app-toggle-switch>
Expand Down
2 changes: 2 additions & 0 deletions src/app/config/setup/plugins/plugins.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export class PluginsComponent {
@Input() psuControlPlugin: boolean;
@Input() ophomPlugin: boolean;
@Input() tpLinkSmartPlugPlugin: boolean;
@Input() tuyaPlugin: boolean;
@Input() tasmotaPlugin: boolean;
@Input() tasmotaMqttPlugin: boolean;

Expand All @@ -27,6 +28,7 @@ export class PluginsComponent {
@Output() psuControlPluginChange = new EventEmitter<boolean>();
@Output() ophomPluginChange = new EventEmitter<boolean>();
@Output() tpLinkSmartPlugPluginChange = new EventEmitter<boolean>();
@Output() tuyaPluginChange = new EventEmitter<boolean>();
@Output() tasmotaPluginChange = new EventEmitter<boolean>();
@Output() tasmotaMqttPluginChange = new EventEmitter<boolean>();
}
1 change: 1 addition & 0 deletions src/app/config/setup/setup.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
[(psuControlPlugin)]="config.plugins.psuControl.enabled"
[(ophomPlugin)]="config.plugins.ophom.enabled"
[(tpLinkSmartPlugPlugin)]="config.plugins.tpLinkSmartPlug.enabled"
[(tyuaPlugin)]="config.plugins.tuya.enabled"
[(tasmotaPlugin)]="config.plugins.tasmota.enabled"
[(tasmotaMqttPlugin)]="config.plugins.tasmotaMqtt.enabled"></app-config-setup-plugins>

Expand Down
1 change: 1 addition & 0 deletions src/app/model/octoprint/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ export * from './plugins/ophomplugstatus.model';
export * from './plugins/tp-link.model';
export * from './plugins/tasmota.model';
export * from './plugins/tasmota-mqtt.model';
export * from './plugins/tuya.model';
4 changes: 4 additions & 0 deletions src/app/model/octoprint/plugins/tuya.model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export interface TuyaCommand {
command: 'turnOn' | 'turnOff';
label: string;
}
25 changes: 25 additions & 0 deletions src/app/services/enclosure/enclosure.octoprint.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
TasmotaCommand,
TasmotaMqttCommand,
TPLinkCommand,
TuyaCommand,
} from '../../model/octoprint';
import { NotificationService } from '../../notification/notification.service';
import { EnclosureService } from './enclosure.service';
Expand Down Expand Up @@ -158,6 +159,8 @@ export class EnclosureOctoprintService implements EnclosureService {
this.setPSUStateTasmota(state);
} else if (this.configService.useTasmotaMqtt()) {
this.setPSUStateTasmotaMqtt(state);
} else if (this.configService.useTuya()) {
this.setPSUStateTuya(state);
} else {
this.notificationService.setNotification({
heading: $localize`:@@error-psu-state:Can't change PSU State!`,
Expand Down Expand Up @@ -307,6 +310,28 @@ export class EnclosureOctoprintService implements EnclosureService {
.subscribe();
}

private setPSUStateTuya(state: PSUState) {
const tuyaPayload: TuyaCommand = {
command: state === PSUState.ON ? 'turnOn' : 'turnOff',
label: this.configService.getTuyaLabel(),
};

this.http
.post(this.configService.getApiURL('plugin/tuyasmartplug'), tuyaPayload, this.configService.getHTTPHeaders())
.pipe(
catchError(error => {
this.notificationService.setNotification({
heading: $localize`:@@error-send-psu-gcode:Can't send GCode!`,
text: error.message,
type: NotificationType.ERROR,
time: new Date(),
});
return of(null);
}),
)
.subscribe();
}

togglePSU(): void {
this.currentPSUState === PSUState.ON ? this.setPSUState(PSUState.OFF) : this.setPSUState(PSUState.ON);
}
Expand Down
23 changes: 23 additions & 0 deletions src/app/settings/settings.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,7 @@
config.plugins.psuControl.enabled ||
config.plugins.ophom.enabled ||
config.plugins.tpLinkSmartPlug.enabled ||
config.plugins.tuya.enabled ||
config.plugins.tasmota.enabled ||
config.plugins.tasmotaMqtt.enabled
)
Expand All @@ -330,6 +331,7 @@
config.plugins.psuControl.enabled ||
config.plugins.ophom.enabled ||
config.plugins.tpLinkSmartPlug.enabled ||
config.plugins.tuya.enabled ||
config.plugins.tasmota.enabled ||
config.plugins.tasmotaMqtt.enabled
)
Expand Down Expand Up @@ -579,6 +581,27 @@
[(ngModel)]="config.plugins.tpLinkSmartPlug.smartPlugIP"
required
[disabled]="!config.plugins.tpLinkSmartPlug.enabled" />
<span class="settings__heading-2" i18n="@@settings-tuya">Tuya Smart Plug</span>
<div
class="settings__checkbox-container"
(click)="config.plugins.tuya.enabled = !config.plugins.tuya.enabled">
<span class="settings__checkbox">
<span class="settings__checkbox-checked" *ngIf="config.plugins.tuya.enabled"></span>
</span>
<span class="settings__checkbox-descriptor" i18n="@@settings-tuya-enabled">enabled</span>
</div>
<label for="tuya-label" class="settings__input-label" i18n="@@settings-tuya-label"
>Label</label
>
<input
type="text"
id="tuya-label"
class="settings__input"
name="tuya-label"
style="width: 44.94vw"
[(ngModel)]="config.plugins.tuya.label"
required
[disabled]="!config.plugins.tuya.enabled" />
<span class="settings__heading-2">Tasmota</span>
<div
class="settings__checkbox-container"
Expand Down