Skip to content
← Public packages

@kentcdodds/hydrawise

Server-side Hunter Hydrawise controller and zone helpers backed by the hydrawiseApiKey secret.

src/index.d.ts

106 lines · 3.9 KB · TypeScript
export type HydrawiseControllerInput = {
	/** Hunter controller id; omit for the account default. */
	controllerId?: number
}

export type HydrawiseRunZoneInput = HydrawiseControllerInput & {
	/** Irrigation relay/zone id. */
	relayId: number
	/** Run duration in seconds. */
	seconds: number
}

export type HydrawiseSuspendInput = HydrawiseControllerInput & {
	relayId: number
	/** Unix epoch seconds to suspend until. */
	untilEpochSeconds: number
}

export type HydrawiseDispatchInput =
	| HydrawiseControllerInput
	| (HydrawiseControllerInput & { action?: 'get-controllers' })
	| (HydrawiseControllerInput & { action: 'get-status' | 'list-zones' })
	| (HydrawiseRunZoneInput & { action: 'run-zone' })
	| (HydrawiseControllerInput & { action: 'run-all-zones'; seconds: number })
	| (HydrawiseRunZoneInput & { action: 'stop-zone'; relayId: number })
	| (HydrawiseControllerInput & { action: 'stop-all-zones' })
	| (HydrawiseSuspendInput & { action: 'suspend-zone' })
	| (HydrawiseControllerInput & { action: 'suspend-all-zones'; untilEpochSeconds: number })
	| (HydrawiseRunZoneInput & { action: 'resume-zone-schedule'; relayId: number })
	| (HydrawiseControllerInput & { action: 'resume-all-zone-schedules' })

export type HydrawiseControllersResult = {
	customerId?: number
	currentControllerId?: number
	currentControllerName?: string
	controllers: Array<{
		id: number
		name: string
		lastContact?: string
		hasSerialNumber: boolean
		status?: string
	}>
	raw: unknown
}

export type HydrawiseZone = {
	id: number
	number: number
	name: string
	nextRunInSeconds?: number
	nextRunTimeLabel?: string
	runDurationSeconds?: number
	type?: string
	periodSeconds?: number
	[key: string]: unknown
}

/**
 * Compact authenticated Hydrawise REST helper. Relative endpoint under `/api/v1/`,
 * `api_key` query secret placeholder, JSON parse, and error handling.
 *
 * @example
 * import { request } from 'kody:@kentcdodds/hydrawise'
 * const details = await request('customerdetails.php')
 */
export function request(
	endpoint: string,
	query?: Record<string, string | number | boolean | null | undefined>,
): Promise<unknown>

/** True for rate limits and transport blips that weekday-status should skip. */
export function isSoftSkipError(error: unknown): boolean

/**
 * Dispatch Hunter Hydrawise irrigation commands (status, run, stop, suspend) via the API key secret.
 *
 * Omit `action` or pass `get-controllers` to list controllers; use `list-zones` for read-only zone status.
 *
 * @example
 * import hydrawise from 'kody:@kentcdodds/hydrawise'
 * const status = await hydrawise({ action: 'list-zones' })
 * // => { nextPollSeconds: 60, zones: [{ id: 1, name: 'Front', ... }], ... }
 */
export default function hydrawise(
	input?: HydrawiseDispatchInput,
): Promise<HydrawiseControllersResult | Record<string, unknown>>

export function getControllers(): Promise<HydrawiseControllersResult>
export function getStatus(input?: HydrawiseControllerInput): Promise<Record<string, unknown>>
export function listZones(
	input?: HydrawiseControllerInput,
): Promise<{ nextPollSeconds?: number; zones: HydrawiseZone[] }>
export function runZone(input: HydrawiseRunZoneInput): Promise<Record<string, unknown>>
export function runAllZones(
	input: HydrawiseControllerInput & { seconds: number },
): Promise<Record<string, unknown>>
export function stopZone(input: HydrawiseRunZoneInput): Promise<Record<string, unknown>>
export function stopAllZones(input?: HydrawiseControllerInput): Promise<Record<string, unknown>>
export function suspendZone(input: HydrawiseSuspendInput): Promise<Record<string, unknown>>
export function suspendAllZones(
	input: HydrawiseControllerInput & { untilEpochSeconds: number },
): Promise<Record<string, unknown>>
export function resumeZoneSchedule(input: HydrawiseRunZoneInput): Promise<Record<string, unknown>>
export function resumeAllZoneSchedules(
	input?: HydrawiseControllerInput,
): Promise<Record<string, unknown>>