# Mode config
input_select:
  bedroom_mode:
    name: Bedroom mode
    options:
      - day
      - bed
      - night
  guest_bedroom_mode:
    name: Guest Bedroom mode
    options:
      - day
      - night

binary_sensor:
  - platform: template
    sensors:
      bedroom_mode:
        friendly_name: Bedroom mode
        value_template: "{{ states('input_select.bedroom_mode') }}"
        icon_template: >-

          {% if is_state('input_select.bedroom_mode', 'day') %}
            mdi:weather-sunny
          {% elif is_state('input_select.bedroom_mode', 'bed') %}
            mdi:bed-king-outline
          {% else %}
            mdi:weather-night
          {% endif %}

      someone_sleeping:
        friendly_name: Someone sleeping
        value_template: >-

          {% if is_state('input_boolean.guest_bedroom', 'off') %}
            {{ is_state('input_select.bedroom_mode', 'night') }}
          {% else %}
            {{ is_state('input_select.bedroom_mode', 'night') or is_state('input_select.guest_bedroom_mode', 'night') }}
          {% endif %}

sensor:
  - platform: template
    sensors:
      house_mode:
        friendly_name: House mode
        value_template: >-

          {% if is_state('input_boolean.guest_bedroom', 'off') %}
            {% if is_state('input_select.bedroom_mode', 'night') %}
              night
            {% else %}
              day
            {% endif %}
          {% else %}
            {% if (is_state('input_select.bedroom_mode', 'night') or is_state('input_select.bedroom_mode', 'bed'))
                and is_state('input_select.guest_bedroom_mode', 'night') %}
              night
            {% else %}
              day
            {% endif %}
          {% endif %}


script:
  toggle_bedroom_mode:
    alias: Toggle bedroom mode
    sequence:
      - service: input_select.select_option
        entity_id: input_select.bedroom_mode
        data_template:
          option: "{{ is_state('input_select.bedroom_mode', 'day') | iif('night', 'day') }}"

automation:
  - alias: Bedroom mode · Change
    trigger:
      platform: state
      entity_id: input_select.bedroom_mode
    action:
      - choose:
          - conditions: "{{ trigger.to_state.state == 'day' }}"
            sequence:
              - service: script.routine_bedroom_morning
          - conditions: "{{ trigger.to_state.state == 'bed' }}"
            sequence:
              - service: script.routine_bedroom_bedtime
          - conditions: "{{ trigger.to_state.state == 'night' }}"
            sequence:
              - service: script.bedroom_good_night

  - alias: Guest Bedroom mode · Set to night
    trigger:
      platform: state
      entity_id: input_select.guest_bedroom_mode
      to: "night"
    action:
      - service: light.turn_off
        entity_id: light.tradfri_large

  - alias: House mode · Change
    trigger:
      platform: state
      entity_id: sensor.house_mode
    action:
      choose:
        - conditions: "{{ trigger.to_state.state == 'day' }}"
          sequence:
            - service: script.routine_waking_up
        - conditions: "{{ trigger.to_state.state == 'night' }}"
          sequence:
            - service: script.routine_good_night
