# https://www.home-assistant.io/integrations/light
# https://www.home-assistant.io/integrations/light.group

# https://www.home-assistant.io/integrations/yeelight
yeelight:
  devices:
    !secret yeelight_bedside_ip:
      name: Bedside light

# https://www.home-assistant.io/integrations/tuya
tuya:
  username: !secret tuya_username
  password: !secret tuya_password
  country_code: bg

light:
  - platform: mqtt
    name: "PC"
    availability_topic: "tele/pc_light/LWT"
    command_topic: "cmnd/pc_light/POWER"
    state_topic: "tele/pc_light/STATE"
    state_value_template: "{{ value_json.POWER }}"
    brightness_command_topic: "cmnd/pc_light/Dimmer"
    brightness_state_topic: "tele/pc_light/STATE"
    brightness_scale: 100
    on_command_type: "brightness"
    brightness_value_template: "{{ value_json.Dimmer }}"
    hs_command_topic: "cmnd/pc_light/HSBColor"
    hs_state_topic: "tele/pc_light/STATE"
    hs_value_template: "{{ value_json.HSBColor.split(',')[0:2]|join(',') }}"
    color_temp_command_topic: "cmnd/pc_light/CT"
    color_temp_state_topic: "tele/pc_light/STATE"
    color_temp_value_template: "{{ value_json.CT }}"
    rgb_command_topic: "cmnd/pc_light/Color"
    rgb_state_topic: "tele/pc_light/STATE"
    rgb_value_template: "{{ value_json.Color.split(',')[0:3]|join(',') }}"
    payload_on: "ON"
    payload_off: "OFF"
    payload_available: "Online"
    payload_not_available: "Offline"
    qos: 1
    retain: false

  - platform: mqtt
    name: "light_le_1"
    availability_topic: "tele/light_le_1/LWT"
    command_topic: "cmnd/light_le_1/POWER"
    state_topic: "tele/light_le_1/STATE"
    state_value_template: "{{ value_json.POWER }}"
    brightness_command_topic: "cmnd/light_le_1/Dimmer"
    brightness_state_topic: "tele/light_le_1/STATE"
    brightness_scale: 100
    on_command_type: "brightness"
    brightness_value_template: "{{ value_json.Dimmer }}"
    color_temp_command_topic: "cmnd/light_le_1/CT"
    color_temp_state_topic: "tele/light_le_1/STATE"
    color_temp_value_template: "{{ value_json.CT }}"
    payload_on: "ON"
    payload_off: "OFF"
    payload_available: "Online"
    payload_not_available: "Offline"
    qos: 1
    retain: false

  - platform: mqtt
    name: "light_le_2"
    availability_topic: "tele/light_le_2/LWT"
    command_topic: "cmnd/light_le_2/POWER"
    state_topic: "tele/light_le_2/STATE"
    state_value_template: "{{ value_json.POWER }}"
    brightness_command_topic: "cmnd/light_le_2/Dimmer"
    brightness_state_topic: "tele/light_le_2/STATE"
    brightness_scale: 100
    on_command_type: "brightness"
    brightness_value_template: "{{ value_json.Dimmer }}"
    color_temp_command_topic: "cmnd/light_le_2/CT"
    color_temp_state_topic: "tele/light_le_2/STATE"
    color_temp_value_template: "{{ value_json.CT }}"
    payload_on: "ON"
    payload_off: "OFF"
    payload_available: "Online"
    payload_not_available: "Offline"
    qos: 1
    retain: false

  - platform: mqtt
    name: "light_le_3"
    availability_topic: "tele/light_le_3/LWT"
    command_topic: "cmnd/light_le_3/POWER"
    state_topic: "tele/light_le_3/STATE"
    state_value_template: "{{ value_json.POWER }}"
    brightness_command_topic: "cmnd/light_le_3/Dimmer"
    brightness_state_topic: "tele/light_le_3/STATE"
    brightness_scale: 100
    on_command_type: "brightness"
    brightness_value_template: "{{ value_json.Dimmer }}"
    color_temp_command_topic: "cmnd/light_le_3/CT"
    color_temp_state_topic: "tele/light_le_3/STATE"
    color_temp_value_template: "{{ value_json.CT }}"
    payload_on: "ON"
    payload_off: "OFF"
    payload_available: "Online"
    payload_not_available: "Offline"
    qos: 1
    retain: false

  - platform: mqtt
    name: "light_le_4"
    availability_topic: "tele/light_le_4/LWT"
    command_topic: "cmnd/light_le_4/POWER"
    state_topic: "tele/light_le_4/STATE"
    state_value_template: "{{ value_json.POWER }}"
    brightness_command_topic: "cmnd/light_le_4/Dimmer"
    brightness_state_topic: "tele/light_le_4/STATE"
    brightness_scale: 100
    on_command_type: "brightness"
    brightness_value_template: "{{ value_json.Dimmer }}"
    color_temp_command_topic: "cmnd/light_le_4/CT"
    color_temp_state_topic: "tele/light_le_4/STATE"
    color_temp_value_template: "{{ value_json.CT }}"
    payload_on: "ON"
    payload_off: "OFF"
    payload_available: "Online"
    payload_not_available: "Offline"
    qos: 1
    retain: false

  - platform: mqtt
    name: "light_lohas_1"
    availability_topic: "tele/light_lohas_1/LWT"
    command_topic: "cmnd/light_lohas_1/POWER"
    state_topic: "tele/light_lohas_1/STATE"
    state_value_template: "{{ value_json.POWER }}"
    brightness_command_topic: "cmnd/light_lohas_1/Dimmer"
    brightness_state_topic: "tele/light_lohas_1/STATE"
    brightness_scale: 100
    on_command_type: "brightness"
    brightness_value_template: "{{ value_json.Dimmer }}"
    hs_command_topic: "cmnd/light_lohas_1/HSBColor"
    hs_state_topic: "tele/light_lohas_1/STATE"
    hs_value_template: "{{ value_json.HSBColor.split(',')[0:2]|join(',') }}"
    color_temp_command_topic: "cmnd/light_lohas_1/CT"
    color_temp_state_topic: "tele/light_lohas_1/STATE"
    color_temp_value_template: "{{ value_json.CT }}"
    rgb_command_topic: "cmnd/light_lohas_1/Color"
    rgb_state_topic: "tele/pc_light/STATE"
    rgb_value_template: "{{ value_json.Color.split(',')[0:3]|join(',') }}"
    payload_on: "ON"
    payload_off: "OFF"
    payload_available: "Online"
    payload_not_available: "Offline"
    qos: 1
    retain: false

  - platform: mqtt
    name: "light_lohas_2"
    availability_topic: "tele/light_lohas_2/LWT"
    command_topic: "cmnd/light_lohas_2/POWER"
    state_topic: "tele/light_lohas_2/STATE"
    state_value_template: "{{ value_json.POWER }}"
    brightness_command_topic: "cmnd/light_lohas_2/Dimmer"
    brightness_state_topic: "tele/light_lohas_2/STATE"
    brightness_scale: 100
    on_command_type: "brightness"
    brightness_value_template: "{{ value_json.Dimmer }}"
    hs_command_topic: "cmnd/light_lohas_2/HSBColor"
    hs_state_topic: "tele/light_lohas_2/STATE"
    hs_value_template: "{{ value_json.HSBColor.split(',')[0:2]|join(',') }}"
    color_temp_command_topic: "cmnd/light_lohas_2/CT"
    color_temp_state_topic: "tele/light_lohas_2/STATE"
    color_temp_value_template: "{{ value_json.CT }}"
    rgb_command_topic: "cmnd/light_lohas_2/Color"
    rgb_state_topic: "tele/pc_light/STATE"
    rgb_value_template: "{{ value_json.Color.split(',')[0:3]|join(',') }}"
    payload_on: "ON"
    payload_off: "OFF"
    payload_available: "Online"
    payload_not_available: "Offline"
    qos: 1
    retain: false

  - platform: group
    name: Living room main lights
    entities:
      - light.light_lohas_1
      - light.light_lohas_2
      - light.light_le_9
      - light.light_le_10

  - platform: group
    name: Living room all lights
    entities:
      - light.living_room_main_lights
      - light.pc

  - platform: group
    name: Master bedroom main lights
    entities:
      - light.light_le_1
      - light.light_le_2
      - light.light_le_3
      - light.light_le_4

  - platform: group
    name: Master bedroom all lights
    entities:
      - light.master_bedroom_main_lights
      - light.bedside_light

  - platform: group
    name: Kitchen main lights
    entities:
      - light.light_le_11
      - light.light_le_12
      - light.light_orbecco_1

  - platform: group
    name: Hallway entrance lights
    entities:
      - light.light_le_5
      - light.light_le_6

  - platform: group
    name: Hallway main lights
    entities:
      - light.light_le_7
      - light.light_le_8

  - platform: group
    name: Hallway all lights
    entities:
      - light.hallway_entrance_lights
      - light.hallway_main_lights

  - platform: group
    name: All lights
    entities:
      - light.living_room_all_lights
      - light.master_bedroom_all_lights
      - light.kitchen_main_lights
      - light.hallway_all_lights

sensor:
  - platform: mqtt
    name: "PC light WiFi"
    availability_topic: "tele/pc_light/LWT"
    device_class: signal_strength
    payload_available: "Online"
    payload_not_available: "Offline"
    state_topic: "tele/pc_light/STATE"
    unit_of_measurement: "%"
    value_template: "{{ value_json['Wifi'].RSSI }}"

  - platform: mqtt
    name: "light_le_1_wifi"
    availability_topic: "tele/light_le_1/LWT"
    device_class: signal_strength
    payload_available: "Online"
    payload_not_available: "Offline"
    state_topic: "tele/light_le_1/STATE"
    unit_of_measurement: "%"
    value_template: "{{ value_json['Wifi'].RSSI }}"

  - platform: mqtt
    name: "light_le_2_wifi"
    availability_topic: "tele/light_le_2/LWT"
    device_class: signal_strength
    payload_available: "Online"
    payload_not_available: "Offline"
    state_topic: "tele/light_le_2/STATE"
    unit_of_measurement: "%"
    value_template: "{{ value_json['Wifi'].RSSI }}"

  - platform: mqtt
    name: "light_le_3_wifi"
    availability_topic: "tele/light_le_3/LWT"
    device_class: signal_strength
    payload_available: "Online"
    payload_not_available: "Offline"
    state_topic: "tele/light_le_3/STATE"
    unit_of_measurement: "%"
    value_template: "{{ value_json['Wifi'].RSSI }}"

  - platform: mqtt
    name: "light_le_4_wifi"
    availability_topic: "tele/light_le_4/LWT"
    device_class: signal_strength
    payload_available: "Online"
    payload_not_available: "Offline"
    state_topic: "tele/light_le_4/STATE"
    unit_of_measurement: "%"
    value_template: "{{ value_json['Wifi'].RSSI }}"

  - platform: mqtt
    name: "light_lohas_1_wifi"
    availability_topic: "tele/light_lohas_1/LWT"
    device_class: signal_strength
    payload_available: "Online"
    payload_not_available: "Offline"
    state_topic: "tele/light_lohas_1/STATE"
    unit_of_measurement: "%"
    value_template: "{{ value_json['Wifi'].RSSI }}"

  - platform: mqtt
    name: "light_lohas_2_wifi"
    availability_topic: "tele/light_lohas_2/LWT"
    device_class: signal_strength
    payload_available: "Online"
    payload_not_available: "Offline"
    state_topic: "tele/light_lohas_2/STATE"
    unit_of_measurement: "%"
    value_template: "{{ value_json['Wifi'].RSSI }}"

switch:
  - platform: mqtt
    name: "PC Light Fade"
    availability_topic: "tele/pc_light/LWT"
    command_topic: "cmnd/pc_light/FADE"
    icon: mdi:gradient
    payload_available: "Online"
    payload_not_available: "Offline"
    qos: 1
    state_topic: "stat/pc_light/RESULT"
    value_template: "{{ value_json.Fade }}"

  - platform: mqtt
    name: "light_le_1_fade"
    availability_topic: "tele/light_le_1/LWT"
    command_topic: "cmnd/light_le_1/FADE"
    icon: mdi:gradient
    payload_available: "Online"
    payload_not_available: "Offline"
    qos: 1
    state_topic: "stat/light_le_1/RESULT"
    value_template: "{{ value_json.Fade }}"

  - platform: mqtt
    name: "light_le_2_fade"
    availability_topic: "tele/light_le_2/LWT"
    command_topic: "cmnd/light_le_2/FADE"
    icon: mdi:gradient
    payload_available: "Online"
    payload_not_available: "Offline"
    qos: 1
    state_topic: "stat/light_le_2/RESULT"
    value_template: "{{ value_json.Fade }}"

  - platform: mqtt
    name: "light_le_3_fade"
    availability_topic: "tele/light_le_3/LWT"
    command_topic: "cmnd/light_le_3/FADE"
    icon: mdi:gradient
    payload_available: "Online"
    payload_not_available: "Offline"
    qos: 1
    state_topic: "stat/light_le_3/RESULT"
    value_template: "{{ value_json.Fade }}"

  - platform: mqtt
    name: "light_le_4_fade"
    availability_topic: "tele/light_le_4/LWT"
    command_topic: "cmnd/light_le_4/FADE"
    icon: mdi:gradient
    payload_available: "Online"
    payload_not_available: "Offline"
    qos: 1
    state_topic: "stat/light_le_4/RESULT"
    value_template: "{{ value_json.Fade }}"

  - platform: mqtt
    name: "light_lohas_1_fade"
    availability_topic: "tele/light_lohas_1/LWT"
    command_topic: "cmnd/light_lohas_1/FADE"
    icon: mdi:gradient
    payload_available: "Online"
    payload_not_available: "Offline"
    qos: 1
    state_topic: "stat/light_lohas_1/RESULT"
    value_template: "{{ value_json.Fade }}"

  - platform: mqtt
    name: "light_lohas_2_fade"
    availability_topic: "tele/light_lohas_2/LWT"
    command_topic: "cmnd/light_lohas_2/FADE"
    icon: mdi:gradient
    payload_available: "Online"
    payload_not_available: "Offline"
    qos: 1
    state_topic: "stat/light_lohas_2/RESULT"
    value_template: "{{ value_json.Fade }}"

automation:
  - alias: Lighting · Turn on all lights after sunset
    trigger:
      platform: sun
      event: sunset
      offset: "+00:00:00"
    condition:
      - condition: state
        entity_id: binary_sensor.georgi_home
        state: "on"
    action:
      - service: light.turn_on
        entity_id: light.all_lights

  - alias: Lighting · Turn off all lights after sunrise
    trigger:
      platform: sun
      event: sunrise
      offset: "+00:00:00"
    action:
      - service: light.turn_off
        entity_id: light.all_lights

  - alias: Lighting · Master bedroom · Toggle bedside light on bedisde switch single click"
    trigger:
      platform: event
      event_type: zha_event
      event_data:
        device_ieee: !secret master_bedroom_bedside_switch_ieee
        command: single
    action:
      - service: light.toggle
        entity_id: light.bedside_light

  - alias: Lighting · Living room · Turn all lights on on Magic Cube flip 90
    trigger:
      platform: event
      event_type: zha_event
      event_data:
        device_ieee: !secret magic_cube_ieee
        command: flip
        args:
          flip_degrees: 90
    action:
      - service: light.turn_on
        entity_id: light.living_room_all_lights

  - alias: Lighting · Living room · Turn all lights off on Magic Cube flip 180
    trigger:
      platform: event
      event_type: zha_event
      event_data:
        device_ieee: !secret magic_cube_ieee
        command: flip
        args:
          flip_degrees: 180
    action:
      - service: light.turn_off
        entity_id: light.living_room_all_lights

  - alias: Lighting · Sync Tasmota states on start-up
    initial_state: true
    trigger:
      platform: homeassistant
      event: start
    action:
      - service: mqtt.publish
        data:
          topic: "cmnd/tasmotas/STATE"
          payload: ""
