homebridge-poolcontroller-clocklear
Version:
A (semi) private Homebridge plugin for my custom Pool Controller app
53 lines • 1.72 kB
JavaScript
export class PoolControllerClient {
baseUrl;
apiKey;
constructor(baseUrl, apiKey) {
this.baseUrl = baseUrl.replace(/\/$/, '');
this.apiKey = apiKey;
}
async request(endpoint, options) {
const url = `${this.baseUrl}${endpoint}`;
const response = await fetch(url, {
...options,
headers: {
'Content-Type': 'application/json',
'X-API-KEY': this.apiKey,
...(options && options.headers),
},
});
if (!response.ok) {
const text = await response.text().catch(() => '');
const detail = text ? ` — ${text}` : '';
throw new Error(`Poolcontroller ${options?.method || 'GET'} ${endpoint} failed: `
+ `${response.status} ${response.statusText}${detail}`);
}
if (response.status === 204) {
return undefined;
}
const text = await response.text();
if (!text) {
return undefined;
}
return JSON.parse(text);
}
async getRelays() {
const body = await this.request('/relays');
return body.relayStates || [];
}
async setRelayState(relayId, on) {
const body = await this.request(`/relays/${relayId}/state`, {
method: 'POST',
body: JSON.stringify({ state: on ? 1 : 0 }),
});
return body.relayStates || [];
}
async getSchedules() {
return this.request('/config/schedules');
}
async toggleScheduler() {
await this.request('/config/schedules/toggle', {
method: 'POST',
});
}
}
//# sourceMappingURL=poolControllerClient.js.map