Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add cover component #676

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion src/api/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ import { ReadData } from './connection';
import {
BinarySensorStateResponse,
CoverStateResponse,
LegacyCoverState,
LightStateResponse,
ListEntitiesBinarySensorResponse,
ListEntitiesCoverResponse,
ListEntitiesLightResponse,
ListEntitiesSensorResponse,
ListEntitiesSwitchResponse,
Expand All @@ -16,7 +18,7 @@ import { ListEntityResponses, StateResponses } from './interfaces';
import { CommandInterface } from '../components';
import { Observable } from 'rxjs';
import { filter } from 'rxjs/operators';
import { BaseComponent, BinarySensorComponent, LightComponent, SensorComponent, SwitchComponent } from '..';
import { BaseComponent, BinarySensorComponent, CoverComponent, LightComponent, SensorComponent, SwitchComponent } from '..';

export const stateParser = (data: ReadData): StateResponses | undefined => {
switch (data.type) {
Expand Down Expand Up @@ -98,6 +100,19 @@ export const createComponents = (
component: new SensorComponent(response, state$, emptyCommandInterface),
};
}
case MessageTypes.ListEntitiesCoverResponse: {
const response: ListEntitiesCoverResponse = decode(ListEntitiesCoverResponse, data);
const state$ = transformStates<CoverStateResponse>(stateEvents$, response);
return knownComponents.has(response.objectId)
? {
id: response.objectId,
state$,
}
: {
id: response.objectId,
component: new CoverComponent(response, state$, connection),
};
}
}
return { id: '' };
};
Expand Down
68 changes: 68 additions & 0 deletions src/components/cover.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { BaseComponent } from './base';
import { ComponentType, CoverEntity } from './entities';
import { MessageTypes } from '../api';
import { CoverCommandRequest, CoverOperation, LegacyCoverCommand } from '../api/protobuf/api';
import { CoverStateEvent } from './states';
import { title } from 'process';

export class CoverComponent extends BaseComponent<CoverEntity, CoverStateEvent> {
public open(): void {
this.queueCommand(MessageTypes.CoverCommandRequest, () =>
CoverCommandRequest.encode(this.generateState(LegacyCoverCommand.LEGACY_COVER_COMMAND_OPEN, 1.0)).finish(),
);
}

public close(): void {
this.queueCommand(MessageTypes.CoverCommandRequest, () =>
CoverCommandRequest.encode(this.generateState(LegacyCoverCommand.LEGACY_COVER_COMMAND_CLOSE, 0.0)).finish(),
);
}

public stop(): void {
this.queueCommand(MessageTypes.CoverCommandRequest, () =>
CoverCommandRequest.encode(this.generateState(LegacyCoverCommand.LEGACY_COVER_COMMAND_STOP, -1)).finish(),
);
}

public setPosition(position: number): void {
if (!this.supportsPosition) {
return;
}

this.queueCommand(MessageTypes.CoverCommandRequest, () =>
CoverCommandRequest.encode({
...this.generateState(LegacyCoverCommand.UNRECOGNIZED, position),
}).finish(),
);
}

public get assumedState(): boolean {
return this.listEntity.assumedState;
}

public get supportsPosition(): boolean {
return this.listEntity.supportsPosition;
}

public get supportsTilt(): boolean {
return this.listEntity.supportsTilt;
}

public get type(): ComponentType {
return 'cover';
}

private generateState(command: LegacyCoverCommand, position: number): CoverCommandRequest {
const state = this.state.getValue();
return {
key: this.key,
hasLegacyCommand: command ? true : false,
legacyCommand: command,
hasPosition: command != LegacyCoverCommand.LEGACY_COVER_COMMAND_STOP,
position: position,
hasTilt: false,
tilt: 0,
stop: command === LegacyCoverCommand.LEGACY_COVER_COMMAND_STOP,
};
}
}
8 changes: 7 additions & 1 deletion src/components/entities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,10 @@ export interface LightEntity extends ListEntity {
supportsRgb: boolean;
}

export type ComponentType = 'light' | 'binarySensor' | 'sensor' | 'switch';
export interface CoverEntity extends ListEntity {
assumedState: boolean;
supportsPosition: boolean;
supportsTilt: boolean;
}

export type ComponentType = 'light' | 'binarySensor' | 'sensor' | 'switch' | 'cover';
1 change: 1 addition & 0 deletions src/components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export * from './base';
export * from './binarySensor';
export * from './binarySensorTypes';
export * from './commandInterface';
export * from './cover';
export * from './entities';
export * from './helpers';
export * from './interfaces';
Expand Down
9 changes: 9 additions & 0 deletions src/components/states.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { CoverOperation, LegacyCoverState } from "../api/protobuf/api";

export interface StateEvent {
key: number;
}
Expand All @@ -20,3 +22,10 @@ export interface LightStateEvent extends StateEvent {
blue?: number;
effect?: string;
}

export interface CoverStateEvent extends StateEvent {
state?: LegacyCoverState;
position?: number;
tilt?: number;
operationr?: CoverOperation;
}
19 changes: 10 additions & 9 deletions src/run.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
import { filter, take, tap } from 'rxjs/operators';
import { LightComponent } from './components/light';
import { EspDevice, isSwitchComponent, isTrue } from './api';
import { EspDevice, isTrue } from './api';
import { BaseComponent } from './components/base';
import { CoverComponent } from './components/cover';

const device = new EspDevice('172.16.0.112');
device.discovery$
.pipe(
filter(isTrue),
take(1),
tap(() => {
const kitchenLights = device.components['kitchen_lights:'] as LightComponent;
const livingRoomDehumidifier = device.components['living_room_dehumidifier'];
console.log(livingRoomDehumidifier.name);

if (isSwitchComponent(livingRoomDehumidifier)) {
livingRoomDehumidifier.state$.pipe(tap(console.log)).subscribe();
livingRoomDehumidifier.turnOff();
for (const [key, value] of Object.entries(device.components)) {
console.log(key, value.type);
if (value instanceof CoverComponent) {
value.open();
value.close();
value.stop();
}
}
}),
)
Expand Down