#:########################################################################################:#
# PACKAGE: Potplant Moisture Alerts
# FILE: packages/potplant_moisture_alerts.yaml
# VERSION: v1.1.0 2026-07-29
#   - Preserve user-selected thresholds and master enable state across restarts.
#:########################################################################################:#
# PURPOSE:
#   Alert when potplant soil moisture drops below a configurable threshold.
#   Displays on the Lounge LED matrix and announces via Lounge Google Home voice.
#:########################################################################################:#
# SETTINGS:
#   - input_number.*_moisture_threshold: per-plant dry threshold (default 30%)
#   - input_boolean.potplant_alert_enabled: master enable toggle
#:########################################################################################:#
# DEPENDENCIES:
#   - packages/view_road_announcement_router.yaml for script.view_road_announcement
#   - sensor.table_plant_soil_moisture
#   - sensor.tv_plant_soil_moisture
#:########################################################################################:#

input_number:
  table_plant_moisture_threshold:
    name: Table Plant Moisture Threshold
    min: 0
    max: 100
    step: 5
    unit_of_measurement: "%"
    icon: mdi:water-percent

  tv_plant_moisture_threshold:
    name: TV Plant Moisture Threshold
    min: 0
    max: 100
    step: 5
    unit_of_measurement: "%"
    icon: mdi:water-percent

input_boolean:
  potplant_alert_enabled:
    name: Potplant Moisture Alerts
    icon: mdi:sprout

template:
  - binary_sensor:
      - name: "Table Plant Needs Water"
        unique_id: table_plant_needs_water
        icon: mdi:flower
        state: >-
          {{ (states('sensor.table_plant_soil_moisture') | float(100)) <
             (states('input_number.table_plant_moisture_threshold') | float(30)) }}

      - name: "TV Plant Needs Water"
        unique_id: tv_plant_needs_water
        icon: mdi:flower
        state: >-
          {{ (states('sensor.tv_plant_soil_moisture') | float(100)) <
             (states('input_number.tv_plant_moisture_threshold') | float(30)) }}

automation:
  ##############################################################################
  # TABLE PLANT
  ##############################################################################

  - id: potplant_table_plant_alert
    alias: "Potplant - Table Plant Needs Water"
    mode: single

    trigger:
      - trigger: state
        entity_id: binary_sensor.table_plant_needs_water
        to: "on"
      - trigger: time_pattern
        hours: "/2"
      - trigger: homeassistant
        event: start

    condition:
      - condition: state
        entity_id: input_boolean.potplant_alert_enabled
        state: "on"
      - condition: state
        entity_id: input_boolean.quiet_time
        state: "off"
      - condition: state
        entity_id: binary_sensor.table_plant_needs_water
        state: "on"

    action:
      - action: script.view_road_announcement
        data:
          channels: "led_matrix_1"
          title: "Plants"
          short_announcement: "Water Table Plant"
          indicator: "table,1,15,10,6,5,0,0"

      - action: script.view_road_announcement
        data:
          channels: "lounge_google_home_voice"
          title: "Plants"
          short_announcement: "Table plant needs water"

      - action: script.view_road_announcement
        data:
          channels: "sony_tv_lounge"
          title: "Plants Announcement"
          short_announcement: "Table plant needs water"

  - id: potplant_table_clear_led
    alias: "Potplant - Clear Table Plant LED"
    mode: single

    trigger:
      - trigger: state
        entity_id: binary_sensor.table_plant_needs_water
        to: "off"

    action:
      - action: mqtt.publish
        data:
          topic: "viewroad-commands/ledmatrix1/cancel_id"
          payload: "table"
          qos: 0
          retain: false

  ##############################################################################
  # TV PLANT
  ##############################################################################

  - id: potplant_tv_plant_alert
    alias: "Potplant - TV Plant Needs Water"
    mode: single

    trigger:
      - trigger: state
        entity_id: binary_sensor.tv_plant_needs_water
        to: "on"
      - trigger: time_pattern
        hours: "/2"
      - trigger: homeassistant
        event: start

    condition:
      - condition: state
        entity_id: input_boolean.potplant_alert_enabled
        state: "on"
      - condition: state
        entity_id: input_boolean.quiet_time
        state: "off"
      - condition: state
        entity_id: binary_sensor.tv_plant_needs_water
        state: "on"

    action:
      - action: script.view_road_announcement
        data:
          channels: "led_matrix_1"
          title: "Plants"
          short_announcement: "Water TV Plant"
          indicator: "tvplant,1,15,10,6,5,0,0"

      - action: script.view_road_announcement
        data:
          channels: "lounge_google_home_voice"
          title: "Plants"
          short_announcement: "TV plant needs water"

      - action: script.view_road_announcement
        data:
          channels: "sony_tv_lounge"
          title: "Plants Announcement"
          short_announcement: "TV plant needs water"

  - id: potplant_tv_clear_led
    alias: "Potplant - Clear TV Plant LED"
    mode: single

    trigger:
      - trigger: state
        entity_id: binary_sensor.tv_plant_needs_water
        to: "off"

    action:
      - action: mqtt.publish
        data:
          topic: "viewroad-commands/ledmatrix1/cancel_id"
          payload: "tvplant"
          qos: 0
          retain: false
