Light State Change With Shell Commands

I’m trying to create a light that requires communicating to the Samsung SmartThings API. I’ve made a script that can turn it off, on and get the status. This all works except when you turn it on or off, the state doesn’t get updated until the binary_sensor scan_interval is triggered. This means the light toggle button can sometimes toggle back and forth while the binary sensor determines the state of the light. I’m thinking if the state of the light can be updated when it’s turned off or on rather than relying on the sensor would make more sense. However, I’m not able to figure out if that’s possible or if there is a better solution.

relevant configuration.yaml section:

shell_command:
    oven_on: bash /config/scripts/samsung_oven_light.sh on
    oven_off: bash /config/scripts/samsung_oven_light.sh off
    oven_status: bash /config/scripts/samsung_oven_light.sh status

light:
  - platform: template
    lights:
      oven_light:
        unique_id: samsung_oven_light
        friendly_name: "Oven Light"
        value_template: "{{ is_state('binary_sensor.oven_light_state', 'on')}}"
        turn_on:
          service: shell_command.oven_on
        turn_off:
          service: shell_command.oven_off
        icon_template:
            mdi:lightbulb

command_line:
  binary_sensor:
    command: "bash /config/scripts/samsung_oven_light.sh status"
    command_timeout: 10
    name: "Oven Light State Sensor"
    payload_on: "high"
    payload_off: "off"
    unique_id: oven_light_state
    scan_interval: 5
    device_class: light

I got it working the way I want by updating the state of the light sensor via the Home Assistant API every time the light is turned off or on with the script.

REST API | Home Assistant Developer Docs /api/states/<entity_id> endpoint

If anybody has a more elegant solution I’d love to hear it.

You don’t need to use an API script to do that. The turn_on and turn_off options can contain a list of actions and there is an update entity service.

light:
  - platform: template
    lights:
      oven_light:
        unique_id: samsung_oven_light
        friendly_name: "Oven Light"
        value_template: "{{ is_state('binary_sensor.oven_light_state', 'on') }}"
        turn_on:
          - service: shell_command.oven_on
          - delay: 1 # may not be required, test without it.
          - service: homeassistant.update_entity
            entity_id: binary_sensor.oven_light_state
        turn_off:
          - service: shell_command.oven_off
          - delay: 1 # may not be required, test without it.
          - service: homeassistant.update_entity
            entity_id: binary_sensor.oven_light_state
        icon_template:
            mdi:lightbulb