Skip to content

Commit

Permalink
feat: improve ambilight brightness input values (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
clement-berard authored Nov 22, 2024
1 parent 0a3ef34 commit fbf0179
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 18 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"consola": "3.2.3",
"is-ip": "5.0.1",
"radash": "12.1.0",
"superstruct": "^2.0.2",
"urllib": "4.4.0",
"zod": "3.23.8"
},
Expand Down
9 changes: 9 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 20 additions & 1 deletion src/constants/jointspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,32 @@ const ambilightFollowAudioModeEnum = [
'VU_METER',
] as const;

const ambilightChangeBrightnessAvailableValues = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 'increase', 'decrease'] as const;
const ambilightBrightnessAvailableValuesIntegers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] as const;
const ambilightChangeBrightnessAvailableValuesStrings = ['increase', 'decrease'] as const;
const ambilightBrightnessAvailableValuesStrings = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10'] as const;
const ambilightBrightnessAvailableValues = [
...ambilightBrightnessAvailableValuesIntegers,
...ambilightBrightnessAvailableValuesStrings,
] as const;
const ambilightChangeBrightnessAvailableValues = [
...ambilightBrightnessAvailableValuesIntegers,
...ambilightBrightnessAvailableValuesStrings,
...ambilightChangeBrightnessAvailableValuesStrings,
] as const;

const ambilightChangeBrightnessAvailableSinglesValues = [
...ambilightBrightnessAvailableValuesStrings,
...ambilightChangeBrightnessAvailableValuesStrings,
] as const;

export const JOINTSPACE_CONSTANTS = {
ambilight: {
followAudioMode: ambilightFollowAudioModeEnum,
followVideoMode: ambilightFollowVideoModeEnum,
brightnessAvailableValues: ambilightChangeBrightnessAvailableValues,
ambilightChangeBrightnessAvailableValues,
ambilightBrightnessAvailableValues,
ambilightChangeBrightnessAvailableSinglesValues,
},
inputKeys,
} as const;
44 changes: 29 additions & 15 deletions src/lib/PhilTVApi.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
import { get } from 'radash';
import * as superstruct from 'superstruct';
import { z } from 'zod';
import { JOINTSPACE_CONSTANTS } from '../constants';
import type { AmbilightFollowAudioMode, AmbilightFollowVideoMode, InputKeys } from '../types/jointspace';
import type {
AmbilightChangeBrightnessAvailableValues,
AmbilightFollowAudioMode,
AmbilightFollowVideoMode,
AmbilightSetBrightnessAvailableValues,
InputKeys,
} from '../types';
import { PhilTVApiBase } from './PhilTVApiBase';

export class PhilTVApi extends PhilTVApiBase {
Expand All @@ -17,32 +24,39 @@ export class PhilTVApi extends PhilTVApiBase {
] as const;
}

async setAmbilightBrightness(brightness: number) {
async setAmbilightBrightness(brightness: AmbilightSetBrightnessAvailableValues) {
const schema = superstruct.enums(JOINTSPACE_CONSTANTS.ambilight.ambilightBrightnessAvailableValues as any);

const [errValidation] = schema.validate(brightness);

if (errValidation) {
return this.renderResponse(errValidation, undefined);
}

return this.handleSetMenuItemSetting('ambilight_brightness', {
value: brightness,
value: Number(brightness),
});
}

async changeAmbilightBrightness(move: string | number) {
const [, currentBrightness] = await this.getAmbilightBrightnessValue();

const realValue = (Number.isNaN(Number(move)) ? move : Number(move)) as any;
async changeAmbilightBrightness(move: AmbilightChangeBrightnessAvailableValues) {
const schema = superstruct.enums(JOINTSPACE_CONSTANTS.ambilight.ambilightChangeBrightnessAvailableValues as any);

const isValid = JOINTSPACE_CONSTANTS.ambilight.brightnessAvailableValues.includes(realValue);
const [errValidation] = schema.validate(move);

if (!isValid) {
return this.renderResponse(new Error('Invalid value'), undefined);
if (errValidation) {
return this.renderResponse(errValidation, undefined);
}

if (typeof realValue === 'string') {
const computedBrightness =
realValue === 'increase' ? Number(currentBrightness) + 1 : Number(currentBrightness) - 1;
const realBrightness = Math.min(10, Math.max(0, computedBrightness));
const [, currentBrightness] = await this.getAmbilightBrightnessValue();

if (move === 'increase' || move === 'decrease') {
const computedBrightness = move === 'increase' ? Number(currentBrightness) + 1 : Number(currentBrightness) - 1;
const realBrightness = Math.min(10, Math.max(0, computedBrightness)) as AmbilightSetBrightnessAvailableValues;

return this.setAmbilightBrightness(realBrightness);
}

return this.setAmbilightBrightness(realValue);
return this.setAmbilightBrightness(move);
}

increaseAmbilightBrightness() {
Expand Down
6 changes: 4 additions & 2 deletions src/types/jointspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,7 @@ export type AmbilightFollowVideoMode = (typeof JOINTSPACE_CONSTANTS.ambilight.fo
export type AmbilightFollowAudioMode = (typeof JOINTSPACE_CONSTANTS.ambilight.followAudioMode)[number];

export type AmbilightChangeBrightnessAvailableValues =
| (typeof JOINTSPACE_CONSTANTS.ambilight.brightnessAvailableValues)[number]
| string;
(typeof JOINTSPACE_CONSTANTS.ambilight.ambilightChangeBrightnessAvailableValues)[number];

export type AmbilightSetBrightnessAvailableValues =
(typeof JOINTSPACE_CONSTANTS.ambilight.ambilightBrightnessAvailableValues)[number];

0 comments on commit fbf0179

Please sign in to comment.