Skip to content

Commit

Permalink
Add constraints for PS5
Browse files Browse the repository at this point in the history
  • Loading branch information
kopiro committed Oct 18, 2023
1 parent 75ac35f commit 12da340
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 18 deletions.
2 changes: 1 addition & 1 deletion config.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"description": "Determine how often should device information be fetched (in milliseconds)"
},
"apps": {
"title": "List of applications you can control.",
"title": "List of applications you can control. (Doesn't work on PlayStation 5)",
"description": "A list of playstation games. You can find them here <a href=\"https://serialstation.com\" target=\"_blank\">serialstation.com</a> or use `playactor check` to get information about the running title.",
"type": "array",
"items": {
Expand Down
62 changes: 45 additions & 17 deletions src/playstationAccessory.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
import {API, Characteristic, CharacteristicValue, PlatformAccessory, Service,} from "homebridge";
import {Device} from "playactor/dist/device";
import {DeviceStatus, IDiscoveredDevice,} from "playactor/dist/discovery/model";

import {PlaystationPlatform} from "./playstationPlatform";
import {PLUGIN_NAME} from "./settings";
import {
API,
Characteristic,
CharacteristicValue,
PlatformAccessory,
Service,
} from "homebridge";
import { Device } from "playactor/dist/device";
import {
DeviceStatus,
IDiscoveredDevice,
} from "playactor/dist/discovery/model";

import { PlaystationPlatform } from "./playstationPlatform";
import { PLUGIN_NAME } from "./settings";

export class PlaystationAccessory {
private readonly accessory: PlatformAccessory;
Expand Down Expand Up @@ -79,7 +88,7 @@ export class PlaystationAccessory {

this.tvService
.getCharacteristic(this.Characteristic.ActiveIdentifier)
.onSet(this.setTitleSwitchState.bind(this))
.onSet(this.setTitleSwitchState.bind(this));

setInterval(
this.updateDeviceInformations.bind(this),
Expand All @@ -91,30 +100,38 @@ export class PlaystationAccessory {

private setTitleList() {
// if nothing selected yet, add a placeholder
this.addTitleToList('CUSAXXXXXX', '---', 0);
this.addTitleToList("CUSAXXXXXX", "---", 0);
const titleList = this.platform.config.apps || [];
titleList.forEach((title, index) => {
this.addTitleToList(title.id, title.name, index + 1);
});
}

private addTitleToList(titleId: string, titleName: string, index: number) {

this.platform.log.debug("adding input for title: ", titleId, titleName);

const titleInputSource = new this.Service.InputSource(titleName, titleId);
titleInputSource
.setCharacteristic(this.Characteristic.Identifier, index)
.setCharacteristic(this.Characteristic.Name, titleName)
.setCharacteristic(this.Characteristic.ConfiguredName, titleName)
.setCharacteristic(this.Characteristic.IsConfigured, this.Characteristic.IsConfigured.CONFIGURED)
.setCharacteristic(this.Characteristic.InputSourceType, this.Characteristic.InputSourceType.APPLICATION)
.setCharacteristic(this.Characteristic.CurrentVisibilityState, this.Characteristic.CurrentVisibilityState.SHOWN);
.setCharacteristic(
this.Characteristic.IsConfigured,
this.Characteristic.IsConfigured.CONFIGURED
)
.setCharacteristic(
this.Characteristic.InputSourceType,
this.Characteristic.InputSourceType.APPLICATION
)
.setCharacteristic(
this.Characteristic.CurrentVisibilityState,
this.Characteristic.CurrentVisibilityState.SHOWN
);

this.accessory.addService(titleInputSource);
this.tvService.addLinkedService(titleInputSource);

this.titleIDs.push(titleId)
this.titleIDs.push(titleId);
}

private async discoverDevice() {
Expand All @@ -130,12 +147,20 @@ export class PlaystationAccessory {
.getCharacteristic(this.platform.Characteristic.Active)
.updateValue(this.deviceInformation.status === DeviceStatus.AWAKE);

const runningAppTitle = this.titleIDs.indexOf(this.deviceInformation.extras['running-app-titleid'] || 0);
const runningAppTitle = this.titleIDs.indexOf(
this.deviceInformation.extras["running-app-titleid"] || 0
);
this.tvService
.getCharacteristic(this.platform.Characteristic.ActiveIdentifier)
.updateValue(runningAppTitle == -1 ? 0 : runningAppTitle);

this.platform.log.debug(`Device status updated ${this.deviceInformation.status}, title: ${this.deviceInformation.extras['running-app-titleid'] || 'no app running'} ${this.deviceInformation.extras['running-app-name'] || ''} id: ${runningAppTitle}`);
this.platform.log.debug(
`Device status updated ${this.deviceInformation.status}, title: ${
this.deviceInformation.extras["running-app-titleid"] || "no app running"
} ${
this.deviceInformation.extras["running-app-name"] || ""
} id: ${runningAppTitle}`
);
}

private async updateDeviceInformations(force = false) {
Expand Down Expand Up @@ -233,7 +258,7 @@ export class PlaystationAccessory {
private async setTitleSwitchState(value: CharacteristicValue) {
this.platform.log.debug("setTitleSwitchState ->", value);

const requestedTitle = this.titleIDs[value as number] as string || null
const requestedTitle = (this.titleIDs[value as number] as string) || null;

if (!requestedTitle) {
this.platform.log.debug("No title found for index ->", value);
Expand All @@ -253,7 +278,10 @@ export class PlaystationAccessory {

this.discoverDevice()
.then(async (device) => {
if (this.deviceInformation.extras['running-app-titleid'] === requestedTitle) {
if (
this.deviceInformation.extras["running-app-titleid"] ===
requestedTitle
) {
this.platform.log.debug("Title already running");
this.updateCharacteristics();
return;
Expand Down

0 comments on commit 12da340

Please sign in to comment.