Skip to content

Commit

Permalink
refactor: move the types to the api directory
Browse files Browse the repository at this point in the history
  • Loading branch information
jvandenaardweg committed Dec 5, 2022
1 parent 6082fb3 commit 1dffdb2
Show file tree
Hide file tree
Showing 10 changed files with 30 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
HomeWizardApiStatePutParams,
HomeWizardApiStatePutResponse,
HomeWizardApiStateResponse,
} from "@/types";
} from "@/api/types";

interface Endpoints {
basic: string;
Expand Down
2 changes: 1 addition & 1 deletion src/api/mocks/data/basic.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { HomeWizardApiBasicInformationResponse } from "../../../types";
import { HomeWizardApiBasicInformationResponse } from "../../types";

export const mockBasicInformationResponse: HomeWizardApiBasicInformationResponse =
{
Expand Down
2 changes: 1 addition & 1 deletion src/api/mocks/data/identify.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { HomeWizardApiIdentifyResponse } from "../../../types";
import { HomeWizardApiIdentifyResponse } from "../../types";

export const mockIdentifyResponse: HomeWizardApiIdentifyResponse = {
identify: "ok",
Expand Down
2 changes: 1 addition & 1 deletion src/api/mocks/data/state.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { HomeWizardApiStateResponse } from "../../../types";
import { HomeWizardApiStateResponse } from "../../types";

export const mockStateResponse: HomeWizardApiStateResponse = {
brightness: 0,
Expand Down
2 changes: 1 addition & 1 deletion src/api/mocks/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { mockApiUrl } from "@/api/mocks/api-url";
import {
HomeWizardApiStatePutParams,
HomeWizardApiStateResponse,
} from "@/types";
} from "@/api/types";

/**
* Little helper method so we don't have to repeat ourselves.
Expand Down
17 changes: 15 additions & 2 deletions src/types.ts → src/api/types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { PlatformConfig } from "homebridge";

export const ENERGY_SOCKET_PRODUCT_TYPE = "HWE-SKT";

export const PLATFORM_MANUFACTURER = "HomeWizard";

/**
Expand All @@ -14,6 +12,21 @@ export const PLATFORM_MANUFACTURER = "HomeWizard";
export const MDNS_DISCOVERY_TYPE = "hwenergy";
export const MDNS_DISCOVERY_PROTOCOL = "tcp";

/**
* A list of device types that HomeWizard supports.
*
* We only support the Energy Socket for now.
*
* @link https://homewizard-energy-api.readthedocs.io/getting-started.html#supported-devices
*/
export enum HomeWizardSupportedDeviceTypes {
WIFI_PI_METER = "HWE-P1",
WIFI_ENERGY_SOCKET = "HWE-SKT",
WIFI_WATER_METER = "HWE-WTR",
WIFI_KWH_METER_PHASE_1 = "SDM230-wifi",
WIFI_KWH_METER_PHASE_2 = "SDM630-wifi",
}

export interface TxtRecord {
/** Indicates if the "Local API" is enabled. `"1"` = enabled, `"0"` = disabled */
api_enabled: string;
Expand Down
2 changes: 1 addition & 1 deletion src/energySocketAccessory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
EnergySocketAccessoryProperties,
HomeWizardEnergyPlatformAccessoryContext,
PLATFORM_MANUFACTURER,
} from "@/types";
} from "@/api/types";

/**
* Platform Accessory
Expand Down
11 changes: 7 additions & 4 deletions src/platform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ import { PLATFORM_NAME, PLUGIN_NAME } from "@/settings";
import { EnergySocketAccessory } from "@/energySocketAccessory";
import {
EnergySocketAccessoryProperties,
ENERGY_SOCKET_PRODUCT_TYPE,
HomeWizardSupportedDeviceTypes,
HomeWizardEnergyConfig,
HomeWizardEnergyPlatformAccessoryContext,
MDNS_DISCOVERY_PROTOCOL,
MDNS_DISCOVERY_TYPE,
TxtRecord,
} from "@/types";
} from "@/api/types";

/**
* HomebridgePlatform
Expand Down Expand Up @@ -158,7 +158,10 @@ export class HomebridgeHomeWizardEnergySocket implements DynamicPlatformPlugin {
}

isDeviceProductTypeSupported(txtRecord: TxtRecord): boolean {
return txtRecord.product_type === ENERGY_SOCKET_PRODUCT_TYPE;
return (
txtRecord.product_type ===
HomeWizardSupportedDeviceTypes.WIFI_ENERGY_SOCKET
);
}

handleDiscoveredService(service: BonjourService): void {
Expand All @@ -178,7 +181,7 @@ export class HomebridgeHomeWizardEnergySocket implements DynamicPlatformPlugin {
if (!this.isDeviceProductTypeSupported(txtRecord)) {
this.log.info(
this.loggerPrefix,
`Found a device that is not an Energy Socket (${ENERGY_SOCKET_PRODUCT_TYPE}), skipping`,
`Found a device that is not an Energy Socket (${HomeWizardSupportedDeviceTypes.WIFI_ENERGY_SOCKET}), skipping`,
JSON.stringify(txtRecord)
);
return;
Expand Down
2 changes: 1 addition & 1 deletion src/tests/platform.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { HomebridgeAPI } from "homebridge/lib/api";
import { HomebridgeHomeWizardEnergySocket } from "../platform";
import { PLATFORM_NAME } from "../settings";
import { TxtRecord } from "../types";
import { TxtRecord } from "../api/types";
import { loggerMock } from "./mocks/logger";

jest.mock("bonjour-service");
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@
"@/*": ["src/*"]
}
},
"include": ["src/", "jest.setup.ts"],
"include": ["src/", "jest.setup.js"],
"exclude": ["src/**/*.test.ts", "src/tests/**/*"]
}

0 comments on commit 1dffdb2

Please sign in to comment.