# virtualentity_devices.yaml
#
# Copyright (C) 2022 Kedron Holdings LLC, All Rights Reserved
# This file is part of Reactor.
#
# *** IMPORTANT ***
# This is a system file. DO NOT place overrides or make modifications here.
---
version: 22280
revision: 1
format: 1

templates:
  "Binary Switch":
    type: Switch
    capabilities:
      - power_switch
      - toggle
    primary_attribute: power_switch.state
  "Dimmer":
    type: Light
    capabilities:
      - dimming
      - power_switch
      - toggle
    primary_attribute: power_switch.state
  "Binary Sensor":
    type: BinarySensor
    capabilities: [binary_sensor]
    primary_attribute: binary_sensor.state
  "Value Sensor":
    type: ValueSensor
    capabilities: [value_sensor]
    primary_attribute: value_sensor.value
  "String Sensor":
    type: ValueSensor
    capabilities: [string_sensor]
    primary_attribute: string_sensor.value

implementation:
  x_virtualentity_sys:
    actions: {}  # all actions are defined with handlers in VirtualDeviceController
  x_virtualentity:
    actions: {}  # all actions are defined with handlers in VirtualDeviceController

  power_switch:
    actions:
      'on':
        - target: state
          value: true
        - if_expr: "! isnull( entity.attributes.dimming )"
          target: dimming.level
          expr: "entity.attributes.dimming.level > 0 ? entity.attributes.dimming.level : 1"
      'off':
        - target: state
          value: false
        - if_expr: "! isnull( entity.attributes.dimming )"
          target: dimming.level
          value: 0
      set:
        - target: state
          expr: bool( parameters.state )
        - if_expr: "! isnull( entity.attributes.dimming )"
          target: dimming.level
          expr: >
            bool( parameters.state ) ? (entity.attributes.dimming.level > 0 ? entity.attributes.dimming.level : 1) : 0
  toggle:
    actions:
      toggle:
        remap:
          - condition: entity.attributes.power_switch.state
            action: power_switch.off
          - action: power_switch.on
  dimming:
    actions:
      set:
        - target: level
          expr: "min( 1, max( 0, parameters.level ?# 0 ) )"
        - target: power_switch.state
          expr: entity.attributes.dimming.level > 0
      up:
        - target: level
          expr: "min( 1, entity.attributes.dimming.level + ( parameters.amount ?# entity.attributes.dimming.step ) )"
        - target: power_switch.state
          expr: entity.attributes.dimming.level > 0
      down:
        - target: level
          expr: "max( 0, entity.attributes.dimming.level - ( parameters.amount ?# entity.attributes.dimming.step ) )"
        - target: power_switch.state
          expr: entity.attributes.dimming.level > 0
  binary_sensor:
    attributes: {}
    actions: {}
  value_sensor:
    attributes: {}
    actions: {}
  string_sensor:
    attributes: {}
    actions: {}
