github.com/e154/smart-home@v0.17.2-0.20240311175135-e530a6e5cd45/doc/content/en/docs/javascript/_index.md (about)

     1  
     2  ---
     3  title: "Javascript"
     4  linkTitle: "Javascript"
     5  date: 2021-11-19
     6  description: >
     7    
     8  ---
     9  
    10  The software logic and applications in the **Smart Home** project are developed using JavaScript, CoffeeScript, and TypeScript. JavaScript is a widely-used programming language that provides flexibility and power in creating interactive components.
    11  
    12  The **Smart Home** project utilizes JavaScript to write all the core components that control the smart home. This includes device management logic, data processing, interaction with external APIs, creating the user interface, and much more.
    13  
    14  In the **Smart Home** project, you can use JavaScript to create automation rules, schedule configurations, process data from sensors, control lighting, heating, security systems, and many other aspects of your smart home. JavaScript enables easy interaction with devices and systems and allows you to create user interfaces to control all the functions of your home.
    15  
    16  Thanks to the flexibility and popularity of JavaScript, you can easily customize and extend the **Smart Home** project according to your needs and preferences. You have the opportunity to use a wide range of JavaScript tools and libraries to adapt the project to your unique requirements and enhance its functionality.
    17  
    18  Regardless of your programming skills, JavaScript provides a powerful toolkit for developing software logic and applications in the **Smart Home** project.
    19  
    20  ### Scope and Visibility
    21  
    22  {{< figure src="/smart-home/img/scrips-scope.svg" >}}
    23  
    24  The script system for Smart Home provides a flexible and powerful environment for automation. However, it's essential to consider different scopes when designing the software architecture. There are five scopes in which scripts can execute:
    25  
    26  First, let's take a look at the big picture. We have five scopes where scripts can execute:
    27  
    28  * **Entity**
    29      * **General Scope**
    30          * Executes once when the entity starts:
    31              * `function init(): void;`
    32          * Executes the `entityAction` method when an action is performed, if not specified in the **Action Scope**:
    33              * `function entityAction(entityId: string, actionName: string, params: { [key: string]: any }): void;`
    34      * **Action Scope**
    35          * Executes the `entityAction` method when an action is performed:
    36              * `function entityAction(entityId: string, actionName: string, params: { [key: string]: any }): void;`
    37  * **Task**
    38      * **Trigger Scope**
    39          * Executes one of the following methods based on the trigger type and event:
    40              * `function automationTriggerStateChanged(msg: TriggerStateChangedMessage): boolean;`
    41              * `function automationTriggerAlexa(msg: TriggerAlexaTimeMessage): boolean;`
    42              * `function automationTriggerTime(msg: TriggerTimeMessage): boolean;`
    43              * `function automationTriggerSystem(msg: TriggerSystemMessage): boolean;`
    44      * **Condition Scope**
    45          * Executes the `automationCondition` method when the trigger is triggered:
    46              * `function automationCondition(entityId: string): boolean;`
    47      * **Action Scope**
    48          * Executes the `automationAction` method when `automationCondition` returns true:
    49              * `function automationAction(entityId: string): void;`
    50  
    51  ### Examples
    52  
    53  We wrote a script describing logic, event handling, and automation.
    54  
    55  ```javascript
    56  // Incorrect script implementation with three scopes
    57  
    58  // General scope
    59  // ##################################
    60  var init, entityAction, automationTriggerStateChanged;
    61  
    62  let entityState;
    63  
    64  // Entity - General Scope
    65  init = function () {
    66    // Entity initialization
    67    entityState = 'off';
    68  }
    69  
    70  // Action scope
    71  // ##################################
    72  
    73  entityAction = function (entityId, actionName, args) {
    74    console.log(entityState); // Incorrect
    75  };
    76  
    77  //  automation
    78  //  ##################################
    79  automationTriggerStateChanged = function (msg) {
    80    console.log(entityState); // Incorrect
    81  };
    82  ```
    83  
    84  ```javascript
    85  // Correct script implementation with three scopes
    86  
    87  // General scope
    88  // ##################################
    89  var init, entityAction, automationTriggerStateChanged;
    90  
    91  let entityState;
    92  let storageKey = entityState + ENTITY_ID;
    93  
    94  // Entity - General Scope
    95  init = function () {
    96    // Entity initialization
    97    entityState = 'off';
    98  
    99    Storage.push(storageKey, entityState);
   100  }
   101  
   102  // Action scope
   103  // ##################################
   104  
   105  entityAction = function (entityId, actionName, args) {
   106    const entityState = Storage.getByName(storageKey);
   107    console.log(entityState);
   108  };
   109  
   110  //  automation
   111  //  ##################################
   112  automationTriggerStateChanged = function (msg) {
   113    const entityState = Storage.getByName(storageKey);
   114    console.log(entityState);
   115  };
   116  ```