Skip to content

Commit

Permalink
fix(core): incorrect state tracking
Browse files Browse the repository at this point in the history
The current state of a device was not taken into consideration when generating the updated state after receiving an update from atvscript. This way, attributes that are not reported in that specific result of atvscript, default to null, which is unwanted since in reality they are still unchanged. E.g. when changing the volume, all attributes beside the volume are set to null, since the volume attribute is the only one reported by atvscript when changing the volume.
  • Loading branch information
maxileith committed Dec 28, 2023
1 parent 65cbc62 commit e4bd0af
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/lib/device-events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ export default class NodePyATVDeviceEvents extends EventEmitter {

private applyPushUpdate(update: NodePyATVInternalState, reqId: string): void {
try {
const newState = parseState(update, reqId, this.options);
const newState = parseState(update, this.state, reqId, this.options);
this.applyStateAndEmitEvents(newState);
}
catch(error) {
Expand Down
6 changes: 3 additions & 3 deletions src/lib/device.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export default class NodePyATVDevice implements EventEmitter{

constructor(options: NodePyATVDeviceOptions) {
this.options = Object.assign({}, options);
this.state = parseState({}, '', {});
this.state = parseState({}, undefined, '', {});
this.events = new NodePyATVDeviceEvents(this.state, this, this.options);

// @todo basic validation
Expand Down Expand Up @@ -232,7 +232,7 @@ export default class NodePyATVDevice implements EventEmitter{
const parameters = getParamters(this.options);

const result = await request(id, NodePyATVExecutableType.atvscript, [...parameters, 'playing'], this.options);
const newState = parseState(result, id, this.options);
const newState = parseState(result, this.state, id, this.options);

this.applyState(newState);
return newState;
Expand All @@ -248,7 +248,7 @@ export default class NodePyATVDevice implements EventEmitter{
* @category State
*/
clearState(): void {
this.applyState(parseState({}, '', {}));
this.applyState(parseState({}, undefined, '', {}));
}

private applyState(newState: NodePyATVState): void {
Expand Down
5 changes: 3 additions & 2 deletions src/lib/tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ function parseStateStringAttr(
d(`No ${outputAttr} value found in input (${JSON.stringify(input)})`);
}

export function parseState(input: NodePyATVInternalState, id: string, options: NodePyATVInstanceOptions): NodePyATVState {
export function parseState(input: NodePyATVInternalState, currentState: NodePyATVState, id: string, options: NodePyATVInstanceOptions): NodePyATVState {
const d = (msg: string) => debug(id, msg, options);
const result: NodePyATVState = {
dateTime: null,
Expand All @@ -253,7 +253,8 @@ export function parseState(input: NodePyATVInternalState, id: string, options: N
appId: null,
powerState: null,
volume: null,
focusState: null
focusState: null,
...currentState
};
if (!input || typeof input !== 'object') {
return result;
Expand Down

0 comments on commit e4bd0af

Please sign in to comment.