Skip to content

Commit 73770e8

Browse files
Add discovery_topic option, descriptive error when stdout empty (#419)
* add mqtt discovery_topic * add mqtt discovery_topic * add mqtt discovery_topic * use custom discoveryTopic if defined by user * add discovery_topic to mqtt config * add discovery topic documentation * add discovery topic documentation * add discovery topic documentation * throw error if stdout is empty - indicates that we are unable to connect to the PlayStation, which would happen when it is powered off or there is a networking issue causing the PlayStation to not be reachable * throw message string instead of Error, improve message to include unreachable devices * Throw string instead of error to prevent including stack trace in debugger (since we already know where the message is originating from). * Improve the message to include the scenario where are user's Playstation may be powered on, but it is unreachable on their network, which will also lead to empty `stdout` * Use existing getContext mechanism to acquire settings --------- Co-authored-by: FunkeyFlo <[email protected]>
1 parent 3fdbca7 commit 73770e8

File tree

11 files changed

+43
-12
lines changed

11 files changed

+43
-12
lines changed

add-ons/ps5-mqtt-edge/DOCS.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,17 @@ account_check_interval: 5000 # Recommended interval for checking account
2424
### `mqtt` *optional*
2525
Optional [MQTT][mqtt-broker] connection information.
2626

27+
2728
If no information was provided the connection information will be acquired automatically.
2829

30+
**If you don't use the default Home Assistant discovery topic (i.e., "homeassistant"), you MUST set the `discovery_topic` to your custom discovery topic as this cannot be acquired automatically.**
31+
2932
```yaml
3033
host: 192.168.0.2 # (ip)address of your mqtt broker
3134
port: '1883' # port of your mqtt broker
3235
user: mqttuser # username used for connecting to your mqtt broker
3336
pass: somepassword # password used for connecting to your mqtt broker
37+
discovery_topic: custom_topic # Home Assistant discovery topic. Must be set if you've changed the discovery topic in Home Assistant. Default: homeassistant
3438
```
3539

3640
### `logger`
@@ -84,4 +88,4 @@ If your devices are located on a VLAN you must use this option to point the addo
8488
[ha-ps4]: https://www.home-assistant.io/integrations/ps4/
8589
[node-debug]: https://github.com/debug-js/debug
8690
[mqtt-broker]: https://www.home-assistant.io/integrations/mqtt/
87-
[psn-api-auth]: https://psn-api.achievements.app/authentication/authenticating-manually#get-started
91+
[psn-api-auth]: https://psn-api.achievements.app/authentication/authenticating-manually#get-started

add-ons/ps5-mqtt-edge/config.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ schema:
3737
port: port?
3838
user: str?
3939
pass: str?
40+
discovery_topic: str?
4041
psn_accounts:
4142
- username: str?
4243
npsso: str

add-ons/ps5-mqtt/DOCS.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,14 @@ Optional [MQTT][mqtt-broker] connection information.
2626

2727
If no information was provided the connection information will be acquired automatically.
2828

29+
**If you don't use the default Home Assistant discovery topic (i.e., "homeassistant"), you MUST set the `discovery_topic` to your custom discovery topic as this cannot be acquired automatically.**
30+
2931
```yaml
3032
host: 192.168.0.2 # (ip)address of your mqtt broker
3133
port: '1883' # port of your mqtt broker
3234
user: mqttuser # username used for connecting to your mqtt broker
3335
pass: somepassword # password used for connecting to your mqtt broker
36+
discovery_topic: custom_topic # Home Assistant discovery topic. Must be set if you've changed the discovery topic in Home Assistant. Default: homeassistant
3437
```
3538

3639
### `logger`

add-ons/ps5-mqtt/config.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ schema:
3737
port: port?
3838
user: str?
3939
pass: str?
40+
discovery_topic: str?
4041
psn_accounts:
4142
- username: str?
4243
npsso: str

docs/DOCKER.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ services:
5050
- MQTT_PORT=1883 # port of your mqtt broker
5151
- MQTT_USERNAME=mqttuser # username used for connecting to your mqtt broker
5252
- MQTT_PASSWORD=mqttpassword # password used for connecting to your mqtt broker
53+
- DISCOVERY_TOPIC=your_custom_discovery_topic # Home Assistant discovery topic. Only needs to be set if you've changed the discovery topic in Home Assistant. Default: homeassistant
5354

5455
- DEVICE_CHECK_INTERVAL=5000
5556
- DEVICE_DISCOVERY_INTERVAL=60000
@@ -96,7 +97,8 @@ services:
9697
"host": "192.168.0.132",
9798
"port": "1883",
9899
"user": "mqttuser",
99-
"pass": "mqttpassword"
100+
"pass": "mqttpassword",
101+
"discovery_topic": "your_custom_discovery_topic"
100102
},
101103
102104
"device_check_interval": 5000,

ps5-mqtt/server/src/config.test.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ describe("Configuration", () => {
2222
host: 'core-mosquitto',
2323
port: '1883',
2424
pass: 'REDACTED',
25-
user: 'addons'
25+
user: 'addons',
26+
discovery_topic: 'homeassistant'
2627
},
2728
logger: '@ha:ps5:*,@ha:ps5-sensitive:*',
2829
device_check_interval: 5000,
@@ -47,7 +48,8 @@ describe("Configuration", () => {
4748
host: 'core-mosquitto',
4849
port: '1883',
4950
pass: 'REDACTED',
50-
user: 'addons'
51+
user: 'addons',
52+
discovery_topic: 'homeassistant'
5153
},
5254
logger: '@ha:ps5:*,@ha:ps5-sensitive:*',
5355
device_check_interval: 5000,
@@ -76,7 +78,8 @@ describe("Configuration", () => {
7678
host: 'core-mosquitto',
7779
port: '1883',
7880
pass: 'REDACTED',
79-
user: 'addons'
81+
user: 'addons',
82+
discovery_topic: 'homeassistant'
8083
},
8184
logger: '@ha:ps5:*,@ha:ps5-sensitive:*',
8285
device_check_interval: 5000,
@@ -99,7 +102,8 @@ describe("Configuration", () => {
99102
host: 'core-mosquitto',
100103
port: '1883',
101104
pass: 'REDACTED',
102-
user: 'addons'
105+
user: 'addons',
106+
discovery_topic: 'homeassistant'
103107
},
104108
logger: '@ha:ps5:*,@ha:ps5-sensitive:*',
105109
device_check_interval: 5000,
@@ -117,4 +121,4 @@ describe("Configuration", () => {
117121
frontendPort: '62428'
118122
});
119123
});
120-
});
124+
});

ps5-mqtt/server/src/config.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,15 @@ export module AppConfig {
4040
pass: string;
4141
port: string;
4242
user: string;
43+
discovery_topic: string;
4344
}
4445

4546
export interface MqttConfig {
4647
host: string;
4748
pass: string;
4849
port: string;
4950
user: string;
51+
discovery_topic: string;
5052
}
5153
}
5254

@@ -83,6 +85,7 @@ function getEnvConfig(): Partial<AppConfig> {
8385
MQTT_PASSWORD,
8486
MQTT_PORT,
8587
MQTT_USERNAME,
88+
DISCOVERY_TOPIC,
8689

8790
FRONTEND_PORT,
8891

@@ -105,6 +108,7 @@ function getEnvConfig(): Partial<AppConfig> {
105108
port: MQTT_PORT,
106109
pass: MQTT_PASSWORD,
107110
user: MQTT_USERNAME,
111+
discovery_topic: DISCOVERY_TOPIC || "homeassistant",
108112
},
109113

110114
device_check_interval:
@@ -130,4 +134,4 @@ function getEnvConfig(): Partial<AppConfig> {
130134
} as Partial<AppConfig & {
131135
mqtt: Partial<AppConfig.MqttConfig>
132136
}>
133-
}
137+
}

ps5-mqtt/server/src/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,9 @@ async function run() {
7979
?? path.join(os.homedir(), '.config', 'playactor', 'credentials.json'),
8080
allowPs4Devices: appConfig.include_ps4_devices ?? true,
8181

82-
deviceDiscoveryBroadcastAddress: appConfig.device_discovery_broadcast_address
82+
deviceDiscoveryBroadcastAddress: appConfig.device_discovery_broadcast_address,
83+
84+
discoveryTopic: appConfig.mqtt.discovery_topic,
8385
};
8486

8587
try {

ps5-mqtt/server/src/redux/sagas/check-devices-state.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ function* checkDevicesState() {
2828
throw new Error(stderr)
2929
}
3030

31+
if (!stdout) {
32+
throw "No data received from Playstation. If this error continues, " +
33+
"your Playstation is likely powered off or unreachable - it will " +
34+
"not be available until it is in either rest mode/powered on and reachable.";
35+
}
36+
3137
const updatedDevice: Device = JSON.parse(stdout);
3238

3339
if (

ps5-mqtt/server/src/redux/sagas/register-device.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type MQTT from "async-mqtt";
22
import { call, getContext, put } from "redux-saga/effects";
3-
import { MQTT_CLIENT } from "../../services";
3+
4+
import { MQTT_CLIENT, SETTINGS, Settings } from "../../services";
45
import { HaMqtt } from "../../util/ha-mqtt";
56
import { addDevice, updateHomeAssistant } from "../action-creators";
67
import type { RegisterDeviceAction } from "../types";
@@ -9,6 +10,7 @@ function* registerDevice(
910
{ payload: device }: RegisterDeviceAction
1011
) {
1112
const mqtt: MQTT.AsyncClient = yield getContext(MQTT_CLIENT);
13+
const { discoveryTopic }: Settings = yield getContext(SETTINGS);
1214

1315
const deviceConfig = HaMqtt.getMqttDeviceConfig(device);
1416

@@ -20,7 +22,7 @@ function* registerDevice(
2022
) => Promise<MQTT.IPublishPacket>
2123
>(
2224
mqtt.publish.bind(mqtt),
23-
`homeassistant/switch/${device.id}/power/config`,
25+
`${discoveryTopic}/switch/${device.id}/power/config`,
2426
// https://www.home-assistant.io/integrations/switch.mqtt/
2527
JSON.stringify(<HaMqtt.Config.MqttSwitchEntity>{
2628
availability: [
@@ -53,7 +55,7 @@ function* registerDevice(
5355
) => Promise<MQTT.IPublishPacket>
5456
>(
5557
mqtt.publish.bind(mqtt),
56-
`homeassistant/sensor/${device.id}/activity/config`,
58+
`${discoveryTopic}/sensor/${device.id}/activity/config`,
5759
JSON.stringify(<HaMqtt.Config.MqttSensorEntity>{
5860
availability: [
5961
{

0 commit comments

Comments
 (0)