{ config, ... }: {

  imports = [ ./mpd.nix ];

  services.homeAssistantConfig = {

    sensor = [{
      platform = "time_date";
      display_options = [ "time" "date" ];
    }];

    input_datetime = {
      wakeup = {
        name = "Arbeitswecker";
        has_time = true;
        has_date = false;
        icon = "mdi:alarm";
      };
      leave = {
        name = "Turn off Time";
        has_time = true;
        has_date = false;
        icon = "mdi:alarm";
      };
      return = {
        name = "Return home";
        has_time = true;
        has_date = false;
        icon = "mdi:alarm";
      };
      sleep = {
        name = "Turn off Time";
        has_time = true;
        has_date = false;
        icon = "mdi:alarm";
      };
    };

    input_boolean = {
      wakeup = {
        name = "enable";
        icon = "mdi:toggle-switch";
      };
      leave = {
        name = "enable";
        icon = "mdi:toggle-switch";
      };
      return = {
        name = "enable";
        icon = "mdi:toggle-switch";
      };
      sleep = {
        name = "enable";
        icon = "mdi:toggle-switch";
      };
    };

    input_select = {
      wakeup = {
        name = "Playlist";
        icon = "mdi:library-music";
        options = [ "wakeup" "wakeup1" "wakeup2" ];
      };
    };

    binary_sensor = [
      {
        platform = "tod";
        name = "night";
        after = "sunset";
        before = "sunrise";
      }
      {
        platform = "tod";
        name = "daytime";
        after = "sunrise";
        before = "sunset";
      }
    ];

    group = {

      timer_wakeup = {
        view = false;
        name = "Arbeits Aufwachen";
        control = "hidden";
        entities = [
          "input_boolean.wakeup"
          "input_datetime.wakeup"
          "input_select.wakeup"
        ];
      };

      timer_leave = {
        view = false;
        name = "Leave Time";
        control = "hidden";
        entities = [ "input_boolean.leave" "input_datetime.leave" ];
      };

      timer_return = {
        view = false;
        name = "Nach Hause kommen";
        control = "hidden";
        entities = [ "input_boolean.return" "input_datetime.return" ];
      };

      timer_sleep = {
        view = false;
        name = "Einschlafen";
        control = "hidden";
        entities = [ "input_boolean.sleep" "input_datetime.sleep" ];
      };

      timers.entities = [
        "group.timer_wakeup"
        "group.timer_leave"
        "group.timer_return"
        "group.timer_sleep"
        "binary_sensor.night"
        "binary_sensor.daytime"
      ];

      today.entities = [ "sensor.date" "sensor.time" ];

      view_overview.entities = [
        "group.timer_wakeup"
        "group.timer_leave"
        "group.timer_return"
        "group.timer_sleep"
      ];
    };

    automation = [
      {
        alias = "Wecker Arbeiten";
        trigger = {
          platform = "template";
          value_template =
            "{{ states('sensor.time') == (states.input_datetime.wakeup.attributes.timestamp | int | timestamp_custom('%H:%M', False)) }}";
        };
        condition = {
          condition = "and";
          conditions = [
            {
              condition = "state";
              entity_id = "input_boolean.wakeup";
              state = "on";
            }
            {
              condition = "state";
              entity_id = "binary_sensor.workday";
              state = "on";
            }
          ];
        };
        action = [
          {
            alias = "Play wakeup list";
            service = "media_player.play_media";
            data_template = {
              entity_id = "media_player.mpd";
              media_content_type = "playlist";
              media_content_id = "{{ states('input_select.wakeup') }}";
            };
          }
          {
            alias = "turn all on";
            service = "script.turn_on";
            entity_id = "script.turn_all_on";
          }
        ];
      }

      {
        alias = "Leave Turn all off Timer";
        trigger = {
          platform = "template";
          value_template =
            "{{ states('sensor.time') == (states.input_datetime.leave.attributes.timestamp | int | timestamp_custom('%H:%M', False)) }}";
        };
        condition = {
          condition = "and";
          conditions = [{
            condition = "state";
            entity_id = "input_boolean.leave";
            state = "on";
          }];
        };
        action = [{
          alias = "turn all off";
          service = "script.turn_on";
          entity_id = "script.turn_all_off";
        }];
      }

      {
        alias = "Return to Home";
        trigger = {
          platform = "template";
          value_template =
            "{{ states('sensor.time') == (states.input_datetime.return.attributes.timestamp | int | timestamp_custom('%H:%M', False)) }}";
        };
        condition = {
          condition = "and";
          conditions = [{
            condition = "state";
            entity_id = "input_boolean.return";
            state = "on";
          }];
        };
        action = [{
          alias = "turn all on";
          service = "script.turn_on";
          entity_id = "script.turn_all_on";
        }];
      }

      {
        alias = "Sleep Turn all off Timer";
        trigger = {
          platform = "template";
          value_template =
            "{{ states('sensor.time') == (states.input_datetime.sleep.attributes.timestamp | int | timestamp_custom('%H:%M', False)) }}";
        };
        condition = {
          condition = "and";
          conditions = [{
            condition = "state";
            entity_id = "input_boolean.sleep";
            state = "on";
          }];
        };
        action = [{
          alias = "turn all off";
          service = "script.turn_on";
          entity_id = "script.turn_all_off";
        }];
      }

    ];
  };

}