# CALinONE – Home Assistant example automations
#
# Prerequisite: the CALinONE integration is installed in Home Assistant
# (via HACS) and the webhook URL is entered in the app — see
# https://cal-in-one.app/homeassistant. Nothing needs to be created
# manually; the integration provides all entities itself:
#
#   calendar.calinone                        All shifts, vacations and appointments
#   sensor.calinone_current_shift            Today's shift (name) or "off"
#   sensor.calinone_shift_start              Shift start today, e.g. "06:00" (else "-")
#   sensor.calinone_shift_end                Shift end today, e.g. "14:00" (else "-")
#   sensor.calinone_shift_duration           Shift duration today in hours, e.g. 8.0
#   sensor.calinone_next_shift               Shift/vacation tomorrow (name) or "off"
#   sensor.calinone_next_appointment         Next appointment today (title) or "off"
#   sensor.calinone_appointment_category     Category of that appointment ("-"/"off")
#   binary_sensor.calinone_work_day          "on" = work day today (off on vacation days)
#   binary_sensor.calinone_vacation_today    "on" = vacation/absence today
#
# After each push from the app the integration fires the bus event
# "calinone_updated" — usable as an automation trigger.
#
# Copy the automations you want into your configuration.yaml (or store
# this file as a package) and adapt the entities (climate.living_room,
# light.bedroom, notify.mobile_app_your_phone) and the shift names to
# your CALinONE configuration.

automation:
  # --- Ramp up heating before shift start ---
  - id: "calinone_heating_before_shift"
    alias: "Heating before shift start"
    description: "Ramps up the heating when you're working today"
    trigger:
      - platform: time
        at: "05:00:00"
    condition:
      - condition: state
        entity_id: binary_sensor.calinone_work_day
        state: "on"
    action:
      - service: climate.set_temperature
        target:
          entity_id: climate.living_room
        data:
          temperature: 22

  # --- Turn lights off earlier when tomorrow is an early shift ---
  - id: "calinone_lights_early_shift"
    alias: "Lights off before early shift"
    description: "Bedroom light off earlier when tomorrow is an early shift"
    trigger:
      - platform: time
        at: "21:30:00"
    condition:
      - condition: state
        entity_id: sensor.calinone_next_shift
        state: "Early shift"   # exactly the shift name from CALinONE
    action:
      - service: light.turn_off
        target:
          entity_id: light.bedroom

  # --- Vacation: mute notifications ---
  - id: "calinone_vacation_mute"
    alias: "Vacation: mute notifications"
    description: "Muted when vacation starts"
    trigger:
      - platform: state
        entity_id: binary_sensor.calinone_vacation_today
        to: "on"
    action:
      - service: notify.mobile_app_your_phone
        data:
          title: "Vacation starts"
          message: "Notifications will be muted."
      # Uncomment if you have a notifications helper:
      # - service: input_boolean.turn_off
      #   target:
      #     entity_id: input_boolean.notifications_enabled

  # --- Adjust heating at shift start (uses the shift time) ---
  - id: "calinone_heating_shift_start"
    alias: "Heating at shift start"
    description: "Adjusts the heating when the shift begins"
    trigger:
      - platform: template
        value_template: >
          {% set t = states('sensor.calinone_shift_start') %}
          {{ t not in ['-', 'unknown', 'unavailable'] and now().strftime('%H:%M') == t }}
    condition:
      - condition: state
        entity_id: binary_sensor.calinone_work_day
        state: "on"
    action:
      - service: climate.set_temperature
        target:
          entity_id: climate.living_room
        data:
          temperature: 22

  # --- Lower heating after shift end ---
  - id: "calinone_heating_shift_end"
    alias: "Lower heating after shift end"
    description: "Lowers the heating when the shift ends"
    trigger:
      - platform: template
        value_template: >
          {% set t = states('sensor.calinone_shift_end') %}
          {{ t not in ['-', 'unknown', 'unavailable'] and now().strftime('%H:%M') == t }}
    condition:
      - condition: state
        entity_id: binary_sensor.calinone_work_day
        state: "on"
    action:
      - service: climate.set_temperature
        target:
          entity_id: climate.living_room
        data:
          temperature: 18

  # --- Reminder 30 minutes before the next appointment ---
  - id: "calinone_appointment_reminder"
    alias: "Appointment reminder"
    description: "Notifies 30 minutes before today's next appointment"
    trigger:
      - platform: template
        value_template: >
          {% set t = state_attr('sensor.calinone_next_appointment', 'time') %}
          {{ t not in ['-', none] and
             now().strftime('%H:%M') == (today_at(t) - timedelta(minutes=30)).strftime('%H:%M') }}
    action:
      - service: notify.mobile_app_your_phone
        data:
          title: "Appointment in 30 minutes"
          message: >
            {{ states('sensor.calinone_next_appointment') }}
            at {{ state_attr('sensor.calinone_next_appointment', 'time') }}

# ──────────────────────────────────────────────
# LOVELACE CARD (optional)
# ──────────────────────────────────────────────
# Add this card to your dashboard ("Edit card" → YAML mode):
#
# type: entities
# title: "Today's shift"
# entities:
#   - entity: sensor.calinone_current_shift
#     name: "Shift"
#   - entity: sensor.calinone_shift_start
#     name: "Start"
#   - entity: sensor.calinone_shift_end
#     name: "End"
#   - entity: sensor.calinone_shift_duration
#     name: "Duration"
#   - entity: sensor.calinone_next_shift
#     name: "Tomorrow"
#   - entity: sensor.calinone_next_appointment
#     name: "Next appointment"
#   - entity: binary_sensor.calinone_work_day
#     name: "Work day"
#   - entity: binary_sensor.calinone_vacation_today
#     name: "Vacation"
