Can I write an automation to reload/restart an integration?

The key is service: homeassistant.reload_config_entry

2 Likes

@Snille

Would not recommend this one for Tractive, as this generated a lot of API calls and I have ended up limited by them, had to pause everything for at least a week, nevertheless you might be able to use this for other purposes or just to have an idea.

Basically checking for feature unavailability, with conditions like having network communication with router and DNS and my router was not restarted manually, then checking for HA restart with input booleans (those are updated at the time of HA shutdown or Startup)

Below is the automation from where I call a script:

alias: Auto-recover Tractive integration
description: ""
trigger:
  - platform: state
    entity_id:
      - switch.maya_live_tracking
    to: unavailable
    for:
      hours: 0
      minutes: 3
      seconds: 0
  - platform: state
    entity_id:
      - switch.maya_live_tracking
    to: unknown
    for:
      hours: 0
      minutes: 3
      seconds: 0
  - platform: state
    entity_id:
      - switch.maya_tracker_led
    to: unavailable
    for:
      hours: 0
      minutes: 3
      seconds: 0
  - platform: state
    entity_id:
      - switch.maya_tracker_led
    to: unknown
    for:
      hours: 0
      minutes: 3
      seconds: 0
condition:
  - condition: state
    entity_id: binary_sensor.192_168_69_1
    state: "on"
    for:
      hours: 0
      minutes: 5
      seconds: 0
  - condition: state
    entity_id: binary_sensor.1_1_1_1
    for:
      hours: 0
      minutes: 5
      seconds: 0
    state: "on"
    enabled: true
  - condition: state
    entity_id: script.router_restart
    state: "off"
    for:
      hours: 0
      minutes: 5
      seconds: 0
    enabled: true
  - condition: state
    entity_id: input_boolean.ha_start
    state: "off"
    for:
      hours: 0
      minutes: 10
      seconds: 0
  - condition: state
    entity_id: input_boolean.ha_stop
    state: "off"
    for:
      hours: 0
      minutes: 10
      seconds: 0
action:
  - parallel:
      - service: script.fix_tractive_integration
        data: {}
      - service: notify.mobile_app_xel_s23
        metadata: {}
        data:
          message: Tractive integration reloaded
          title: HA debug message
  - delay:
      hours: 0
      minutes: 5
      seconds: 0
      milliseconds: 0
mode: single

And below the script being called, this device “xa3cs9fbf047b5b03f995867985709f6” is my Tractive tracker device id (changed some numbers in it before sharing to avoid funny business). The “activitylogger” part can be skipped, this is just a way for me to log activities and events on one of my dashboards.

alias: Fix Tractive integration
sequence:
  - parallel:
      - service: input_button.press
        data: {}
        target:
          entity_id: input_button.activitylogger
      - service: homeassistant.reload_config_entry
        data: {}
        target:
          device_id: xa3cs9fbf047b5b03f995867985709f6
        enabled: true
mode: single
icon: mdi:dog

I’m sure there are bettery ways to do this, but this is what I’ve come up with for my needs. I’m using the same logic to handle some of my other integrations which occasionally need restart, like Sonoff or Xiaomi Miot Auto and helps a lot to avoid manual effort.

Really appreciate you posting the YAML here. My Nexia integration for my new American Standard HVAC keeps dropping and needed a way to restart it. I was able to use the code here to hack something together.

alias: Reload Nexia when Unavailable
description: ""
trigger:
  - platform: state
    entity_id: sensor.system_####_humidity
    to: unavailable
condition: []
action:
  - service: homeassistant.reload_config_entry
    metadata: {}
    data: {}
    target:
      device_id:
        - ################################
        - ################################
mode: single

1 Like