Skip to content

Commit

Permalink
Новая типизация и переписан MediaDevice
Browse files Browse the repository at this point in the history
  • Loading branch information
kraineff committed Feb 8, 2025
1 parent 300803f commit 0544f0f
Show file tree
Hide file tree
Showing 93 changed files with 2,242 additions and 1,589 deletions.
36 changes: 18 additions & 18 deletions app.ts → app.mts
Original file line number Diff line number Diff line change
@@ -1,36 +1,36 @@
import Homey from "homey";
import type { YandexMediaDevice } from "./library/client/home/devices/media.js";
import { Yandex } from "./library/index.js";
import { YandexSpeaker } from "./library/client/home/devices/speaker.js";

module.exports = class YandexApp extends Homey.App {
export default class YandexApp extends Homey.App {
yandex!: Yandex;
private scenarioIcons!: string[];
#scenarioIcons!: string[];

async onInit() {
this.yandex = new Yandex({
get: async () => JSON.parse(this.homey.settings.get("storage") ?? "{}"),
set: async content => this.homey.settings.set("storage", JSON.stringify(content))
});
await this.initFlows();

setInterval(async () => await this.yandex.api.quasar.getDevices().catch(console.error), 2.16e+7);

await this.initFlows();
}

async onUninit() {
await this.yandex.home.destroy();
await this.yandex.home.disconnect();
}

async initFlows() {
const mediaSayAction = this.homey.flow.getActionCard("media_say");
mediaSayAction.registerRunListener(async args => {
const speaker: YandexSpeaker = await args.device.getSpeaker();
await speaker.actionSay(args.text, args.volume);
const speaker: YandexMediaDevice = await args.device.getSpeaker();
await speaker.say(args.text, args.volume);
});

const mediaRunAction = this.homey.flow.getActionCard("media_run");
mediaRunAction.registerRunListener(async args => {
const speaker: YandexSpeaker = await args.device.getSpeaker();
const response = await speaker.actionRun(args.command, args.volume) || "";
const speaker: YandexMediaDevice = await args.device.getSpeaker();
const response = await speaker.send(args.command, args.volume);
return { response };
});

Expand All @@ -42,15 +42,15 @@ module.exports = class YandexApp extends Homey.App {
const items = [];

// Обновление иконки для нового сценария
if (!query.length && this.scenarioIcons === undefined) {
if (!query.length && this.#scenarioIcons === undefined) {
const scenarioIcons = await this.yandex.api.quasar.getScenarioIcons();
this.scenarioIcons = scenarioIcons.icons;
this.#scenarioIcons = scenarioIcons.icons;
}

// Отображение нового сценария
if (query.length && !names.includes(query)) {
const index = Math.floor(Math.random() * this.scenarioIcons.length);
const icon = this.scenarioIcons[index];
const index = Math.floor(Math.random() * this.#scenarioIcons.length);
const icon = this.#scenarioIcons[index];

items.push({
name: query,
Expand Down Expand Up @@ -115,7 +115,7 @@ module.exports = class YandexApp extends Homey.App {
type: "devices.capabilities.quasar.server_action",
state: {
instance: "text_action",
value: "громче на 0" + "!".repeat(this.getNextNumber(scenarioActions))
value: `громче на 0${"!".repeat(this.getNextNumber(scenarioActions))}`
},
parameters: {
instance: "text_action"
Expand All @@ -131,9 +131,9 @@ module.exports = class YandexApp extends Homey.App {
}

private getNextNumber(nums: number[]) {
nums = nums.sort();
for (let n = 1; n <= nums.length + 1; n++)
if (nums.indexOf(n) === -1) return n;
const sorted = nums.sort();
for (let n = 1; n <= sorted.length + 1; n++)
if (sorted.indexOf(n) === -1) return n;
return 1;
};
}
13 changes: 13 additions & 0 deletions biome.jsonc
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"$schema": "https://biomejs.dev/schemas/1.9.4/schema.json",
"formatter": {
"lineWidth": 120
},
"linter": {
"rules": {
"suspicious": {
"noExplicitAny": "warn"
}
}
}
}
Loading

0 comments on commit 0544f0f

Please sign in to comment.