public async Task <PendingCommand> Send <TCommand>(TCommand command, CancellationToken ct)
                where TCommand : HomeAssistantCommand
            {
                if (await _state.FirstOrDefaultAsync(state => state == State.Connected) != State.Connected)
                {
                    throw new InvalidOperationException("Connection failed");
                }

                using (await _sendGate.LockAsync(ct))
                    using (var id = _commandIdProvider.GetNext())
                    {
                        var sender  = new PendingCommand(command, this, id, ct);
                        var payload = JsonSerializer.SerializeToUtf8Bytes(command, JsonWriteOpts);

                        await _client.SendAsync(new ArraySegment <byte>(payload), WebSocketMessageType.Text, true, ct);

                        return(sender);
                    }
            }