Skip to content

Commit

Permalink
chore: format code
Browse files Browse the repository at this point in the history
  • Loading branch information
philippwaller committed Dec 17, 2023
1 parent 81454b5 commit e3c396e
Show file tree
Hide file tree
Showing 8 changed files with 199 additions and 200 deletions.
16 changes: 8 additions & 8 deletions src/modules/homeassistant/actions/action.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
* intended for direct instantiation.
*/
export class Action {
/**
* Constructs an Action instance. Blocks direct instantiation of this abstract class.
* @throws {TypeError} If directly instantiated.
*/
constructor() {
if (new.target === Action) {
throw new TypeError("Cannot instantiate abstract class Action directly");
}
/**
* Constructs an Action instance. Blocks direct instantiation of this abstract class.
* @throws {TypeError} If directly instantiated.
*/
constructor() {
if (new.target === Action) {
throw new TypeError('Cannot instantiate abstract class Action directly')
}
}
}
56 changes: 28 additions & 28 deletions src/modules/homeassistant/actions/service-action.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,36 @@
import {Action} from "@/modules/homeassistant/actions/action";
import { Action } from '@/modules/homeassistant/actions/action'

/**
* ServiceAction, extending Action, facilitates interactions with HomeAssistant services.
*/
export class ServiceAction extends Action {
/**
* Constructs a ServiceAction instance.
* @param {string} domain - Service domain, must be a non-empty string.
* @param {string} service - Service name, must be a non-empty string.
* @param {Array} [entity_id=[]] - Target entity IDs array.
* @param {Object} [serviceData={}] - Additional service data.
* @throws {Error} if 'domain' or 'service' are empty or not strings.
* @throws {TypeError} if 'entity_id' is not an array or 'serviceData' is not an object.
*/
constructor(domain, service, entity_id, serviceData) {
super();
/**
* Constructs a ServiceAction instance.
* @param {string} domain - Service domain, must be a non-empty string.
* @param {string} service - Service name, must be a non-empty string.
* @param {Array} [entity_id=[]] - Target entity IDs array.
* @param {Object} [serviceData={}] - Additional service data.
* @throws {Error} if 'domain' or 'service' are empty or not strings.
* @throws {TypeError} if 'entity_id' is not an array or 'serviceData' is not an object.
*/
constructor(domain, service, entity_id, serviceData) {
super()

if (typeof domain !== 'string' || !domain.trim()) {
throw new Error('Domain must be a non-empty string');
}
if (typeof service !== 'string' || !service.trim()) {
throw new Error('Service must be a non-empty string');
}
if (!Array.isArray(entity_id)) {
throw new TypeError('entity_id must be an array');
}
if (typeof serviceData !== 'object' || serviceData === null) {
throw new TypeError('serviceData must be an object');
}

this.service = `${domain}.${service}`;
this.data = serviceData;
this.target = {"entity_id": entity_id};
if (typeof domain !== 'string' || !domain.trim()) {
throw new Error('Domain must be a non-empty string')
}
if (typeof service !== 'string' || !service.trim()) {
throw new Error('Service must be a non-empty string')
}
if (!Array.isArray(entity_id)) {
throw new TypeError('entity_id must be an array')
}
if (typeof serviceData !== 'object' || serviceData === null) {
throw new TypeError('serviceData must be an object')
}

this.service = `${domain}.${service}`
this.data = serviceData
this.target = { entity_id: entity_id }
}
}
45 changes: 22 additions & 23 deletions src/modules/homeassistant/commands/command.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,29 @@
* that can be used to interact with the HomeAssistant WebSocket API.
*/
export class Command {
/**
* Constructs a Command instance.
* @param {number} requestId - The unique identifier for the command request.
* @param {string} type - The type of the command. Must be a non-empty string.
* @throws {TypeError} If an attempt is made to instantiate Command directly.
* @throws {Error} If the requestId is not a non-negative number.
* @throws {Error} If the type is not a non-empty string.
*/
constructor(requestId, type) {
// Prevent direct instantiation of this abstract class.
if (new.target === Command) {
throw new TypeError("Cannot instantiate abstract class Command directly");
}

if (typeof requestId !== 'number' || requestId < 0) {
throw new Error('requestId must be a non-negative number');
}
/**
* Constructs a Command instance.
* @param {number} requestId - The unique identifier for the command request.
* @param {string} type - The type of the command. Must be a non-empty string.
* @throws {TypeError} If an attempt is made to instantiate Command directly.
* @throws {Error} If the requestId is not a non-negative number.
* @throws {Error} If the type is not a non-empty string.
*/
constructor(requestId, type) {
// Prevent direct instantiation of this abstract class.
if (new.target === Command) {
throw new TypeError('Cannot instantiate abstract class Command directly')
}

if (typeof type !== 'string' || !type.trim()) {
throw new Error('type must be a non-empty string');
}
if (typeof requestId !== 'number' || requestId < 0) {
throw new Error('requestId must be a non-negative number')
}

this.id = requestId;
this.type = type;
if (typeof type !== 'string' || !type.trim()) {
throw new Error('type must be a non-empty string')
}
}

this.id = requestId
this.type = type
}
}
38 changes: 19 additions & 19 deletions src/modules/homeassistant/commands/execute-script-command.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {Command} from "@/modules/homeassistant/commands/command";
import {Action} from "@/modules/homeassistant/actions/action";
import { Command } from '@/modules/homeassistant/commands/command'
import { Action } from '@/modules/homeassistant/actions/action'

/**
* CallExecuteScriptCommand
Expand All @@ -9,24 +9,24 @@ import {Action} from "@/modules/homeassistant/actions/action";
* This enhancement enables more dynamic and context-sensitive operations within HomeAssistant.
*/
export class ExecuteScriptCommand extends Command {
/**
* Constructs a CallExecuteScriptCommand instance.
*
* @param {number} requestId - Number of iterations for execution. Must be non-negative.
* @param {Action[]} [actions=[]] - Array of ScriptCommand instances. Optional, defaults to empty.
* @throws {TypeError} if actions is not an array or has non-Action elements.
*/
constructor(requestId, actions = []) {
super(requestId, "execute_script");
/**
* Constructs a CallExecuteScriptCommand instance.
*
* @param {number} requestId - Number of iterations for execution. Must be non-negative.
* @param {Action[]} [actions=[]] - Array of ScriptCommand instances. Optional, defaults to empty.
* @throws {TypeError} if actions is not an array or has non-Action elements.
*/
constructor(requestId, actions = []) {
super(requestId, 'execute_script')

if (!Array.isArray(actions)) {
throw new TypeError('Actions must be an array');
}

if (actions.some(action => !(action instanceof Action))) {
throw new TypeError('Elements in actions must be Action instances or subclasses');
}
if (!Array.isArray(actions)) {
throw new TypeError('Actions must be an array')
}

this.sequence = actions;
if (actions.some((action) => !(action instanceof Action))) {
throw new TypeError('Elements in actions must be Action instances or subclasses')
}

this.sequence = actions
}
}
16 changes: 8 additions & 8 deletions src/modules/homeassistant/commands/get-services-command.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import {Command} from "@/modules/homeassistant/commands/command";
import { Command } from '@/modules/homeassistant/commands/command'

/**
* The GetServicesCommand class, a subclass of Command, is used for requesting
* service information from HomeAssistant.
*/
export class GetServicesCommand extends Command {
/**
* Constructs a GetServicesCommand instance.*
* @param {number} requestId - The unique identifier for the command request.
*/
constructor(requestId) {
super(requestId, "get_services");
}
/**
* Constructs a GetServicesCommand instance.*
* @param {number} requestId - The unique identifier for the command request.
*/
constructor(requestId) {
super(requestId, 'get_services')
}
}
16 changes: 8 additions & 8 deletions src/modules/homeassistant/commands/get-states-command.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import {Command} from "@/modules/homeassistant/commands/command";
import { Command } from '@/modules/homeassistant/commands/command'

/**
* The GetStatesCommand class, a subclass of Command, handles the retrieval of
* state information from HomeAssistant.
*/
export class GetStatesCommand extends Command {
/**
* Constructs a GetStatesCommand instance.
* @param {number} requestId - The unique identifier for the command request.
*/
constructor(requestId) {
super(requestId, "get_states");
}
/**
* Constructs a GetStatesCommand instance.
* @param {number} requestId - The unique identifier for the command request.
*/
constructor(requestId) {
super(requestId, 'get_states')
}
}
18 changes: 9 additions & 9 deletions src/modules/homeassistant/commands/subscribe-events-command.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import {Command} from "@/modules/homeassistant/commands/command";
import { Command } from '@/modules/homeassistant/commands/command'

/**
* The SubscribeEventCommand class, a subclass of Command, specifically handles
* subscription to event types in HomeAssistant.
*/
export class SubscribeEventsCommand extends Command {
/**
* Constructs a SubscribeEventCommand instance.
* @param {number} requestId - The unique identifier for the command request.
*/
constructor(requestId) {
super(requestId, "subscribe_events");
this.event_type = "state_changed";
}
/**
* Constructs a SubscribeEventCommand instance.
* @param {number} requestId - The unique identifier for the command request.
*/
constructor(requestId) {
super(requestId, 'subscribe_events')
this.event_type = 'state_changed'
}
}
Loading

0 comments on commit e3c396e

Please sign in to comment.