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

     1  ---
     2  title: "Entity"
     3  linkTitle: "entity"
     4  date: 2021-11-20
     5  description: >
     6  
     7  ---
     8  
     9  In the **Smart Home** project, the "Entity" object is the main unit and the primary object that stores the device's state and settings.
    10  
    11  An "Entity" represents an abstraction of a specific device in a smart home. It contains information about the device type, its identifier, current state, and other settings.
    12  
    13  The key attributes of an "Entity" include:
    14  
    15  1. Device Type: Determines the category or class of the device, such as "light fixture," "thermostat," "door lock," etc. This helps classify devices and define the operations and settings available to them.
    16  
    17  2. Identifier: A unique identifier assigned to the device, which allows it to be uniquely identified within the smart home system. This can be a numerical value, string, or other identification format.
    18  
    19  3. Device State: Contains information about the current state of the device, such as "on/off," "open/closed," "temperature," etc. The state can change as a result of operations or events related to the device.
    20  
    21  4. Device Settings: Includes various parameters and settings related to the specific device. For example, these could be brightness settings for a light fixture, preferred temperature for a thermostat, etc.
    22  
    23  The "Entity" serves as the main device abstraction in the **Smart Home** project and provides a unified interface for working with different types of devices. This allows convenient management of device states and settings, as well as the creation of flexible automation scenarios and interactions between devices in your smart home.
    24  
    25  ### Entity Object Properties
    26  ```coffeescript
    27  {
    28    "id": "sensor.device1",
    29    "type": "sensor",
    30    "name": "",
    31    "description": "device description",
    32    "icon": null,
    33    "image_url": null,
    34    "actions": [
    35      {
    36        "name": "ENABLE",
    37        "description": "enable",
    38        "image_url": null,
    39        "icon": null
    40      },
    41      {
    42        "name": "DISABLE",
    43        "description": "disable",
    44        "image_url": null,
    45        "icon": null
    46      }
    47    ],
    48    "states": [
    49      {
    50        "name": "ENABLED",
    51        "description": "enabled state",
    52        "image_url": null,
    53        "icon": null
    54      },
    55      {
    56        "name": "DISABLED",
    57        "description": "disabled state",
    58        "image_url": null,
    59        "icon": null
    60      }
    61    ],
    62    "state": {
    63      "name": "ENABLED",
    64      "description": "enabled state",
    65      "image_url": null,
    66      "icon": null
    67    },
    68    "attributes": {},
    69    "area": null,
    70    "metrics": [],
    71    "hidden": false
    72  }
    73  ```
    74  
    75  ### Entity Object Methods
    76  
    77  ```coffeescript
    78  EntitySetState(ENTITY_ID, attr)
    79  EntitySetStateName(ENTITY_ID, stateName)
    80  EntityGetState(ENTITY_ID)
    81  EntitySetAttributes(ENTITY_ID, attr)
    82  EntitySetMetric(ENTITY_ID, name, value)
    83  EntityCallAction(ENTITY_ID, action, args)
    84  EntityCallScene(ENTITY_ID, args)
    85  EntityGetSettings(ENTITY_ID)
    86  EntityGetAttributes(ENTITY_ID)
    87  ```
    88  
    89  The abstract "Entity" object in the **Smart Home** project provides the following methods:
    90  
    91  1. `EntitySetState(ENTITY_ID, attr)`: This method is used to set the state of a specific entity (`ENTITY_ID`) by providing a set of attributes (`attr`). It likely allows you to update the state of an entity with new attribute values.
    92  
    93      ```javascript
    94      const attrs = {
    95          foo: bar,
    96      }
    97      const stateName = 'connected'
    98      EntitySetState(ENTITY_ID, {
    99          new_state: stateName,
   100          attribute_values: attrs,
   101          storage_save: true
   102      });
   103      ```
   104  
   105  2. `EntitySetStateName(ENTITY_ID, stateName)`: This method sets the state of an entity (`ENTITY_ID`) to a specific named state (`stateName`). It's used when you want to change the state of an entity to a predefined state.
   106  
   107      ```javascript
   108      const stateName = 'connected'
   109      EntitySetStateName(ENTITY_ID, stateName);
   110      ```
   111  
   112  3. `EntityGetState(ENTITY_ID)`: This method retrieves the current state of a specified entity (`ENTITY_ID`). It allows you to query the current state of the entity.
   113  
   114      ```javascript
   115    
   116      const currentState = EntityGetState(ENTITY_ID);
   117      print(marshal(homeState))
   118      // out: {"name":"connected","description":"connected","image_url":null,"icon":null}
   119      ```
   120  
   121  4. `EntitySetAttributes(ENTITY_ID, attr)`: Similar to `EntitySetState`, this method sets attributes for a specific entity (`ENTITY_ID`). It may be used when you want to update attributes without changing the state.
   122  
   123      ```javascript
   124      const attrs = {
   125          foo: bar,
   126      }
   127      const stateName = 'connected'
   128      EntitySetAttributes(ENTITY_ID, attrs);
   129      ```
   130  
   131  5. `EntitySetMetric(ENTITY_ID, name, value)`: This method sets a metric for the specified entity (`ENTITY_ID`). Metrics are often used to measure and record various aspects of an entity's behavior or performance.
   132  
   133      ```javascript
   134      const attrs = {
   135          foo: bar,
   136      }
   137      const metricName = 'counter'
   138      EntitySetMetric(ENTITY_ID, name, attrs);
   139      ```
   140  
   141  6. `EntityCallAction(ENTITY_ID, action, args)`: This method is used to trigger or call a specific action (`action`) associated with the entity (`ENTITY_ID`) and provides any necessary arguments (`args`) for that action.
   142  
   143      ```javascript
   144      const attrs = {
   145          foo: bar,
   146      }
   147      const action = 'ENABLE'
   148      EntityCallAction(ENTITY_ID, action, attrs);
   149      ```
   150  
   151  7. `EntityCallScene(ENTITY_ID, args)`: Similar to `EntityCallAction`, this method is used to call or trigger a scene associated with the entity (`ENTITY_ID`) and provides any required arguments (`args`) for that scene.
   152  
   153      ```javascript
   154      const attrs = {
   155          foo: bar,
   156      }
   157      EntityCallScene(ENTITY_ID, attrs);
   158      ```
   159  
   160  8. `EntityGetSettings(ENTITY_ID)`: This method retrieves the settings or configuration for the specified entity (`ENTITY_ID`). It allows you to access and query the settings associated with an entity.
   161  
   162      ```javascript
   163      const settings = EntityGetSettings(ENTITY_ID);
   164      print(marshal(settings))
   165      // out: {"username":"zigbee2mqtt","cleanSession":false,"keepAlive":15,"direction":"in","topics":"owntracks/#","pingTimeout":10,"connectTimeout":30,"qos":0}
   166  
   167      ```
   168  
   169  9. `EntityGetAttributes(ENTITY_ID)`: This method retrieves all attributes associated with a specific entity (`ENTITY_ID`). It provides a way to obtain detailed information about the entity's attributes.
   170  
   171      ```javascript
   172      const attrs = EntityGetAttributes(ENTITY_ID);
   173      print(marshal(attrs))
   174      // out: {"username":"zigbee2mqtt","cleanSession":false,"keepAlive":15,"direction":"in","topics":"owntracks/#","pingTimeout":10,"connectTimeout":30,"qos":0}
   175  
   176      ```
   177  
   178  The methods of the "Entity" object provide convenient capabilities for managing the device's state, attributes, metrics, and executing actions on the device. You can use these methods to create device control logic in your **Smart Home** project.
   179