#:########################################################################################:#
# Package: Pet Feeding and Jobs Timing
# File:    pet_feeding_and_jobs.yaml
# Version: v1.1.0
# Date:    2026-07-21
#
# Purpose:
#   Provides legacy MQTT pet activity sensors and Jobs-derived timing sensors for
#   the Lounge dog-feeding and depooping gauges.
#
# Behaviour:
#   - Dog feeding reports the newest valid morning or evening completion time.
#   - Dog depooping reports the weekly Jobs completion time.
#   - Invalid or unrestored Jobs attributes make a timing sensor unavailable
#     instead of exposing a numeric sentinel value.
#
# Version History:
#   - v1.1.0 (2026-07-21): Reject invalid Jobs timing attributes and remove the
#     999999-hour fallback from the dog-feeding gauge source.
#   - v1.0.0 (2026-07-21): Added Jobs-derived dog timing sensors for P010.
#:########################################################################################:#

#:######################################################################################:#
# LEGACY MQTT PET ACTIVITY AND TIMING SENSORS
#:######################################################################################:#
mqtt:
  sensor:
    - unique_id: dog_fed_activity
      name: "Dog Fed"
      state_topic: "viewroad-status/activityfeed/dogfed"
      icon: mdi:dog
    - unique_id: depooped_activity
      name: "DePooped"
      state_topic: "viewroad-status/activityfeed/depooped"
      icon: mdi:emoticon-poop
    - unique_id: jobs_done_activity
      name: "Jobs Done"
      state_topic: "viewroad-status/activityfeed/jobsdone"
      icon: mdi:notebook
    - unique_id: hours_since_jobs_done
      name: "Hours Since Jobs Done"
      state_topic: "viewroad-tele/petfeeding/hoursSinceJobsDone"
      unit_of_measurement: "Hours"
    - unique_id: hours_since_zorro_fed
      name: "Hours Since Zorro Fed"
      state_topic: "viewroad-tele/petfeeding/hoursSinceDogFed"
      unit_of_measurement: "Hours"
    - unique_id: hours_since_depooped
      name: "Hours Since DePooped"
      state_topic: "viewroad-tele/petfeeding/hoursSinceDePooped"
      unit_of_measurement: "Hours"

#:######################################################################################:#
# JOBS-DERIVED LOUNGE GAUGE SENSORS
#:######################################################################################:#
template:
  - sensor:
      - name: "Jobs Tracker Hours Since Dog Fed"
        unique_id: "jobs_tracker_hours_since_dog_fed"
        device_class: duration
        state_class: measurement
        unit_of_measurement: "h"
        icon: "mdi:dog-side"
        availability: >-
          {% set morning = state_attr('sensor.jobs_tracker_dog_feeding_morning', 'hours_since_completed') %}
          {% set evening = state_attr('sensor.jobs_tracker_dog_feeding_evening', 'hours_since_completed') %}
          {{ is_number(morning) or is_number(evening) }}
        state: >-
          {% set morning = state_attr('sensor.jobs_tracker_dog_feeding_morning', 'hours_since_completed') %}
          {% set evening = state_attr('sensor.jobs_tracker_dog_feeding_evening', 'hours_since_completed') %}
          {% if is_number(morning) and is_number(evening) %}
            {{ [morning | float(0), evening | float(0)] | min | round(0) }}
          {% elif is_number(morning) %}
            {{ morning | float(0) | round(0) }}
          {% elif is_number(evening) %}
            {{ evening | float(0) | round(0) }}
          {% else %}
            {{ none }}
          {% endif %}

      - name: "Jobs Tracker Hours Since Dog Depooped"
        unique_id: "jobs_tracker_hours_since_dog_depooped"
        device_class: duration
        state_class: measurement
        unit_of_measurement: "h"
        icon: "mdi:emoticon-poop"
        availability: >-
          {% set hours = state_attr('sensor.jobs_tracker_dog_depooping', 'hours_since_completed') %}
          {{ is_number(hours) }}
        state: >-
          {% set hours = state_attr('sensor.jobs_tracker_dog_depooping', 'hours_since_completed') %}
          {{ hours | float(0) if is_number(hours) else none }}
