Skip to content

Commit

Permalink
Use new hilo api (#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
SanterreJo authored Feb 23, 2024
1 parent f91165c commit c143382
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 36 deletions.
10 changes: 5 additions & 5 deletions src/devices/Light.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import axios from "axios";
import { API, CharacteristicValue, PlatformAccessory } from "homebridge";
import { automationApi } from "../hiloApi";
import { HiloDevice } from "./HiloDevice";
import { DeviceValue, HiloAccessoryContext } from "./types";
import { hiloApi } from "../hiloApi";

export class Light extends HiloDevice<
"LightDimmer" | "LightSwitch" | "ColorBulb" | "WhiteBulb"
Expand Down Expand Up @@ -60,8 +60,8 @@ export class Light extends HiloDevice<
const on = value as boolean;
this.logger.debug(`Setting ${this.device.name} ${on ? "on" : "off"}`);
try {
await automationApi.put(
`/Locations/${this.device.locationId}/Devices/${this.device.id}/Attributes`,
await hiloApi.put(
`/Automation/v1/api/Locations/${this.device.locationId}/Devices/${this.device.id}/Attributes`,
{ OnOff: on }
);
} catch (error) {
Expand All @@ -83,8 +83,8 @@ export class Light extends HiloDevice<
`Setting ${this.device.name} brightness to ${brightness}`
);
try {
await automationApi.put(
`/Locations/${this.device.locationId}/Devices/${this.device.id}/Attributes`,
await hiloApi.put(
`/Automation/v1/api/Locations/${this.device.locationId}/Devices/${this.device.id}/Attributes`,
{ Intensity: brightness / 100 }
);
} catch (error) {
Expand Down
6 changes: 3 additions & 3 deletions src/devices/Outlet.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import axios from "axios";
import { API, CharacteristicValue, PlatformAccessory } from "homebridge";
import { automationApi } from "../hiloApi";
import { HiloDevice } from "./HiloDevice";
import { DeviceValue, HiloAccessoryContext } from "./types";
import { hiloApi } from "../hiloApi";

export class Outlet extends HiloDevice<"Outlet"> {
constructor(
Expand Down Expand Up @@ -40,8 +40,8 @@ export class Outlet extends HiloDevice<"Outlet"> {
const on = value as boolean;
this.logger.debug(`Setting ${this.device.name} ${on ? "on" : "off"}`);
try {
await automationApi.put(
`/Locations/${this.device.locationId}/Devices/${this.device.id}/Attributes`,
await hiloApi.put(
`/Automation/v1/api/Locations/${this.device.locationId}/Devices/${this.device.id}/Attributes`,
{ OnOff: on }
);
} catch (error) {
Expand Down
6 changes: 3 additions & 3 deletions src/devices/Thermostat.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import axios from "axios";
import { API, CharacteristicValue, PlatformAccessory } from "homebridge";
import { automationApi } from "../hiloApi";
import { HiloDevice } from "./HiloDevice";
import { DeviceValue, HiloAccessoryContext } from "./types";
import { hiloApi } from "../hiloApi";

export class Thermostat extends HiloDevice<"Thermostat"> {
constructor(
Expand Down Expand Up @@ -113,8 +113,8 @@ export class Thermostat extends HiloDevice<"Thermostat"> {
`Setting ${this.device.name} target temparature to ${targetTemperature}`
);
try {
await automationApi.put(
`/Locations/${this.device.locationId}/Devices/${this.device.id}/Attributes`,
await hiloApi.put(
`/Automation/v1/api/Locations/${this.device.locationId}/Devices/${this.device.id}/Attributes`,
{ TargetTemperature: targetTemperature }
);
} catch (error) {
Expand Down
24 changes: 11 additions & 13 deletions src/hilo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,7 @@ import * as signalR from "@microsoft/signalr";
import { getConfig, HiloConfig, setConfig } from "./config";
import { getLogger, setLogger, signalRLogger } from "./logger";
import { setApi } from "./api";
import {
automationApi,
eventsApi,
getWsAccessToken,
negotiate,
} from "./hiloApi";
import { getWsAccessToken, hiloApi, negotiate } from "./hiloApi";
import {
Device,
DeviceValue,
Expand Down Expand Up @@ -253,8 +248,8 @@ class Hilo implements DynamicPlatformPlugin {
return;
}
try {
const response = await eventsApi.get<EventsResponse>(
`/Locations/${location.id}/Events`,
const response = await hiloApi.get<EventsResponse>(
`/GDService/v1/api/Locations/${location.id}/Events`,
{ params: { active: true } }
);
const challenges = response.data;
Expand Down Expand Up @@ -291,9 +286,12 @@ type LocationsResponse = Array<Location>;
async function fetchLocations() {
getLogger().debug("Fetching locations");
try {
const response = await automationApi.get<LocationsResponse>("/Locations", {
params: { force: true },
});
const response = await hiloApi.get<LocationsResponse>(
"/Automation/v1/api/Locations",
{
params: { force: true },
}
);
return response.data;
} catch (error) {
getLogger().error(
Expand All @@ -308,8 +306,8 @@ type DevicesResponse = Array<Device>;
async function fetchDevices(location: Location) {
getLogger().debug("Fetching devices for location", location.name);
try {
const response = await automationApi.get<DevicesResponse>(
`/Locations/${location.id}/Devices`,
const response = await hiloApi.get<DevicesResponse>(
`/Automation/v1/api/Locations/${location.id}/Devices`,
{
params: { force: true },
}
Expand Down
16 changes: 4 additions & 12 deletions src/hiloApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ type NegotiateResponse = {
};
export async function negotiate() {
getLogger().debug("Negotiating websocket connection");
const response = await hubApi.post<NegotiateResponse>(
const response = await hiloApi.post<NegotiateResponse>(
"/DeviceHub/negotiate",
{},
{
Expand Down Expand Up @@ -156,14 +156,8 @@ export function setupAutoRefreshToken(expiresIn: number) {
}, expiresIn * 1000 - 1000 * 60 * 5); // 5 minutes before expiration
}

export const automationApi = axios.create({
baseURL: "https://apim.hiloenergie.com/Automation/v1/api",
});
export const eventsApi = axios.create({
baseURL: "https://apim.hiloenergie.com/GDService/v1/api",
});
export const hubApi = axios.create({
baseURL: "https://automation.hiloenergie.com",
export const hiloApi = axios.create({
baseURL: "https://api.hiloenergie.com",
});

const authInterceptor = async (config: AxiosRequestConfig) => {
Expand Down Expand Up @@ -197,9 +191,7 @@ const authInterceptor = async (config: AxiosRequestConfig) => {
return config;
};

automationApi.interceptors.request.use(authInterceptor);
eventsApi.interceptors.request.use(authInterceptor);
hubApi.interceptors.request.use(authInterceptor);
hiloApi.interceptors.request.use(authInterceptor);

const unableToLogin = (e: unknown) =>
getLogger().error(
Expand Down

0 comments on commit c143382

Please sign in to comment.