         case @handler.register_pin(pin_num) do
           :ok ->
             ConfigStorage.add_gpio_registry(pin_num, sequence_id)
-            new_state = %{state | registered: Map.put(state.registered, pin_num, sequence_id)}
+
+            new_state = %{
+              state
+              | registered: Map.put(state.registered, pin_num, sequence_id)
+            }
+
             {:reply, :ok, [{:gpio_registry, new_state.registered}], new_state}
-          {:error, _} = err -> {:reply, err, [], state}
+
+          {:error, _} = err ->
+            {:reply, err, [], state}
         end
-      _ -> {:reply, {:error, :already_registered}, [], state}
+
+      _ ->
+        {:reply, {:error, :already_registered}, [], state}
     end
-
   end
 
   def handle_call({:unregister_pin, pin_num}, _from, state) do
     case state.registered[pin_num] do
-      nil -> {:reply, {:error, :unregistered}, [], state}
+      nil ->
+        {:reply, {:error, :unregistered}, [], state}
+
       sequence_id ->
         case @handler.unregister_pin(pin_num) do
           :ok ->
             ConfigStorage.delete_gpio_registry(pin_num, sequence_id)
-            new_state = %{state | registered: Map.delete(state.registered, pin_num)}
+
+            new_state = %{
+              state
+              | registered: Map.delete(state.registered, pin_num)
+            }
+
             {:reply, :ok, [{:gpio_registry, new_state.registered}], new_state}
-          err -> {:reply, err, [], state}
+
+          err ->
+            {:reply, err, [], state}
         end
     end
   end


  defp do_execute(sequence_id, env) do
    import Farmbot.CeleryScript.AST.Node.Execute, only: [execute: 3]
    try do
      case execute(%{sequence_id: sequence_id}, [], env) do
        {:ok, env} -> env


  @doc false
  def start_link do
    GenStage.start_link(__MODULE__, [], name: __MODULE__)
  end

  defmodule State do
    @moduledoc false
    defstruct repo_up: false,
              registered: %{},
              handler: nil,
              env: struct(Macro.Env, [])
  end

  def init([]) do
    case @handler.start_link() do
      {:ok, handler} ->
        all_gpios = ConfigStorage.all_gpios()
        state = initial_state(all_gpios, struct(State, handler: handler))
        Process.send_after(self(), :update_fb_state_tree, 10)

        {:producer_consumer, state,
         subscribe_to: [handler], dispatcher: GenStage.BroadcastDispatcher}

      err ->
        err
    end
  end

  defp initial_state([], state), do: state

  defp initial_state(
         [%GpioRegistry{pin: pin, sequence_id: sequence_id} | rest],
         state
       ) do
    case @handler.register_pin(pin) do
      :ok ->
        initial_state(rest, %{
          state
          | registered: Map.put(state.registered, pin, sequence_id)
        })

      _ ->
        initial_state(rest, state)
    end
  end

  def handle_events(_, _, %{repo_up: false} = state) do
    Logger.warn(3, "Not handling gpio events until Asset storage is up.")
    {:noreply, [], state}
  end

  def handle_events(pin_triggers, _from, state) do
    t = Enum.uniq(pin_triggers)

    new_env =
      Enum.reduce(t, state.env, fn {:pin_trigger, pin}, env ->
        sequence_id = state.registered[pin]

        if sequence_id do
          Logger.busy(1, "Starting Sequence: #{sequence_id} from pin: #{pin}")
          do_execute(sequence_id, env)
        else
          Logger.warn(3, "No sequence assosiated with: #{pin}")
          env
        end
      end)

    {:noreply, [], %{state | env: new_env}}
  end

